在Listbox中增加背景圖
來源:易賢網(wǎng) 閱讀:866 次 日期:2014-09-05 11:50:51
溫馨提示:易賢網(wǎng)小編為您整理了“在Listbox中增加背景圖”,方便廣大網(wǎng)友查閱!

1. 建立一個(gè)窗體

2. 放一個(gè)ComboBox和Listbox

3. 改變Component的Style為csOwnerDrawVariable和ListBox的Style為lbOwnerDrawVariable。

4. 聲明5個(gè)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:delphiimagesuttonsglobe.bmp');

TheBitmap2 := TBitmap.Create;

TheBitmap2.LoadFromFile('C:delphiimagesuttonsvideo.bmp');

TheBitmap3 := TBitmap.Create;

TheBitmap3.LoadFromFile('C:delphiimagesuttonsgears.bmp');

TheBitmap4 := TBitmap.Create;

TheBitmap4.LoadFromFile('C:delphiimagesuttonskey.bmp');

TheBitmap5 := TBitmap.Create;

TheBitmap5.LoadFromFile('C:delphiimagesuttonsools.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

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:在Listbox中增加背景圖
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)