http://www.delphitop.com/html/kongjian/893.html
本例效果图:
提示: 新建工程后, 添加 TColorBox、TColorListBox、TColorGrid、TButtonColor 四个控件, 分别双击它们建立默认事件, 然后贴入下面代码:
--------------------------------------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ColorGrd, StdCtrls, TeCanvas, ExtCtrls;
type
TForm1 = class(TForm)
ColorBox1: TColorBox;
ColorListBox1: TColorListBox;
ColorGrid1: TColorGrid;
ButtonColor1: TButtonColor;
procedure ColorListBox1Click(Sender: TObject);
procedure ColorBox1Change(Sender: TObject);
procedure ColorGrid1Change(Sender: TObject);
procedure ButtonColor1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ColorListBox1Click(Sender: TObject);
begin
Self.Color := ColorListBox1.Selected;
end;
procedure TForm1.ColorBox1Change(Sender: TObject);
begin
Self.Color := ColorBox1.Selected;
end;
procedure TForm1.ColorGrid1Change(Sender: TObject);
begin
Self.Color := ColorGrid1.ForegroundColor;
end;
procedure TForm1.ButtonColor1Click(Sender: TObject);
begin
Self.Color := ButtonColor1.SymbolColor;
end;
end.
原文:http://blog.163.com/net_cwp/blog/static/182940035201213112941449/
在窗体中加入一个ColorDialog对象和一个Name属性为ChangeColor的按钮。为按钮的OnClick事件建立事件处理过程如下:
procedure TForm1.ChangeColorClick(Sender: TObject);
begin
if ColorDialog1.Execute then
Form1.Color := ColorDialog1.Color
else
Form1.Color := clRed;
end;
此事件处理过程使用一个返回Boolean值的Execute方法。按动按钮,并在颜色对话框中选择一个颜色。如果按动OK按钮,ColorDialog.Execute方法将返回True,则Form1.Color将被赋值为ColorDialog1.Color,窗体显现您选用的颜色;如果按动颜色对话框的Cancel按钮,方法将返回False值,窗体将变为红色。