中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > C++Builder
使用C++ Buider制作自绘画列表框
作者:未知 时间:2005-08-07 21:02 出处:编程爱好者网站 责编:chinaitpower
              摘要:使用C++ Buider制作自绘画列表框
  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中编译测试通过。读者可以自行修改,使功能更加完善。

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有