使用C++Buider制作自绘画列表框

王朝c/c++·作者佚名  2008-06-01
窄屏简体版  字體: |||超大  

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中编译测试通过。读者可以自行修改,使功能更加完善。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航