如何写一个自己的Button能够改变字体颜色(200分)

  • 主题发起人 主题发起人 zsy_good
  • 开始时间 开始时间
Z

zsy_good

Unregistered / Unconfirmed
GUEST, unregistred user!
谢谢了,给段代码
 
playicq.com上的:

unit ColorButton;

interface

uses
SysUtils, Classes, Controls, StdCtrls,Graphics,Messages,windows;

type
TClrButton = class(TButton)
private
{ Private declarations }
IsFocused : Boolean;
FCanvas : TCanvas;
procedure CNDrawItem(var Msg : TWMDrawItem);message CN_DRAWITEM;
procedure CMFontChanged(var Msg : TMessage);message CM_FONTCHANGED;
procedure CMEnabledChanged(var Msg : TMessage);message CM_ENABLEDCHANGED;
procedure WMLButtonDblClk(var Msg : TWMLButtonDblClk);message WM_LBUTTONDBLCLK;
protected
{ Protected declarations }
procedure SetBounds(ALeft,ATop,AWidth,AHeight : integer);override;
procedure CreateParams(var Params : TCreateParams);override;
procedure CreateWnd;override;
procedure SetButtonStyle(ADefault : Boolean);override;
public
{ Public declarations }
constructor Create(AOwner : TComponent);override;
destructor Destroy;override;
published
{ Published declarations }
property Color;
property Width default 75;
property Height default 25;
property ParentshowHint;
property ShowHint;
property TabOrder;
property Visible;
property OnEnter;
property OnExit;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TClrButton]);
end;
constructor TClrButton.Create(AOwner : TComponent);
begin
inherited Create(AOwner);
SetBounds(Left,Top,75,25);
FCanvas := TCanvas.Create;
end;
destructor TClrButton.Destroy;
begin
inherited Destroy;
FCanvas.Free;
end;
procedure TClrButton.CreateParams(var Params : TCreateParams);
begin
inherited CreateParams(Params);
with Params do
Style := Style or bs_OwnerDraw;
end;
procedure TClrButton.CreateWnd;
var
hRegin : THandle;
begin
inherited CreateWnd;
hRegin := CreateRectRgn(0,0,Width,Height);
SetWindowRgn(Handle,hRegin,true);
end;
procedure TClrButton.SetBounds(ALeft,ATop,AWidth,AHeight : integer);
var
hRegin : THandle;
begin
inherited SetBounds(ALeft,ATop,AWidth,AHeight);
if HandleAllocated then
begin
hRegin := CreateRectRgn(0,0,Width,Height);
SetWindowRgn(Handle,hRegin,true);
end;
end;
procedure TClrButton.CNDrawItem(var Msg : TWMDrawItem);
var
OdsDown,OdsFocus,ActionFocus : Boolean;
Rect : TRect;
begin
FCanvas.Handle := Msg.DrawItemStruct^.hDC;
Rect := ClientRect;
Dec(Rect.Right);
Dec(Rect.Bottom);
with Msg.DrawItemStruct^ do
begin
OdsDown := ItemState and ODS_SELECTED <> 0;
OdsFocus := ItemState and ODS_FOCUS <> 0;
ActionFocus := ItemAction = ODA_FOCUS;
end;
with FCanvas do
begin
Brush.Color := Color;
if not ActionFocus then
begin
Brush.Style := bsSolid;
FillRect(Rect);
end
end;
if OdsDown then
begin
FCanvas.Pen.Color := clBtnshadow;
if not ActionFocus then
FCanvas.Rectangle(Rect.Left,Rect.top,Rect.Right,Rect.Bottom);
end
else if not ActionFocus then
begin
FCanvas.Pen.color := clWindowFrame;
FCanvas.Rectangle(Rect.Left,Rect.top,Rect.Right,Rect.Bottom);
FCanvas.Pen.Color := clWhite;
FCanvas.MoveTo(Rect.Left,Rect.Bottom-1);
FCanvas.LineTo(Rect.Left,Rect.Top);
FCanvas.LineTo(Rect.Right-1,Rect.Top);
FCanvas.Pen.Color := clBtnShadow;
InflateRect(Rect,-1,-1);
FCanvas.MoveTo(Rect.Right,Rect.Top);
FCanvas.LineTo(Rect.Right,Rect.Bottom);
FCanvas.LineTo(Rect.Left,Rect.Bottom);
end;
InflateRect(Rect,-2,-2);
if OdsDown then
begin
inc(Rect.Left);
inc(Rect.Top);
end;
font := Self.Font;
if not ActionFocus then
DrawText(FCanvas.Handle,PChar(Caption),-1,Rect,dt_SingleLine or dt_Center or dt_VCenter);
FCanvas.Brush.Style := bssolid;
FCanvas.Pen.color := clBlack;
FCanvas.Brush.color := clWhite;
if IsFocused or OdsFocus or ActionFocus then
FCanvas.DrawFocusRect(Rect);
end;
procedure TClrButton.CMFontChanged(var Msg : TMessage);
begin
inherited;
invalidate;
end;
procedure TClrButton.CMEnabledChanged(var Msg : TMessage);
begin
inherited;
invalidate;
end;
procedure TClrButton.WMLButtonDblClk(var Msg : TWMLButtonDblClk);
begin
Perform(WM_LBUTTONDOWN,Msg.Keys,LongInt(Msg.Pos));
end;
procedure TClrButton.SetButtonStyle(ADefault : Boolean);
begin
if ADefault<>IsFocused then
begin
IsFocused := ADefault;
invalidate;
end;
end;
end.
 
检索“Button 颜色”
 
这个在 delphi 中的处理比较勉强。
 
建议加button控件,很简单!
自己去google.com找找!!
 
如果仅仅是要改变字体的颜色,为什么不用TBitbtn呢,难道它有什么问题呢?
 
这个问题大这里已经提过很多次了,如果要修改按钮的颜色,那重载TButton的CN_DRAWITEM消息自己绘制控件就可以做到.
TBitbtn在很大程序上也主要是重载了TButton的CN_DRAWITEM消息而已,如果重载这个消息,说加字体颜色,按钮颜色,就
是你想在按钮上加一张图片或者加双态图片都可以.如果有什么问题可以到我的主页
http://www.aidelphi.com
或者发邮件到
aizb@163.com.
 
Color可以设置,在设计时看不出来,运行时是可以的,但是得单击以后
作如下改正后就可以在设计时也可见了
with FCanvas do
begin
Font := Self.Font
Brush.Color := Color;
if not ActionFocus then
begin
Brush.Style := bsSolid;
FillRect(Rect);
end
end;
后面还有一句Font := Self.Font要删除,有其他问题和我联系 QQ:123732915
 
后退
顶部