科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件用Delphi实现自定义颜色对话框及其构件

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

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

 在开发证券分析软件中,经常要绘制各种股票的分析曲线。为了使得 软件的功能更加方便.灵活,用户希望能够按照自己的喜好自定义各种曲线的颜色……

作者:胡晓鹰 陈谦 来源:popcool开发专区 2007年11月2日

关键字: Delphi 颜色 对话框 构件

  • 评论
  • 分享微博
  • 分享邮件
 ≡诳⒅と治鋈砑校R嬷聘髦止善钡姆治銮摺N耸沟萌砑墓δ芨臃奖?.灵活,用户希望能够按照自己的喜好自定义各种曲线的颜色。在WORD97的[格式]菜单下的字体对话框中有类似的功能。当用户单击字体对话框中的颜色下拉框时,各种颜色的简单图案和字体的颜色名称一起显示出来,这样处理的结果显然比只提供一个装有颜色名称 的下拉框效果要好的多。

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

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

property OnDrawItem: TDrawItemEvent;
TDrawItemEvent = procedure
(Control: TWinControl; Index: Integer Rect:
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属性旁的圆点按纽,在"String ListEditor"对话框中输入黑色、蓝色、蓝绿、鲜绿、红色、黄色等各种颜 色的名称

  4.在ColorCombo1的OnDrawItem事件中加入如下代码


procedure T m1.ColorComboDrawItem
(Control: TWinControl; Index: Integer;
Rect: TRect; State: OwnerDrawState);
var
TempColor :TColor; // 自 画 颜 色
TempBrushColor :TColor; // 临 时 颜 色
begin
with (Control as TComboBox) 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) div 3,
Rect.Bottom-1);
Canvas.Brush.Color:=TempBrushColor;
// 显示与颜色对应的字符串
Canvas.TextOut((Rect.Left+Rect.Right) div 2,
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选项,点击Intonew package,在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
{ Private declarations }
FOnDrawItem : TDrawItemEvent;
procedure DrawItem(Index: Integer; Rect: TRect;
State: TOwnerDrawState);override;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(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;

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

procedure TColorComboBox.CreateWnd;
// 颜 色 构 件 的Items 属 性 初 始 化
begin
inherited CreateWnd;
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
else if Items[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) div 3,
Rect.Bottom-1);
Canvas.Brush.Color:=TempBrushColor;
// 显示与颜色对应的字符串
Canvas.TextOut((Rect.Left+Rect.Right) div 2,
Rect.Top+1,
Items[Index]);
end;
end.
  此控件可以在所有需要颜色选项的程序中使用而且非常方便和美观,并且使编程节省很多时间,增加了程序可靠性和可读性。

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

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

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章