国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

使用C++ Buider制作自繪畫列表框

2019-11-17 05:13:21
字體:
來源:轉載
供稿:網友

  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;
     //因為C++ Builder中的列表框封裝了LBS_HASSTRINGS特性,所以在這個事
     //件中不能采用諸如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); //填充方框
   //取得圖像對象
   Tgraphic* tmpImage=(Tgraphic*)(Items->Objects[Index]);
   pCanvas->Draw(Rect.Left+Offset,Rect.Top,tmpImage); //畫圖
   if(tmpImage)Offset+=tmpImage->Width+4;  //顯示文本
   pCanvas->TextOut(Rect.Left + Offset, Rect.Top, ((TListBox *)Control)->Items->Strings[Index]);
   }
  //--------------------------------------------------------------------------
  void __fastcall TImageListBox::AddImage(System::AnsiString Filename,System::AnsiString* String)
  {
   //裝載圖像,并追加至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()
   { //釋放圖像資源
    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中編譯測試通過。讀者可以自行修改,使功能更加完善。  


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安宁市| 昆山市| 德安县| 油尖旺区| 仙居县| 灵丘县| 襄樊市| 佛坪县| 馆陶县| 水富县| 江山市| 永顺县| 德庆县| 万山特区| 宽城| 治多县| 台州市| 西乌珠穆沁旗| 高平市| 西林县| 太仆寺旗| 太白县| 精河县| 洛阳市| 屯昌县| 班玛县| 呼图壁县| 七台河市| 三穗县| 许昌市| 高碑店市| 阿克陶县| 三台县| 固安县| 乐业县| 黄陵县| 墨竹工卡县| 巢湖市| 台州市| 沁水县| 巴中市|