用Delphi实现自定义颜色对话框及其构件

一、自定义颜色对话框的实现

----在Delphi中,我们可以使用TComboBox实现类似的功能。在TcomboBox构件中有一个Style属性,决定TcomboBox的显示属性。通常可选取csDropDown,csSimple,csDropDownList,csOwnerDrawFixed,csOwnerDrawVariable等。其中当选取csOwnerDrawFixed时表示创建一个自画下拉框,下拉框的每一项的高度由ItemHeight属性决定。并且必须在TcomboBox的OnDrawItem事件中响应自画过程。OnDrawItem的定义为:

property OnDrawItem:TDrawItemEvent;
TDrawItemEvent=procedure(Control:TWinControl;Index:IntegerRect:TRect;State:TOwnerDrawState) of object;
    其中的三个参数的含义为:
        Control: 包含下拉框的TComboBox
        Index:自画的下拉框在
TComboBox的Items属性中的索引号
    Rect:自画的位置
----因此,知道了需要自画的矩形的位置(Rect参数)和在TComboBox中的索引号(Index参数),我们可以使用TcomboBox的Canvas属性在其画布上自画。

----具体的实现过程如下:
----1.新建一个工程文件,设置其默认窗体的有关属性为:
----Caption自定义下拉框
----NameForm1
----PositionpoScreenCenter
----2.在窗体中放置两个TcomboBox构件,设置其属性如下:
----Name Style ItemHeight OnDrawItem
----ColorCombo1 csOwnerDrawFixed 20 ColorComboDrawItem
----ColorCombo2 csOwnerDrawFixed 30 ColorComboDrawItem
----3.双击ColorCombo1和ColorCombo2的Items属性旁的圆点按纽,在"StringListEditor"对话框中输入
----黑色
----蓝色
----蓝绿
----鲜绿
----红色
----黄色
----等各种颜色的名称
----4.在ColorCombo1的OnDrawItem事件中加入如下代码
procedure TForm1.ColorComboDrawItem(Control:TWinControl;Index:Integer;Rect:TRect;State:OwnerDrawState);
var
    TempColor    :TColor;//自画颜色
    TempBrushColor:TColor;//临时颜色
begin
    with (ControlasTComboBox) do
//在Combo的Canvas上自画
    begin
    TempBrushColor:=Canvas.Brush.Color;
//保存原来的的颜色
    Canvas.FillRect(Rect);
    case Index of//根据Index的不同,定义不同自画的颜色
0://黑色
TempColor:=clBlack;
1://蓝色
TempColor:=clBlue;
2://蓝绿
TempColor:=clAqua;
3://鲜绿
TempColor:=clLime;
4://红色
TempColor:=clRed;
5://黄色
TempColor:=clyellow;
//可以在此加入对其它颜色的响应
end;

Canvas.Brush.Color:=TempColor;
//自画颜色矩形
Canvas.Rectangle(Rect.Left+4,Rect.Top+1,(Rect.Right+Rect.Left)div3,Rect.Bottom-1);
Canvas.Brush.Color:=TempBrushColor;
//显示与颜色对应的字符串
Canvas.TextOut((Rect.Left+Rect.Right)div2,Rect.Top+1,Items[Index]);
end;
end;
----5.保存,运行文件,我们可以看到和WORD中颜色下拉框相同的效果
----有兴趣的读者,可以在文中所示的位置加入对其它颜色处理。
----以上程序在Delphi3.0,4.0上通过。
----二、自定义颜色对话框构件的编写
----对许多Delphi程序员来说,如何编写自己的Delphi构件还是比较陌生的,Delphi构件实际上是从Tcomponent类继承发展而来,编写构件实际就是编写特殊的类。下面我们就以自定义颜色对话框为例介绍构件的编写。

----下面TColorComboBox是从TcomboBox类继承来的,当点击右边的下拉箭头时弹出和下拉items对应的各种颜色自画框。

----1.选中Component菜单项中的NewComponent选项。在AncestorType框中选TcomboBox,在ClassName框中填入TColorComboBox,在PalettePage框中选Samples,在UnitFileName框中填入ColorComboBox.pas,然后点击OK按钮。

----2.选中Component菜单项中的InstallComponent选项,点击Intonewpackage,在packagename框中写入路径和ColorComboDpk.dpk,点击ok,生成ColorComboDpk.bpl文件。

