在此本人提供一种用自定义组件制作有颜色属性的铵钮的方法,
第一步
打开Delphi,选择菜单的Component/New Component选项,在弹出对话框的Ancestor type下拉框中手工填入或下拉选择TButton,
第二步
做完以上工作后,按下面的OK按钮,
第三步
在上面的代码框架中添加我们的代码,
1. 将Delphi自动生成的单元文件的数据类型定义部份修改为:
type
TButton1 = class(TButton)
private
FColor:TColor;
FCanvas: TCanvas;
IsFocused: Boolean;
procedure SetColor(Value:TColor);
procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM;
protected
procedure CreateParams(var Params: TCreateParams); override;
procedure SetButtonStyle(ADefault: Boolean); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Color:TColor read FColor write SetColor default clWhite;
end;
说明:
a. 我们只添加了一个属性,
b. 重载构造函数和析构函数,二者应为可以在外部调用,
c. 读属性的私有数据域FColor和属性的写方法SetColor
2. Delphi自动生成的 procedure Register可以不理它。我们在它的过程体之后,在end.
//*** 构造函数 ******************************
constructor TButton1.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FCanvas := TCanvas.Create;
FColor:=clWhite;//默认颜色
end;
//*** 析构函数 ******************************
destructor TButton1.Destroy;
begin
FCanvas.Free;
inherited Destroy;
end;
//*** 定义按钮样式,必须将该按钮重定义为自绘式按钮 *************
procedure TButton1.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
with Params do Style := Style or BS_OWNERDRAW;
end;
//*** 属性写方法 ******************************
procedure TButton1.SetColor(Value:
begin
FColor:=Value;
Invalidate;
end;
//*** 设置按钮状态************************
procedure TButton1.SetButtonStyle(
begin
if ADefault <> IsFocused then
begin
IsFocused := ADefault;
Refresh;
end;
end;
//*** 绘制按钮 ******************************
procedure TButton1.CNDrawItem(var Message: TWMDrawItem);
var
IsDown, IsDefault: Boolean;
ARect: TRect;
Flags: Longint;
DrawItemStruct: TDrawItemStruct;
wh:TSize;
begin
DrawItemStruct:=Message.
FCanvas.Handle := DrawItemStruct.hDC;
ARect := ClientRect;
with DrawItemStruct do
begin
IsDown := itemState and ODS_SELECTED <> 0;
IsDefault := itemState and ODS_FOCUS <> 0;
end;
Flags := DFCS_BUTTONPUSH or DFCS_ADJUSTRECT;
if IsDown then Flags := Flags or DFCS_PUSHED;
if DrawItemStruct.itemState and ODS_DISABLED <> 0 then
Flags := Flags or DFCS_INACTIVE;
if IsFocused or IsDefault then
begin
//按钮得到焦点时的状态绘制
FCanvas.Pen.Color := clWindowFrame;
FCanvas.Pen.Width := 1;
FCanvas.Brush.Style := bsClear;
FCanvas.Rectangle(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom);
InflateRect(ARect, -1, -1);
end;
FCanvas.Pen.Color := clBtnShadow;
FCanvas.Pen.Width := 1;
FCanvas.Brush.Color := FColor;
if IsDown then begin
//按钮被按下时的状态绘制
FCanvas.Rectangle(ARect.Left , ARect.Top, ARect.Right, ARect.Bottom);
InflateRect(ARect, -1, -1);
end else
//绘制一个未按下的按钮
DrawFrameControl(
FCanvas.FillRect(ARect);
//绘制Caption文本内容
FCanvas.Font := Self.Font;
ARect:=ClientRect;
wh:=FCanvas.TextExtent(
FCanvas.Pen.Width := 1;
FCanvas.Brush.Style := bsClear;
if not Enabled then
begin //按钮失效时应多绘一次Caption文本
FCanvas.Font.Color := clBtnHighlight;
FCanvas.TextOut((Width div 2)-(wh.cx div 2)+1,
(height div 2)-(wh.cy div 2)+1,
Caption);
FCanvas.Font.Color := clBtnShadow;
end;
FCanvas.TextOut((Width div 2)-(wh.cx div 2),(height div 2)-(wh.cy div 2),Caption);
//绘制得到焦点时的内框虚线
if IsFocused and IsDefault then
begin
ARect := ClientRect;
InflateRect(ARect, -4, -4);
FCanvas.Pen.Color := clWindowFrame;
FCanvas.Brush.Color := FColor;
DrawFocusRect(FCanvas.Handle, ARect);
end;
FCanvas.Handle := 0;
end;
//** The End ******************************
end.
第四步:
如果你完全按本文步聚进行,在编译安装无误后,
最后说明: