在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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 网贷不知道在哪个平台借的怎么办 车过户后出现问题车主不承认怎么办 卖了车买家一直不过户怎么办 我的车卖了但是买家不过户怎么办 如果买家拍了赠品但联系不上怎么办 淘宝产品处罚下架顾客退货怎么办 生源地贷款续贷密码忘了怎么办 助学贷款续贷密码忘了怎么办 京东上回收东西如果是坏的怎么办 58.同城找的工作被骗了怎么办 京东第三方买了二手机怎么办 手机无法显示百度视频的视频怎么办 如果微信被盗号朋友钱被骗怎么办 绑定银行卡的电话号码换了怎么办办 银行卡绑定的手机号空号了怎么办 微信提示绑定银行卡次数超限怎么办 手机卡太久没用被注销了怎么办 电信宽带欠费缴费后上不了网怎么办 电信宽带欠费后缴费连不上网怎么办 电脑开不了机屏亮但不开机怎么办 手机信息探探链接点开了怎么办 买的钻戒的票丢掉了怎么办 如果我过户了原来的积分怎么办? 英雄联盟安装到了一半卡住了怎么办 微信登录不上怎么办一直在转圈 lol老是忘了放装备技能怎么办 英雄联盟屏幕出现红框锁定了怎么办 钢三开局修改对电脑有用怎么办 我的世界为什么一直黑屏闪退怎么办 苹果6plus玩游戏闪退怎么办 电脑重置开机黑屏了怎么办才好? 龟头有一小块和鱼鳞一样脱皮怎么办 海盗来了赠送碎片密码忘了怎么办 王者荣耀还差几百金币买英雄怎么办 英雄联盟更新后画面突然很卡怎么办 苹果手机微信登陆没反应怎么办 谷歌商店注册短信一直验证怎么办 英雄联盟开游戏退出来进不去怎么办 忘记了路由器和网关的密码怎么办 逆战无尽塔防71关没怪了怎么办 看香的师傅要钱特别多怎么办