发信人: skyice() 
整理人: supermario(1999-11-09 09:33:24), 站内信件
 | 
 
 
我想在listbox中为每个列表项添加位图,采用如下方法: 1.将listbox的style属性设为lbOnwerDrawvarible 2.unit.h文件内容: //-------------------------------------------------------------------- ------- #ifndef UnitH #define UnitH //-------------------------------------------------------------------- ------- #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> //-------------------------------------------------------------------- ------- class TForm1 : public TForm { __published:    // IDE-managed Components     TListBox *ListBox1;     void __fastcall ListBox1DrawItem(TWinControl *Control, int Index,
            TRect &Rect, TOwnerDrawState State);     void __fastcall ListBox1MeasureItem(TWinControl *Control, int Inde x,           int &Height);     void __fastcall FormCreate(TObject *Sender); private:        // User declarations         Graphics::TBitmap* Bitmap[4]; public:         // User declarations     __fastcall TForm1(TComponent* Owner); }; //-------------------------------------------------------------------- ------- extern PACKAGE TForm1 *Form1; //-------------------------------------------------------------------- ------- #endif
  3.unit.cpp 文件内容如下:
  //-------------------------------------------------------------------- ------- #include <vcl.h> #pragma hdrstop
  #include "Unit.h" //-------------------------------------------------------------------- ------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //-------------------------------------------------------------------- ------- __fastcall TForm1::TForm1(TComponent* Owner)     : TForm(Owner) { } //-------------------------------------------------------------------- -------
  void __fastcall TForm1::ListBox1DrawItem(TWinControl *Control, int Ind ex,       TRect &Rect, TOwnerDrawState State) {     Graphics::TBitmap * pBitmap;     int Offset=2;
      TCanvas * pCanvas=((TListBox *)Control)->Canvas;     pCanvas->FillRect(Rect);     pBitmap=(Graphics::TBitmap *)     ((TListBox *)Control)->Items->Objects[Index];     if(pBitmap)     {     pCanvas->BrushCopy(Bounds(Rect.Left+Offset,Rect.Top,     pBitmap->Width,pBitmap->Height),pBitmap,     Bounds(0,0,pBitmap->Width,pBitmap->Height),clRed);     Offset+=6;     }     pCanvas->TextOut(Rect.Left+Offset,Rect.Top+10,     ((TListBox *)Control)->Items->Strings[Index]);      } //-------------------------------------------------------------------- -------
  void __fastcall TForm1::ListBox1MeasureItem(TWinControl *Control,       int Index, int &Height) {     Graphics::TBitmap * pBitmap;     pBitmap->LoadFromResourceName(     (unsigned int)HInstance,     "Bitmap"+IntToStr(Index+1));     if(pBitmap)         Height=pBitmap->Height;     else         Height=13;      } //-------------------------------------------------------------------- ------- void __fastcall TForm1::FormCreate(TObject *Sender) {     int i;     for(i=0;i<=4;i++)     {         Bitmap[i]=new Graphics::TBitmap;         Bitmap[i]->LoadFromResourceName(         (unsigned int)HInstance,         "Bitmap"+IntToStr(i+1));     ListBox1->Items->AddObject("pic"+IntToStr(i+1),     Bitmap[i]);     }
  } //-------------------------------------------------------------------- ------- 4.在工程中插入资源文件unit.res,内容为Bitmap1-Bitmap5 但是显示出来的listbox并没有位图,关闭的时候就会出错,将位图加入到  project.res中也不行。不知道是否我插入位图的方法有错,还是别的原因 请各位大虾帮忙,谢了!
  -- ※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.99.83.185] 发信人: skeeter (小妖), 信区: CLanguage 标  题: Re: bcb菜鸟问题? 发信站: 网易虚拟社区 (Wed Nov  3 18:53:37 1999), 站内信件 //copy from a_hao's homepage Drawing icons or bitmaps in a ComboBox or ListBox
  In this example, we'll utilize the diversity of an ImageList to help u s draw graphics in a ListBox. This code applies to ComboBoxes as well.  The Combo/ListBox must be set to OwnerDrawn (fixed or variable). 
  (1) Drop a TListBox on your Form, and set it to OwnerDrawn (Fixed or V ariable). Also drop a TImageList on your form, and add your ListItem i cons or bitmaps
  (2) Declare an array of int(s) called MyIconMapper and set it's size t o however many items are in your ListBox... 
  //in header file  private: int MyIconMapper[1000]; 
  (3) Initialize the icon mapper, and handle the OnDrawItem event... 
  // MyIconMapper will define what icons in the  // ImageList correspond to what items in the ListBox.  // Define the mapping in your constructor...  __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)  {   MyIconMapper[0] = 1;  //ImageList image index 1 for ListBox item 0  MyIconMapper[1] = 0;  //ImageList image index 0 for ListBox item 1  MyIconMapper[2] = 1;  //ImageList image index 1 for ListBox item 2  // and so on...  }  //-------------------------------------------------------------------- -------  // Add the following to the ListBox's OnDrawItem event handler...  void __fastcall TForm1::ListBox1DrawItem(TWinControl *Control, int Ind ex, TRect &Rect, TOwnerDrawState State) {   //Eliminate artifacts ListBox1->Canvas->FillRect(Rect);  if (State.Contains(odSelected))  {    ListBox1->Canvas->Font->Color = clWhite;   ListBox1->Canvas->Brush->Color = clNavy;  }   //Draw your Icon ImageList1->Draw(ListBox1->Canvas, Rect.Left, Rect. Top, MyIconMapper[Index]);  int offset = ImageList1->Width + 2;  ListBox1->Canvas->TextOut(Rect.Left + offset, Rect.Top, ListBox1->It ems->Strings[Index]);  } 
  -- 每次当你想着谁 就披散了头发任风吹  沉默脸上留着泪 连哭了都没感觉  我想知道他是谁 可是话到嘴边却后悔  在这温柔的长夜 何必让你流泪 
  ※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.98.120.226] 发信人: mrcloud (阿豪), 信区: CLanguage 标  题: Re: bcb菜鸟问题? 发信站: 网易虚拟社区 (Wed Nov  3 20:35:46 1999), 站内信件 刚刚试了一下. ListBox1MeasureItem事件里要改一下:     Graphics::TBitmap *pBitmap=new Graphics::TBitmap;     ......     delete pBitmap; 还有一个地方,我看了很长时间:   private: // User declarations          Graphics::TBitmap* Bitmap[4]; <--Bitmap1~5,怎么只有4啊? 然后就pass了. -- 阿豪 [email protected] 最好的BCB学习网站 C++Builder开发网络(http://a_hao.163.net) C++Builder 论坛(http://cppbahao.abc.yesite.com)
  ※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.101.57.159] 发信人: skeeter (小妖), 信区: CLanguage 标  题: Re: bcb菜鸟问题? 发信站: 网易虚拟社区 (Thu Nov  4 09:50:31 1999), 站内信件 问一哈:     你是怎莫把位图加入资源文件的???用IMAGELIST???     我编译乐还是通不过????     我再想想.......
  -- 每次当你想着谁 就披散了头发任风吹  沉默脸上留着泪 连哭了都没感觉  我想知道他是谁 可是话到嘴边却后悔  在这温柔的长夜 何必让你流泪 
  ※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.98.120.226] 发信人: mrcloud (阿豪), 信区: CLanguage 标  题: Re: bcb菜鸟问题? 发信站: 网易虚拟社区 (Thu Nov  4 10:51:05 1999), 站内信件 用Tools->ImageEditor new一个res文件 再new五个bmp 保存为unit1.res 再在project viewer 里加入此资源文件即可。
  -- 阿豪 [email protected] 最好的BCB学习网站 C++Builder开发网络(http://a_hao.163.net) C++Builder 论坛(http://cppbahao.abc.yesite.com)
  ※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.101.17.228]
  | 
 
 
 |