人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當(dāng)前位置:蘿卜系統(tǒng)下載站 > 技術(shù)開發(fā)教程 > 詳細(xì)頁(yè)面

運(yùn)用C++ Buider制作自繪畫下文框

運(yùn)用C++ Buider制作自繪畫下文框

更新時(shí)間:2022-09-27 文章作者:未知 信息來(lái)源:網(wǎng)絡(luò) 閱讀次數(shù):

C++ Builder中的列表框和組合框控件都已經(jīng)提供了自繪畫接口。使用這些接口可以實(shí)現(xiàn)把列表框和組合框中的項(xiàng)目顯示為圖像。如果把這種功能封裝成組件就更妙了,下面以列表框?yàn)槔痉哆@一過(guò)程。

  一 實(shí)現(xiàn)原理

  列表框有的Style屬性定義了三種風(fēng)格,lbStandard風(fēng)格只能顯示文本,bOwner-DrawFixed與lbOwnerDrawVariable風(fēng)格可以實(shí)現(xiàn)自繪畫功能,所不同的是,具有前者風(fēng)格的列表框中每一項(xiàng)的高度都是相同的,后者允許應(yīng)用程序?yàn)槊恳豁?xiàng)定義不同高度。

  具有自繪畫風(fēng)格的列表框,在列表框的外觀改變的時(shí)候,如增加,刪除,滾動(dòng)項(xiàng)目,總要觸發(fā)兩個(gè)事件句柄:

  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事件傳遞一個(gè)參數(shù)Height,應(yīng)用程序需要填寫一項(xiàng)來(lái)決定這一項(xiàng)的高度,如果沒(méi)有改變,則使用列表框的ItemHeight的值。lbOwnerDrawFixed風(fēng)格的列表框不觸發(fā)這一事件,故它使用自身的ItemHeight。OnDrawItem傳遞的Rect表示可在上作畫的矩形區(qū),程序可以使用列表框Canvas屬性來(lái)畫圖。


二 示例
  1 在IDE環(huán)境中,選擇“File-New”,在對(duì)話框中雙擊“Component”,出現(xiàn)"New Component"對(duì)話框,在Ancestor Type中選擇“TCustomListBox",在Class Name中輸入:  “TImageListBox”,點(diǎn)Create Uints,就生成一個(gè)類框架。

  2 在頭文件(ImageListBox.h)中的相應(yīng)域中,增加下列成員:
  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.在實(shí)現(xiàn)文件(ImageListBox.cpp)定義以下函數(shù):
   void __fastcall TImageListBox::MyMeasureItem(TWinControl *Control,                    int Index, int &Height)
   {
    if(tmpGraphic)
     Height=tmpGraphic->Height+2;
     file://因?yàn)镃++ Builder中的列表框封裝了LBS_HASSTRINGS特性,所以在這個(gè)事
     file://件中不能采用諸如Items->Objects[Index]形式來(lái)取得圖像數(shù)據(jù)。
   }

  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://取得圖像對(duì)象
    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對(duì)象。
   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;
    }
   }

三 測(cè)試組件

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

  在窗體的析構(gòu)函數(shù)中增加一句:“delete Til;”,運(yùn)行程序。

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

溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!

本類教程下載

系統(tǒng)下載排行

網(wǎng)站地圖xml | 網(wǎng)站地圖html
主站蜘蛛池模板: 黄龙县| 炎陵县| 通海县| 梁河县| 惠安县| 治县。| 昆明市| 漾濞| 历史| 固原市| 赫章县| 乌兰县| 南雄市| 漳州市| 神木县| 霞浦县| 吉木乃县| 肥东县| 贵阳市| 乌拉特前旗| 青州市| 射洪县| 高邮市| 祁东县| 南漳县| 和静县| 扎赉特旗| 永兴县| 息烽县| 登封市| 罗甸县| 宜兰市| 武鸣县| 南岸区| 临泉县| 四子王旗| 广汉市| 松桃| 沁水县| 阿合奇县| 石景山区|