怎么在Listboxes中加背景图 ? 求助代码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在Listboxes中加背景图 Delphi的例子1. 建立一个窗体2. 放一个ComboBox和Listbox3. 改变Component的Style为csOwnerDrawVariable和ListBox的Style为lbOwnerDrawVariable。4. 声明5个TBitmap的全局变量5. 覆盖Form的OnCreate.6. 覆盖ComboBox的OnDraw.7. 覆盖ComboBox的OnMeasureItem.8. 释放资源在Form的OnClose. 下面给出完整的主程序源程序: unit Ownerdrw;interfaceusesSysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,Forms, Dialogs, StdCtrls;typeTForm1 = 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;varForm1: TForm1;TheBitmap1, TheBitmap2, TheBitmap3, TheBitmap4,TheBitmap5 : TBitmap;implementation{$R *.DFM}procedure TForm1.FormCreate(Sender: TObject);beginTheBitmap1 := 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);beginTheBitmap1.Free;TheBitmap2.Free;TheBitmap3.Free;TheBitmap4.Free;TheBitmap5.Free;end;procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;Rect: TRect; State: TOwnerDrawState);varBitmap: TBitmap;Offset: Integer;beginwith (Control as TComboBox).Canvas dobeginFillRect(Rect);Bitmap := TBitmap(ComboBox1.Items.Objects[Index]);if Bitmap <> nil thenbeginBrushCopy(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);beginheight:= 20;end;procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;Rect: TRect; State: TOwnerDrawState);varBitmap: TBitmap;Offset: Integer;beginwith (Control as TListBox).Canvas dobeginFillRect(Rect);Bitmap := TBitmap(ListBox1.Items.Objects[Index]);if Bitmap <> nil thenbeginBrushCopy(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, Listbox1.Items[Index])end;end;procedure TForm1.ListBox1MeasureItem(Control: TWinControl; Index: Integer;var Height: Integer);beginheight:= 20;end; end. //该窗体的DFM文件object Form1: TForm1Left = 211Top = 155Width = 435Height = 300Caption = 'Form1'Font.Color = clWindowTextFont.Height = -13Font.Name = 'System'Font.Style = []PixelsPerInch = 96OnClose = FormCloseOnCreate = FormCreateTextHeight = 16object ComboBox1: TComboBoxLeft = 26Top = 30Width = 165Height = 22Style = csOwnerDrawVariableItemHeight = 16TabOrder = 0OnDrawItem = ComboBox1DrawItemOnMeasureItem = ComboBox1MeasureItemendobject ListBox1: TListBoxLeft = 216Top = 28Width = 151Height = 167ItemHeight = 16Style = lbOwnerDrawVariableTabOrder = 1OnDrawItem = ListBox1DrawItemOnMeasureItem = ListBox1MeasureItemendend 大家帮忙看看这句提示是什么意思 怎么从进程pid获得窗口句柄呢? 寻找一个希望有自己一片天空的兄弟 请教各位一下delphi的打印问题??? Delphi程序设计时字体的问题 怎么让程序排队等待? 0x01是16进制数???怎么定义?? 急!!关于ADOQUERY数据更新问题。(如何只修改多表连接的数据集中一个表的数据) 一个简单问题:如何在本地上实现数据库程序,而且不用安装Access,Foxpro等数据库? 又是一个键盘钩子问题 怎样捕捉原始出错信息? 高手请进
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;interfaceuses
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;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 (Control as TListBox).Canvas do
begin
FillRect(Rect);
Bitmap := TBitmap(ListBox1.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, Listbox1.Items[Index])
end;
end;procedure TForm1.ListBox1MeasureItem(Control: TWinControl; Index: Integer;
var Height: Integer);
begin
height:= 20;
end;
end. //该窗体的DFM文件object Form1: TForm1
Left = 211
Top = 155
Width = 435
Height = 300
Caption = 'Form1'
Font.Color = clWindowText
Font.Height = -13
Font.Name = 'System'
Font.Style = []
PixelsPerInch = 96
OnClose = FormClose
OnCreate = FormCreate
TextHeight = 16
object ComboBox1: TComboBox
Left = 26
Top = 30
Width = 165
Height = 22
Style = csOwnerDrawVariable
ItemHeight = 16
TabOrder = 0
OnDrawItem = ComboBox1DrawItem
OnMeasureItem = ComboBox1MeasureItem
end
object ListBox1: TListBox
Left = 216
Top = 28
Width = 151
Height = 167
ItemHeight = 16
Style = lbOwnerDrawVariable
TabOrder = 1
OnDrawItem = ListBox1DrawItem
OnMeasureItem = ListBox1MeasureItem
end
end