程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 自繪ListBox的兩種效果

自繪ListBox的兩種效果

編輯:Delphi
本文利用Listbox自繪實現了兩種特殊效果,其中第兩種風格來自C++ Builder 研究 www.ccrun.com,老妖用BCB實現了,現在把它轉換成Delphi代碼。
  
  演示圖片:
  
  
  //--------------------------------------------------------------------------
  
  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:       [email protected]

    文章為作者原創,轉載前請先與本人聯系,轉載請注明文章出處、保留作者信息,謝謝支持!
  =========================================================================}
  

  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;

  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved