自绘ListBox的两种效果
2006-02-04 13:57:00 来源:WEB开发网 闂傚倸鍊搁崐鎼佸磹閹间礁纾归柟闂寸绾惧綊鏌熼梻瀵割槮缁炬儳缍婇弻鐔兼⒒鐎靛壊妲紒鐐劤缂嶅﹪寮婚悢鍏尖拻閻庨潧澹婂Σ顔剧磼閻愵剙鍔ょ紓宥咃躬瀵鎮㈤崗灏栨嫽闁诲酣娼ф竟濠偽i鍓х<闁诡垎鍐f寖闂佺娅曢幑鍥灳閺冨牆绀冩い蹇庣娴滈箖鏌ㄥ┑鍡欏嚬缂併劎绮妵鍕箳鐎n亞浠鹃梺闈涙搐鐎氫即鐛崶顒夋晬婵絾瀵ч幑鍥蓟閻斿摜鐟归柛顭戝枛椤牆顪冮妶搴′簼缂侇喗鎸搁悾鐑藉础閻愬秵妫冮崺鈧い鎺戝瀹撲礁鈹戦悩鎻掝伀缁惧彞绮欓弻娑氫沪閹规劕顥濋梺閫炲苯澧伴柟铏崌閿濈偛鈹戠€n€晠鏌嶆潪鎷屽厡闁汇倕鎳愮槐鎾存媴閸撴彃鍓卞銈嗗灦閻熲晛鐣烽妷褉鍋撻敐搴℃灍闁绘挻娲橀妵鍕箛闂堟稐绨肩紓浣藉煐濮樸劎妲愰幘璇茬闁冲搫鍊婚ˇ鏉库攽椤旂》宸ユい顓炲槻閻g兘骞掗幋鏃€鐎婚梺瑙勬儗閸樺€熲叺婵犵數濮烽弫鍛婃叏椤撱垹纾婚柟鍓х帛閳锋垶銇勯幒鍡椾壕缂備礁顦遍弫濠氱嵁閸℃稒鍊烽柛婵嗗椤旀劕鈹戦悜鍥╃У闁告挻鐟︽穱濠囨嚃閳哄啰锛滈梺褰掑亰閸欏骸鈻撳⿰鍫熺厸閻忕偟纭堕崑鎾诲箛娴e憡鍊梺纭呭亹鐞涖儵鍩€椤掑啫鐨洪柡浣圭墪閳规垿鎮欓弶鎴犱桓闂佸湱枪閹芥粎鍒掗弮鍫熷仺缂佸顕抽敃鍌涚厱闁哄洢鍔岄悘鐘绘煕閹般劌浜惧┑锛勫亼閸婃牠宕濋敃鈧…鍧楀焵椤掍胶绠剧€光偓婵犱線鍋楀┑顔硷龚濞咃絿妲愰幒鎳崇喓鎷犻懠鑸垫毐闂傚倷鑳舵灙婵炲鍏樺顐ゆ嫚瀹割喖娈ㄦ繝鐢靛У绾板秹寮查幓鎺濈唵閻犺櫣灏ㄥ銉р偓瑙勬尭濡繂顫忛搹鍦<婵☆垰鎼~宥囩磽娴i鍔嶉柟绋垮暱閻g兘骞嬮敃鈧粻濠氭偣閸パ冪骇鐎规挸绉撮—鍐Χ閸℃ê闉嶇紓浣割儐閸ㄥ墎绮嬪澶嬪€锋い鎺嶇瀵灝鈹戦埥鍡楃仯闁告鍕洸濡わ絽鍟崐鍨叏濡厧浜鹃悗姘炬嫹

演示图片:

//--------------------------------------------------------------------------
unit DrawListItem;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ImgList, jpeg, ExtCtrls;
type
TForm1 = class(TForm)
lsbRight: TListBox;
ImageList1: TImageList;
StaticText1: TStaticText;
lsbLeft: TListBox;
imgHouse: TImage;
imgHouseGray: TImage;
PRocedure FormCreate(Sender: TObject);
procedure lsbRightDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure lsbRightClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure lsbLeftDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
private
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{========================================================================
DESIGN BY : 彭国辉
DATE: 2004-11-29
SITE: http://kacarton.yeah.net/
BLOG: http://blog.csdn.net/nhconch
EMAIL: kacarton@sohu.com
文章为作者原创,转载前请先与本人联系,转载请注明文章出处、保留作者信息,谢谢支持!
=========================================================================}
procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
begin
lsbRight.Style := lbOwnerDrawFixed;
lsbRight.Ctl3D := false;
lsbRight.ItemHeight := 50;
lsbRight.Items.Add('C++ Builder 研究 www.ccrun.com'#13'致力于BCB的学习探讨和研究'#13'ccrun(老妖)');
lsbRight.Items.Add('编程手札 My Developer Knowledge Base'#13'http://blog.csdn.net/nhconch'#13'天蝎蝴蝶');
for i:=3 to 10 do begin
lsbRight.Items.Add('ListBox Items of ' + IntTostr(i) + #13'Second of '
+ IntToStr(i) + #13'Third of ' + IntToStr(i));
end;
lsbLeft.Style := lbOwnerDrawFixed;
lsbLeft.Ctl3D := false;
lsbLeft.ItemHeight := 90;
lsbLeft.Items.Add('编程手札');
lsbLeft.Items.Add('My Developer Knowledge Base');
lsbLeft.Items.Add('站长:天蝎蝴蝶');
lsbLeft.Items.Add('http://blog.csdn.net/nhconch');
end;
procedure TForm1.lsbRightDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
strTemp: String;
begin
//文字颜色
lsbRight.Canvas.Font.Color := clBlack;
//设置背景颜色并填充背景
lsbRight.Canvas.Brush.Color := clWhite;
lsbRight.Canvas.FillRect (Rect);
//设置圆角矩形颜色并画出圆角矩形
lsbRight.Canvas.Brush.Color := TColor($00FFF7F7);
lsbRight.Canvas.Pen.Color := TColor($00131315);
lsbRight.Canvas.RoundRect(Rect.Left + 3, Rect.Top + 3,
Rect.Right - 2, Rect.Bottom - 2, 8, 8);
//以不同的宽度和高度再画一次,实现立体效果
lsbRight.Canvas.RoundRect(Rect.Left + 3, Rect.Top + 3,
Rect.Right - 3, Rect.Bottom - 3, 5, 5);
//如果是当前选中项
if(odSelected in State) then
begin
//以不同的背景色画出选中项的圆角矩形
lsbRight.Canvas.Brush.Color := TColor($00FFB2B5);
lsbRight.Canvas.RoundRect(Rect.Left + 3, Rect.Top + 3,
Rect.Right - 3, Rect.Bottom - 3, 5, 5);
//选中项的文字颜色
lsbRight.Canvas.Font.Color := clBlue;
//如果当前项拥有焦点,画焦点虚框,当系统再绘制时变成XOR运算从而达到擦除焦点虚框的目的
if(odFocused in State) then DrawFocusRect(lsbRight.Canvas.Handle, Rect);
end;
//画出图标
ImageList1.Draw(lsbRight.Canvas, Rect.Left + 7,
Rect.top + (lsbRight.ItemHeight - ImageList1.Height) div 2, Index, true);
//分别绘出三行文字
strTemp := lsbRight.Items.Strings[Index];
lsbRight.Canvas.TextOut(Rect.Left + 32 + 10, Rect.Top + 4
, Copy(strTemp, 1, Pos(#13, strTemp)-1));
strTemp := Copy(strTemp, Pos(#13, strTemp)+1, Length(strTemp));
lsbRight.Canvas.TextOut(Rect.Left + 32 + 10, Rect.Top + 18,
Copy(strTemp, 1, Pos(#13, strTemp)-1));
lsbRight.Canvas.TextOut(Rect.Left + 32 + 10, Rect.Top + 32,
Copy(strTemp, Pos(#13, strTemp)+1, Length(strTemp)));
end;
procedure TForm1.lsbRightClick(Sender: TObject);
begin
StaticText1.Caption := ' ' + lsbRight.Items.Strings[lsbRight.ItemIndex];
end;
procedure TForm1.FormShow(Sender: TObject);
begin
lsbRight.ItemIndex := 0;
lsbRight.Repaint();
lsbLeft.ItemIndex := 0;
lsbLeft.Repaint();
end;
procedure TForm1.lsbLeftDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
r: TRect;
begin
with lsbLeft.Canvas do begin
//设置填充的背景颜色并填充背景
Brush.Color := clWhite;
FillRect (Rect);
//绘制圆角矩形
if (odSelected in State) then //选中项的圆角矩形颜色
Pen.Color := $FFB2B5
else //未选中项的圆角矩形颜色
Pen.Color := clSilver;
Brush.Style := bsClear;
SetRect(r, Rect.Left+3, Rect.Top+3, Rect.Right-3, Rect.Bottom-3);
RoundRect(r.Left, r.Top, r.Right, r.Bottom, 10, 10);
//画出图标
if (odSelected in State) then //选中项的图像
Draw(r.Left + (r.Right - r.Left - imgHouse.Width) shr 1,
r.Top + 2, imgHouse.Picture.Graphic)
else //未选中项的图像
Draw(r.Left + (r.Right - r.Left - imgHouseGray.Width) shr 1,
r.Top + 2, imgHouseGray.Picture.Graphic);
//填充文字区背景
r.Top := r.Bottom - Abs(Font.Height) - 4;
Brush.Style := bsSolid;
if (odSelected in State) then //选中项的背景颜色
Brush.Color := $FFB2B5
else //未选中项的背景颜色
Brush.Color := clSilver;
FillRect(r);
//输出文字,仅支持单行
Font.Color := clBlack;
r.Top := r.Top + 2; //计算文字顶点位置,(水平居中,DT_CENTER不可用)
DrawText(Handle, PChar(TListBox(Control).Items.Strings[Index]), -1, r
, DT_CENTER or DT_END_ELLipSIS{ or DT_WordBREAK});
//画焦点虚框,当系统再绘制时,变成XOR运算,从而达到擦除焦点虚框的目的
if(odFocused in State) then DrawFocusRect(Rect);
end;
end;
end.
赞助商链接