Delphi学习:在Listbox加背景图

liuxc0012

liuxc0012

2016-01-29 14:10

Delphi学习:在Listbox加背景图,Delphi学习:在Listbox加背景图
 
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
展开更多 50%)
分享

猜你喜欢

Delphi学习:在Listbox加背景图

Delphi
Delphi学习:在Listbox加背景图

在Listbox加背景图

编程语言 网络编程
在Listbox加背景图

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

在Listboxes中加背景图

编程语言 网络编程
在Listboxes中加背景图

对C#中的TreeView加背景图

C语言教程 C语言函数
对C#中的TreeView加背景图

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

PS复杂背景图抠图方法

PS
PS复杂背景图抠图方法

美丽说怎么更换背景图

手机软件 应用软件
美丽说怎么更换背景图

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

制作真正的TopMost窗口

制作真正的TopMost窗口

Delphi单元文件详解

Delphi单元文件详解
下拉加载更多内容 ↓