----3.使用Tools菜单中的ImageEditor来创建编辑文件ColorComBox.dcr,为TColorComboBox类建立位图。

----4.在Create中加入对字体大小高度的规定及对控件的Style属性(设成csOwnerDrawFixed)的规定,在Create后执行的CreateWnd中初始化颜色的items,如果不需要那么多颜色项,可以以后在生成控件的items属性中直接删除不需要的颜色。

----5.在DrawItem事件中加入颜色自画程序,此事件在OnDrawItem之前发生。

----实现程序如下:

unit ColorComboBox;
interface
uses
Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs,
StdCtrls;
type
TColorComboBox=class(TComboBox)
private
{Privatede clarations}
FOnDrawItem:TDrawItemEvent;
procedure DrawItem(Index:Integer;Rect:TRect;State:TOwnerDrawState);override;
protected
{Protected declarations}
public
{Publicde clarations}
constructorCreate(AOwner:TComponent);override;
procedure CreateWnd;override;
published
{Published declarations}
property OnDrawItem:TDrawItemEvent Read FOnDrawItem write FOnDrawItem;
end;
procedure Register;

implementation

procedure Register;//注册构件
begin
RegisterComponents('Samples',[TColorComboBox]);
end;

constructorTColorComboBox.Create
(AOwner:TComponent);//构件的初始化
begin
inheritedCreate(AOwner);
Style:=csOwnerDrawFixed;//构件的初始类型
ItemHeight:=20;
Font.Size:=10;
end;

procedure TColorComboBox.CreateWnd;
//颜色构件的Items属性初始化
begin
inheritedCreateWnd;
Items.Clear;
Items.Add('黑色');
Items.Add('蓝色');
Items.Add('蓝绿');
Items.Add('鲜绿');
Items.Add('粉红');
Items.Add('红色');
Items.Add('黄色');
Items.Add('白色');
Items.Add('深蓝');
Items.Add('青色');
Items.Add('绿色');
Items.Add('紫色');
Items.Add('深红');
Items.Add('深黄');
Items.Add('深灰');
Items.Add('银色');
----//若不需要这么多颜色可在构件的items属性中删除不需要的颜色

----end;

----//重载DrawItem过程

procedure TColorComboBox.DrawItem(Index:Integer;Rect:TRect;State:TOwnerDrawState);
var
    TempColor    :TColor;//自画颜色
    TempBrushColor:TColor;//临时颜色
begin//本构件的默认自画设置
    TempBrushColor:=Canvas.Brush.Color;
//保存原来的的颜色
    Canvas.FillRect(Rect);

if Items[index]='黑色' then
TempColor:=clBlack
else if Items[index]='蓝色' then
TempColor:=clBlue
else if Items[index]='蓝绿' then
TempColor:=clAqua
else if Items[index]='鲜绿' then
TempColor:=clLime
else if Items[index]='粉红' then
TempColor:=clFuchsia
else if Items[index]='红色' then
TempColor:=clRed
else if Items[index]='黄色' then
TempColor:=clYellow
else if Items[index]='白色' then
TempColor:=clWhite
else if Items[index]='深蓝' then
TempColor:=clNavy
else if Items[index]='青色' then
TempColor:=clTeal
elseifItems[index]='绿色'then
TempColor:=clGreen
else if Items[index]='紫色' then
TempColor:=clPurple
else if Items[index]='深红' then
TempColor:=clMaroon
else if Items[index]='深黄' then
TempColor:=clOlive
else if Items[index]='深灰' then
TempColor:=clGray
else if Items[index]='银色' then
else TempColor:=clSilver;

    Canvas.Brush.Color:=TempColor;
    //自画颜色矩形
Canvas.Rectangle(Rect.Left+4,Rect.Top+1,(Rect.Right+Rect.Left)div3,Rect.Bottom-1);
Canvas.Brush.Color:=TempBrushColor;
//显示与颜色对应的字符串
Canvas.TextOut((Rect.Left+Rect.Right)div2,Rect.Top+1,Items[Index]);
end;
end.
----此控件可以在所有需要颜色选项的程序中使用而且非常方便和美观,并且使编程节省很多时间,增加了程序可靠性和可读性。

----三、自定义颜色对话框构件的使用

----当注册完自定义颜色构件后,可以从Delphi构件模板的Sample页中选择自定义颜色构件,和使用Delphi本身构件没有区别。

转载于:https://www.cnblogs.com/myamanda/articles/1527918.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值