自绘ListBox的两种效果

时间:2022-12-26 03:26:15

 本文利用Listbox自绘实现了两种特殊效果(见图),左边的风格是自己突然灵感触发想到的,右边的风格来自"C++ Builder 研究"的一个帖子,老妖用BCB实现了,这里则用Delphi实现它。

演示图片:
自绘ListBox的两种效果

//--------------------------------------------------------------------------

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

;
    lsbRight.Items.Add(' to  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 := ;
    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
, Rect.Top + ,
            Rect.Right - , Rect.Bottom - , , );
, Rect.Top + ,
            Rect.Right - , Rect.Bottom - , , );
, Rect.Top + ,
                Rect.Right - , Rect.Bottom - , , );
,
            Rect.top + (lsbRight.ItemHeight - ImageList1.Height) div , Index, true);
 + , Rect.Top + , Length(strTemp));
    lsbRight.Canvas.TextOut(Rect.Left +  + , Rect.Top + ,
                            Copy(strTemp, , Pos));
    lsbRight.Canvas.TextOut(Rect.Left +  + , Rect.Top + ,
                            Copy(strTemp, Pos, 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 := ;
    lsbRight.Repaint();

lsbLeft.ItemIndex := ;
    lsbLeft.Repaint();
end;

procedure TForm1.lsbLeftDrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
    r: TRect;
begin
    with lsbLeft.Canvas do begin
, Rect.Top+, Rect.Right-, Rect.Bottom-);
        RoundRect(r.Left, r.Top, r.Right, r.Bottom, , );
,
                r.Top + , imgHouse.Picture.Graphic)
        else                            ,
                r.Top + , imgHouseGray.Picture.Graphic);
;
        Brush.Style := bsSolid;
        if (odSelected in State) then   ; //计算文字顶点位置,(水平居中,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.

http://blog.csdn.net/nhconch/article/details/205127