<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      使用C++ Buider制作自繪畫下文框

      [摘要]C++ Builder中的列表框和組合框控件都已經提供了自繪畫接口。使用這些接口可以實現把列表框和組合框中的項目顯示為圖像。如果把這種功能封裝成組件就更妙了,下面以列表框為例,示范這一過程! ∫ 實現原理  列表框有的Style屬性定義了三種風格,lbStandard風格只能顯示文本,bOwne...
      C++ Builder中的列表框和組合框控件都已經提供了自繪畫接口。使用這些接口可以實現把列表框和組合框中的項目顯示為圖像。如果把這種功能封裝成組件就更妙了,下面以列表框為例,示范這一過程。

        一 實現原理

        列表框有的Style屬性定義了三種風格,lbStandard風格只能顯示文本,bOwner-DrawFixed與lbOwnerDrawVariable風格可以實現自繪畫功能,所不同的是,具有前者風格的列表框中每一項的高度都是相同的,后者允許應用程序為每一項定義不同高度。

        具有自繪畫風格的列表框,在列表框的外觀改變的時候,如增加,刪除,滾動項目,總要觸發兩個事件句柄:

        TMeasureItemEvent OnMeasureItem;
        TDrawItemEvent OnDrawItem;
        typedef void __fastcall (_closure *TDrawItemEvent)(TWinControl* Control,int Index,TRect& Rect; TOwnerDrawState State);
        typedef void __fastcall(_closure* TMeasureItemEvent)(TWinControl* Control, int Index,int& Height);

      OnMeasureItem事件傳遞一個參數Height,應用程序需要填寫一項來決定這一項的高度,如果沒有改變,則使用列表框的ItemHeight的值。lbOwnerDrawFixed風格的列表框不觸發這一事件,故它使用自身的ItemHeight。OnDrawItem傳遞的Rect表示可在上作畫的矩形區,程序可以使用列表框Canvas屬性來畫圖。


      二 示例
        1 在IDE環境中,選擇“File-New”,在對話框中雙擊“Component”,出現"New Component"對話框,在Ancestor Type中選擇“TCustomListBox",在Class Name中輸入:  “TImageListBox”,點Create Uints,就生成一個類框架。

        2 在頭文件(ImageListBox.h)中的相應域中,增加下列成員:
        private:
         Graphics::TGraphic* tmpGraphic;
        protected:
         void __fastcall MyDrawItem(TWinControl *Control,                int Index, const TRect &Rect, TOwnerDrawState State);
         void __fastcall MyMeasureItem(TWinControl *Control,                int Index, int &Height);
        public:
         __fastcall TImageListBox(TComponent* Owner);
         __fastcall ~TImageListBox();
         void __fastcall AddImage(System::AnsiString Filename,                System::AndiString* String);
         ...

        3.在實現文件(ImageListBox.cpp)定義以下函數:
         void __fastcall TImageListBox::MyMeasureItem(TWinControl *Control,                    int Index, int &Height)
         {
          if(tmpGraphic)
           Height=tmpGraphic->Height+2;
           file://因為C++ Builder中的列表框封裝了LBS_HASSTRINGS特性,所以在這個事
           file://件中不能采用諸如Items->Objects[Index]形式來取得圖像數據。
         }

        void __fastcall TImageListBox::MyDrawItem(TWinControl *Control,
         int Index, const TRect &Rect, TOwnerDrawState State)
         {
          int Offset = 2; // 定義文本與圖像的距離
          TCanvas *pCanvas = ((TListBox *)Control)->Canvas;
          pCanvas->FillRect(Rect); file://填充方框
          file://取得圖像對象
          TGraphic* tmpImage=(TGraphic*)(Items->Objects[Index]);
          pCanvas->Draw(Rect.Left+Offset,Rect.Top,tmpImage); file://畫圖
          if(tmpImage)Offset+=tmpImage->Width+4;
           file://顯示文本
           pCanvas->TextOut(Rect.Left + Offset, Rect.Top,                   ((TListBox*)Control)->Items->Strings[Index]);
         }
         file://------------------------------------------------------------------

         void __fastcall TImageListBox::AddImage(System::AnsiString Filename,      System::AnsiString* String)
        {
         file://裝載圖像,并追加至Objects對象。
         if(Filename.Pos(".ico"))
         { tmpGraphic=new Graphics::TIcon();
          tmpGraphic->LoadFromFile(Filename);
          Items->AddObject(String,(TObject*)tmpGraphic);
         }
         else if(Filename.Pos(".bmp"))
         { tmpGraphic=new Graphics::TBitmap();
          tmpGraphic->LoadFromFile(Filename);
          Items->AddObject(String,(TObject*)tmpGraphic);
         }
         tmpGraphic=NULL;
         }

         __fastcall TImageListBox::TImageListBox(TComponent* Owner)
            : TCustomListBox(Owner)
         {
          Style=lbOwnerDrawVariable;
          OnDrawItem=MyDrawItem;
          OnMeasureItem=MyMeasureItem;
         }

         __fastcall TImageListBox::~TImageListBox()
         { file://釋放圖像資源
          for(int i=0;iCount;i++)
          { if((tmpGraphic=(TGraphic*)Items->Objects[i])!=NULL)
            delete tmpGraphic;
          }
         }

      三 測試組件

        新建一個工程,先在工程中添加剛才建立的ImageListBox.cpp,并在主窗體的頭文件(.h)及實現文件(.cpp)中增加#include "Imagelistbox.h". 然后在private域中增加一個成員:
        TImageListBox* Til;
        在窗體的構造函數中增加如下代碼:
        Til=new TImageListBox(this);
        Til->Parent=this;
        Til->Width=80;
        Til->Height=90;
        Til->AddImage("1.ico","First");
        Til->AddImage("2.bmp,"Second");
        ...

        在窗體的析構函數中增加一句:“delete Til;”,運行程序。

      以上代碼在Windows 95 OSR2 ,C++ Builder 3.0中編譯測試通過。讀者可以自行修改,使功能更加完善。


      日韩精品一区二区三区高清