Delphi学习:在Listbox加背景图

2016-01-29 14:10 84 1 收藏

Delphi学习:在Listbox加背景图,Delphi学习:在Listbox加背景图

【 tulaoshi.com - Delphi 】

 
1. 建立一个窗体
  2. 放一个ComboBox和Listbox
  3. 改变Component的Style为csOwnerDrawVariable和ListBox的Style为lbOwnerDrawVariable。
  4. 声明5个TBitmap的全局变量
  5. 覆盖Form的OnCreate.
  6. 覆盖ComboBox的OnDraw.
  7. 覆盖ComboBox的OnMeasureItem.
  8. 释放资源在Form的OnClose.
  
  unit Ownerdrw;
  
  interface
  
  uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls;
  
  type
  TForm1 = class(TForm)
  ComboBox1: TComboBox;
  ListBox1: TListBox;
  procedure FormCreate(Sender: TObject);
  procedure FormClose(Sender: TObject; var Action: TCloseAction);
  procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
  procedure ComboBox1MeasureItem(Control: TWinControl; Index: Integer;
  var Height: Integer);
  procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
  procedure ListBox1MeasureItem(Control: TWinControl; Index: Integer;
  var Height: Integer);
  
  private
  { Private declarations }
  
  public
  { Public declarations }
  
  end;
  
  var
  Form1: TForm1;
  TheBitmap1, TheBitmap2, TheBitmap3, TheBitmap4,
  TheBitmap5 : TBitmap;
  implementation
  
  {$R *.DFM}
  
  procedure TForm1.FormCreate(Sender: TObject);
  begin
  TheBitmap1 := TBitmap.Create;
  TheBitmap1.LoadFromFile('C:delphiimagesbuttonsglobe.bmp');
  TheBitmap2 := TBitmap.Create;
  TheBitmap2.LoadFromFile('C:delphiimagesbuttonsvideo.bmp');
  TheBitmap3 := TBitmap.Create;
  TheBitmap3.LoadFromFile('C:delphiimagesbuttonsgears.bmp');
  TheBitmap4 := TBitmap.Create;
  TheBitmap4.LoadFromFile('C:delphiimagesbuttonskey.bmp');
  TheBitmap5 := TBitmap.Create;
  TheBitmap5.LoadFromFile('C:delphiimagesbuttonstools.bmp');
  ComboBox1.Items.AddObject('Bitmap1: Globe', TheBitmap1);
  ComboBox1.Items.AddObject('Bitmap2: Video', TheBitmap2);
  ComboBox1.Items.AddObject('Bitmap3: Gears', TheBitmap3);






ComboBox1.Items.AddObject('Bitmap4: Key', TheBitmap4);
  ComboBox1.Items.AddObject('Bitmap5: Tools', TheBitmap5);
  ListBox1.Items.AddObject('Bitmap1: Globe', TheBitmap1);
  ListBox1.Items.AddObject('Bitmap2: Video', TheBitmap2);
  ListBox1.Items.AddObject('Bitmap3: Gears', TheBitmap3);
  ListBox1.Items.AddObject('Bitmap4: Key', TheBitmap4);
  ListBox1.Items.AddObject('Bitmap5: Tools', TheBitmap5);
  
  end;
  
  procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  begin
  TheBitmap1.Free;
  TheBitmap2.Free;
  TheBitmap3.Free;
  TheBitmap4.Free;
  TheBitmap5.Free;
  end;
  
  procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
  var
  Bitmap: TBitmap;
  Offset: Integer;
  begin
  with (Control as TComboBox).Canvas do
  begin
  FillRect(Rect);
  Bitmap := TBitmap(ComboBox1.Items.Objects[Index]);
  if Bitmap < nil then
  begin
  BrushCopy(Bounds(Rect.Left + 2, Rect.Top + 2, Bitmap.Width,
  Bitmap.Height), Bitmap, Bounds(0, 0, Bitmap.Width,
  Bitmap.Height), clRed);
  Offset := Bitmap.width + 8;
  end;
  { display the text }
  TextOut(Rect.Left + Offset, Rect.Top, Combobox1.Items[Index])
  end;
  end;
  
  procedure TForm1.ComboBox1MeasureItem(Control: TWinControl; Index:
  Integer; var Height: Integer);
  begin
  height:= 20;
  end;
  
  procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
  var
  Bitmap: TBitmap;
  Offset: Integer;
  begin
  with (Co

来源:https://www.tulaoshi.com/n/20160129/1492690.html

延伸阅读
标签: Delphi
  开发中有时需要在流中查找某个字串,这些字串有可能是ASCII表后半部分或混杂.但是网上提供的例程效果不佳而且代码冗长. 后来自己抽个时间写了一个,代码极少,效率很高.仅供各位参考: function ScanStream(T:Tstream;S:String):integer; var i,j:integer; p:Pchar; begin getMem(p,T.size);//分配内...
原图 加上背景的效果图 抠出的效果图
    如果我们希望在网页的不同角落里放置不重复平铺的背景图,该怎么办呢? 比如网页的背景要如图所示,并要求在不同的分辩率下该背景图都显示在右下角,想想看,有办法吗? 想将它做成一张大图?!不行,不同的分辩率,图的大小不同呀,你只能做一张背景。怎么办呢??? 别担心,你只需要下面这个背...
懒人天气修改背景图方法   1)安装并打开懒人天气,点击主界面中的。   2)选择,点击在对话框中点击。   3)根据需要选择自己喜欢的背景图即可。  
懒人天气如何更换背景图   1.安装并打开懒人天气,点击主界面中的。 2.选择,点击在对话框中点击。 3.根据需要选择自己喜欢的背景图即可。 懒人天气着装参考操作方法   1)打开懒人天气,然后把主界面拉到最低端的www.tulaoshi.com。   2)点击进去之后可以根据自己想穿的类型进行选择,...

经验教程

680

收藏

7
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部