在TListBox和TComboBox中绘图
来源:互联网 发布:jsp连接sql的方法 编辑:程序博客网 时间:2024/04/30 03:21
导读: 涉及TListBox自定义重绘的属性和事件: 属性: Style: 取值为lbStandard(标准风格),lbOwnerDrawFixed( 所有者固定绘制风格),lbOwnerDrawVariable(所有者可变绘制风 格) 说明: 1. 当Style = lbStandard时,使用控件默认的绘制风格。 2. 当Style = lbOwnerDrawFixed时,用户只能在控件默认大小的区域绘图。 3. 当Style = lbOwnerDrawVariable时,用户可改变控件默认的绘图区域大小并决定如何绘图。 事件: OnMeasureItem:当Style = lbOwnerDrawVariable时计算TListBox中某项的高度时调用。 OnDrawItem :当Style = lbOwnerDrawVariable时由用户例程确定如何绘制TlistItem。 由此,可以看出,要实现定制界面风格的TListBox,首先,需要设置TlistBox的Style 属性为lbOwnerDrawVariable,其次,需要写自定义的重绘事件。 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) ListBox1: TListBox; Button1: TButton; Button2: TButton; ComboBox1: TComboBox; procedure Button1Click(Sender: TObject); 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} //Set ListBox1 and ComboBox1's Style = lbOwnerDrawVariable; procedure TForm1.Button1Click(Sender: TObject); begin ListBox1.Items.Add('asdfsadf'); end; procedure TForm1.FormCreate(Sender: TObject); begin TheBitmap1 := TBitmap.Create; TheBitmap1.LoadFromFile('31.bmp'); TheBitmap2 := TBitmap.Create; TheBitmap2.LoadFromFile('32.bmp'); TheBitmap3 := TBitmap.Create; TheBitmap3.LoadFromFile('33.bmp'); TheBitmap4 := TBitmap.Create; TheBitmap4.LoadFromFile('34.bmp'); TheBitmap5 := TBitmap.Create; TheBitmap5.LoadFromFile('35.bmp'); ComboBox1.Items.AddObject('Bitmap1', TheBitmap1); ComboBox1.Items.AddObject('Bitmap2', TheBitmap2); ComboBox1.Items.AddObject('Bitmap3', TheBitmap3); ComboBox1.Items.AddObject('Bitmap4', TheBitmap4); ComboBox1.Items.AddObject('Bitmap5', TheBitmap5); ListBox1.Items.AddObject('Bitmap1', TheBitmap1); ListBox1.Items.AddObject('Bitmap2', TheBitmap2); ListBox1.Items.AddObject('Bitmap3', TheBitmap3); ListBox1.Items.AddObject('Bitmap4', TheBitmap4); ListBox1.Items.AddObject('Bitmap5', 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 := 40; 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 := 40; end; end. 本文转自 http://hi.baidu.com/lobtao/blog/item/f8d76943521510129213c6bf.html
- 在TListBox和TComboBox中绘图
- 在TListBox和TComboBox中绘图
- Delphi中如何使用TEdit控件和TListBox控件组合使用达到TComboBox控件一样的效果呢?
- 如何在TStringGrid中加入TComboBox TCheckBox
- 如何在TStringGrid中加入TComboBox TCheckBox2
- 用于在TComboBox中存储编号的小过程
- 在窗口中绘图
- 在AWT中绘图
- 在Java中使用Draw2D和SWT绘图
- Qt5 在控件中绘图
- 在窗口中绘图(转)
- Qt 在控件中绘图
- 6. 在DIRECT3D中绘图
- C#在窗口中绘图
- Ttreeview和TlistBox的几个常用属性与方法
- 在QT和SDL搭建的框架中使用OPENGL在SDL窗口上进行绘图
- Android绘图基础之 ~~~ 在XML中绘图
- 在网页中引用DWG控件,交互绘图,和响应鼠标点击对象的方法
- 转:paypal express checkout
- 使用 IBM Data Studio 为 WAS 创建和部署 Data Web Services
- DbgridToExcell
- 百灵报表(BIRT)接口学习:设计report design
- 程序员的爱情表白
- 在TListBox和TComboBox中绘图
- 水晶报表中显示动态图片
- 《Linux命令行和Shell脚本开发圣经》:内容速览
- 高效率创建安全的 Java 应用,第 2 部分
- 定制MOSS列表编辑页面
- reportViewer1报表打印
- [OpenSolaris][kernel]Solaris内核加载设备驱动过程
- const用法
- Delphi中ListBox控件的六种特效