网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Delphi
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.实现QQ窗体的缩入伸出功能.
.使TStringGrid自适应宽度.
.用Delphi产生一个最小的可执行程.
.在Delphi中开发使用多显示器的应.
.解决无法按y轴显示的问题!.
.systemparametersinfo详细用法.
.GSM规范中的部分编码转换.
.为应用软件加入全面的功能权限控.
.提取应用程序中的图标资源.
.在网络上进行摄像头视频通讯.
.以最少的资源耗用,显示系统键状.
.NeHe的opengl教程delphi版(6)---.
.DELPHI中的快捷方式一览(完全正式.
.发现:不要Excel也可生成xls文件.
.生成GBK码表.
.Delphi下常用转换(一).
.Delphi中两个BUG的分析与修复.
.《参透delphi》书评.
.基本图象处理代码(2).
.利用剪贴板实现高速导出数据到Ex.

自绘ListBox的两种效果

发表日期:2006-2-4


本文利用Listbox自绘实现了两种特殊效果,其中第两种风格来自C++ Builder 研究 www.ccrun.com,老妖用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

  文章为作者原创,转载前请先与本人联系,转载请注明文章出处、保留作者信息,谢谢支持!
=========================================================================}

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.

上一篇:Delphi程序设计综合训练任务书 人气:4247
下一篇:delphi的属性property和消息处理特点事件属性 人气:4760
浏览全部Delphi的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