This paper uses the ListBox self-painting to achieve two special effects (see figure), the left side of the style is their own sudden inspiration to think of, the right side of the style from the " C + + Builder research " a post, the old demon with BCB realized, here is to use Delphi to achieve it.
Demo Image:
//--------------------------------------------------------------------------
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;
procedureFormcreate (Sender:tobject);
procedureLsbrightdrawitem (Control:twincontrol; Index:integer;
Rect:trect; State:townerdrawstate);
procedureLsbrightclick (Sender:tobject);
procedureFormshow (Sender:tobject);
procedureLsbleftdrawitem (Control:twincontrol; Index:integer;
Rect:trect; State:townerdrawstate);
Private
Public
{Public declarations}
end;
Var
Form1:tform1;
Implementation
{$R *.DFM}
{========================================================================
DESIGN by: Peng
date:2004-11-29
SITE: http://kacarton.yeah.net/
BLOG: Http://blog.csdn.net/nhconch
EMAIL: kacarton#sohu.com
article for the author original, please first contact with me, reproduced please indicate the source of the article, retain the author information, thank you for your support!
=========================================================================}
Procedure tform1.formcreate (sender:tobject );
var
I:integer;
Begin
Lsbright.style: = lbownerdrawfixed;
Lsbright.ctl3d: = false;
lsbright.itemheight: = 50 ;
lsbRight.Items.Add (' dedicated to BCB learning and research ' #13 ' ccrun (old Demon) ');
lsbRight.Items.Add (' ');
for i:=3 To 10 do begin
lsbRight.Items.Add (' #13 ' second of '
+ inttostr (i) + #13 ' end;
Lsbleft.style: = lbownerdrawfixed;
Lsbleft.ctl3d: = false;
Lsbleft.itemheight: = ;
LSBLEFT.ITEMS.ADD (' programming Codex ');
LsbLeft.Items.Add ('My Developer knowledge Base');
LSBLEFT.ITEMS.ADD (' webmaster: Scorpio Butterfly ');
LsbLeft.Items.Add ('http://blog.csdn.net/nhconch');
End
procedureTform1.lsbrightdrawitem (Control:twincontrol; Index:integer;
Rect:trect; State:townerdrawstate);
var
strtemp:String;
begin
//Text color
LsbRight.Canvas.Font.Color: = Clblack;
//Set background color and fill background
LsbRight.Canvas.Brush.Color: = Clwhite;
LsbRight.Canvas.FillRect (Rect);
//Set Rounded rectangle color and draw rounded rectangle
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);
//with different width and height to draw again, achieve three-dimensional effect
LsbRight.Canvas.RoundRect (Rect.left +3, Rect.top +3,
Rect.right-3, Rect.bottom-3,5,5);
//If the item is currently selected
if(odselectedinchState) Then
begin
//Draw a rounded rectangle of the selected item with a different background color
LsbRight.Canvas.Brush.Color: = TColor ($00ffb2b5);
LsbRight.Canvas.RoundRect (Rect.left +3, Rect.top +3,
Rect.right-3, Rect.bottom-3,5,5);
//The text color of the selected item
LsbRight.Canvas.Font.Color: = Clblue;
//If the current item has focus, draw the focus virtual box, and when the system is redrawn it becomes an XOR operation to erase the focus dashed box.
if(odfocusedinchState) ThenDrawFocusRect (LsbRight.Canvas.Handle, Rect);
end;
//Draw out icons
Imagelist1.draw (Lsbright.canvas, Rect.left +7,
Rect.top + (Lsbright.itemheight-imagelist1.height)Div2, Index, True);
//Draw three lines of text, respectively
strtemp: = Lsbright.items.strings[index];
LsbRight.Canvas.TextOut (Rect.left + ++Ten, Rect.top +4
, Copy (strtemp, 1, Pos (#13, strtemp)-1));
strtemp: = Copy (strtemp, Pos (#13, strtemp) +1, Length (strtemp));
LsbRight.Canvas.TextOut (Rect.left + ++Ten, Rect.top + -,
Copy (strtemp,1Pos(#13, strtemp)-1));
LsbRight.Canvas.TextOut (Rect.left + ++Ten, Rect.top + +,
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
procedureTform1.lsbleftdrawitem (Control:twincontrol; Index:integer;
Rect:trect; State:townerdrawstate);
var
R:trect;
begin
withLsbleft.canvas Dobegin
//Set the background color of the fill and fill the background
Brush.color: = Clwhite;
FillRect (Rect);
//Draw rounded rectangles
if(odselectedinchState) Then//The rounded rectangle color of the selected item
Pen.color: = $FFB 2b5
Else//Round rectangle color for unselected items
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,Ten,Ten);
//Draw out icons
if(odselectedinchState) Then//The image of the selected item
Draw (R.left + (r.right-r.left-imghouse.width)SHR1,
R.top +2, imgHouse.Picture.Graphic)
Else//image of unselected item
Draw (R.left + (r.right-r.left-imghousegray.width)SHR1,
R.top +2, imgHouseGray.Picture.Graphic);
//Fill text area background
R.top: = R.bottom-abs (font.height)-4;
Brush.style: = Bssolid;
if(odselectedinchState) Then //The background color of the selected item
Brush.color: = $FFB 2b5
Else//Background color of unselected items
Brush.color: = Clsilver;
FillRect (R);
//Output text, support only single line
Font.Color: = Clblack;
R.top: = R.top +2; //Calculate text vertex position (horizontal center, dt_center not available)
DrawText (Handle, PChar (Tlistbox (Control). Items.strings[index]),-1R
, Dt_centerorDt_end_ellipsis{or dt_wordbreak});
//Draw the focus virtual box, when the system is re-drawn, become an XOR operation, so as to achieve the purpose of erasing the focus virtual box
if(odfocusedinchState) ThenDrawFocusRect (Rect);
end;
End;
End.
http://blog.csdn.net/nhconch/article/details/205127
Two effects of self-drawn listbox