[英]Custom Button Component : caption is displaying different color in Delphi7
i am working with Delphi 7 Below is my code for sample button component. 我正在使用Delphi 7,以下是我的示例按钮组件代码。 When i place button component on form at design time it is displaying different color in Delphi7 as attached in screen shot.
当我在设计时将按钮组件放置在窗体上时,它在Delphi7中显示为不同的颜色,如屏幕截图所示。
in Delphi 5 same is Working fine. 在Delphi 5中同样可以正常工作。 i mean it is displaying caption as black color.
我的意思是将字幕显示为黑色。
unit TestButton1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Math;
type
TButtonShape = (bsRectangle, bsOval, bsTriangle);
type
TTestButton = class(TCustomControl)
private
{ Private declarations }
FCaption: TCaption;
FButtonDown: boolean;
FBorderStyle: TBorderStyle;
FBtnHighLight: TColor;
FBtnShadow: TColor;
FBtnFace: TColor;
procedure DrawButton;
procedure DrawButtonDown;
procedure DrawCaption(rc: TRect);
procedure DrawButtonUp;
procedure SetCaption(Value: TCaption);
procedure SetButtonColor(Value: TColor);
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure WndProc(var Msg: TMessage); override;
published
{ Published declarations }
property Caption: TCaption read FCaption write SetCaption;
property Color: TColor read FBtnFace write SetButtonColor;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TTestButton]);
end;
{ TTestButton }
constructor TTestButton.Create(AOwner: TComponent);
begin
inherited;
if AOwner is TWinControl then Parent := TWinControl(AOwner);
FCaption := Name;
FButtonDown := FALSE;
FBorderStyle := bsNone;
ControlStyle := ControlStyle - [csDoubleClicks];
FBtnHighLight := clBtnHighLight;
FBtnShadow := clBtnShadow;
FBtnFace := clBtnFace;
Width := 75;
Height := 25;
end;
destructor TTestButton.Destroy;
begin
inherited;
end;
procedure TTestButton.DrawButton;
var rc: TRect;
begin
with rc do
Begin
Top := 0;
Left := 0;
Bottom := Height;
Right := Width;
end;
with Canvas do
Begin
if FBorderStyle = bsSingle then
Begin
Brush.Color := clBlack;
Framerect(rc);
end;
end;
if FButtonDown then DrawButtonDown
else DrawButtonUp;
end;
procedure TTestButton.DrawButtonDown;
var rc: TRect;
Cnv: TCanvas;
begin
with rc do
Begin
Top := 0;
Left := 0;
Bottom := Height - 1;
Right := Width - 1;
end;
Cnv := TCanvas.Create;
Cnv.Handle := CreateCompatibleDC(Canvas.Handle);
SelectObject(Cnv.Handle, CreateCompatibleBitmap(Canvas.Handle, Width, Height));
with Canvas do
Begin
Brush.Color := FBtnFace;
FillRect(rc);
Pen.Color := clBlack;
MoveTo(rc.Left, rc.Bottom - 1);
LineTo(rc.Left, rc.Top);
LineTo(rc.Right, rc.Top);
Pen.Color := FBtnShadow;
MoveTo(rc.Left + 1, rc.Bottom - 2);
LineTo(rc.Left + 1, rc.Top + 1);
LineTo(rc.Right - 1, rc.Top + 1);
Pen.Color := FBtnHighlight;
MoveTo(rc.Left, rc.Bottom);
LineTo(rc.Right, rc.Bottom);
Lineto(rc.Right, rc.Top - 1);
rc.Top := rc.Top + 1;
rc.Left := rc.Left + 1;
end;
rc.Top := rc.Top + 1;
rc.Left := rc.Left + 1;
if FCaption > '' then DrawCaption(rc);
end;
procedure TTestButton.DrawButtonUp;
var rc: TRect;
begin
with rc do
Begin
Top := 0;
Left := 0;
Bottom := Height - 1;
Right := Width - 1;
end;
with Canvas do
Begin
Brush.Color := FBtnFace;
FillRect(rc);
Pen.Color := FBtnHighlight;
MoveTo(rc.Left, rc.Bottom - 1);
LineTo(rc.Left, rc.Top);
LineTo(rc.Right, rc.Top);
Pen.Color := FBtnShadow;
MoveTo(rc.Left + 1, rc.Bottom - 1);
LineTo(rc.Right - 1, rc.Bottom - 1);
LineTo(rc.Right - 1, rc.Top);
Pen.Color := clBlack;
MoveTo(rc.Left, rc.Bottom);
LineTo(rc.Right, rc.Bottom);
Lineto(rc.Right, rc.Top - 1);
end;
if FCaption > '' then DrawCaption(rc);
end;
procedure TTestButton.DrawCaption(rc: TRect);
begin
Canvas.Brush.Style := bsClear;
SetTextColor(Canvas.Handle, Canvas.Font.Color);
DrawText(Canvas.Handle, PChar(FCaption), Length(FCaption), rc, DT_CENTER + DT_VCENTER + DT_SINGLELINE);
end;
procedure TTestButton.SetButtonColor(Value: TColor);
var OldValue: TColor;
begin
OldValue := FBtnFace;
FBtnFace := Value;
if (OldValue <> Value) then Repaint;
end;
procedure TTestButton.SetCaption(Value: TCaption);
begin
FCaption := Value;
Repaint;
end;
procedure TTestButton.WndProc(var Msg: TMessage);
begin
inherited;
with Msg do if Msg = WM_PAINT then
Begin
DrawButton;
Result := 0;
Exit;
end
else if Msg = WM_LBUTTONDOWN then
Begin
if not (csDesigning in ComponentState) then FButtonDown := TRUE;
Repaint;
Result := 0;
Exit;
end
else if Msg = WM_LBUTTONUP then
Begin
FButtonDown := FALSE;
Repaint;
Result := 0;
Exit;
end
end;
end.
at design time, Button caption is displaying in black color with delphi5 and with delphi7 button caption is displaying in different color. 在设计时,按钮标题显示为带有delphi5的黑色,而delphi7按钮标题显示为不同的颜色。 Why it is working with delphi5 and why it is not working with Delphi7.
为什么它可以与delphi5一起使用以及为什么不能与Delphi7一起使用。
SetTextColor(Canvas.Handle, Canvas.Font.Color);
in DrawCaption
sets font color to same value it already has. 在
DrawCaption
字体颜色设置为已经具有的相同值。
Different Delphi versions can set different initial values therefore you see the difference in behavior. 不同的Delphi版本可以设置不同的初始值,因此您会看到行为上的差异。
Use SetTextColor(Canvas.Handle, clBlack);
使用
SetTextColor(Canvas.Handle, clBlack);
instead (if you want black text color) 相反(如果您想要黑色文本颜色)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.