您所在的位置: 首页 > 开发 > 语言&工具 >

在Listbox加背景图

http://developer.51cto.com  2005-10-13 14:10    51cto.com整理  我要评论(0)
  • 摘要:本文讲解如何实在在Listbox加背景图的过程,并附注原代码
  • 标签:I  BO  s

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:\delphi\images\buttons\globe.bmp');
TheBitmap2 := TBitmap.Create;
TheBitmap2.LoadFromFile('C:\delphi\images\buttons\video.bmp');
TheBitmap3 := TBitmap.Create;
TheBitmap3.LoadFromFile('C:\delphi\images\buttons\gears.bmp');
TheBitmap4 := TBitmap.Create;
TheBitmap4.LoadFromFile('C:\delphi\images\buttons\key.bmp');
TheBitmap5 := TBitmap.Create;
TheBitmap5.LoadFromFile('C:\delphi\images\buttons\tools.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;


乔布斯病假月底结束 能否上岗决定苹果前途
艰难WAPI标准之路
2009年5月51CTO热门文章排行榜
Scala编程语言
华山论剑——五款主流服务器OS评测
 
 验证码: (点击刷新验证码)   匿名发表
  • 亮剑.NET. 图解C#开发实战

  • 作者:李新峰 付志涛 缪勇
  • 本书采用全新的图解思路,分3篇介绍使用微软C#语言开发实际应用程序的基本知识。第1篇包括10章,介绍了C#语言的基础知识,主要..
Copyright©2005-2009 51CTO.COM 版权所有