阅读内容 

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

[日期:2001-02-07] 来源:yesky  作者:不详 [字体: ]

  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中编译测试通过。读者可以自行修改,

使功能更加完善。

阅读:
录入:

推荐 】 【 打印
相关新闻      
本文评论       全部评论
发表评论
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款


点评: 字数
姓名:
Advertisement
内容查询


Advertisement