当前位置:天才代写 > tutorial > C语言/C++ 教程 > 用C++ Buider建造自绘画列表框

用C++ Buider建造自绘画列表框

2017-11-06 08:00 星期一 所属: C语言/C++ 教程 浏览:611

副标题#e#

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)中的相应域中,增加下列成员:


#p#副标题#e#

  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中编译测试通过。读者可以自行修改,使成果越发完善。

 

    关键字:

[contact-form-7 id="17097" title="form2"]天才代写-代写联系方式