J
j5203
Unregistered / Unconfirmed
GUEST, unregistred user!
我写了一个改变Hint的控件,但是Hint中字的颜色却始终是初始值,无法改变,请问大家这是为什么?
以下是我写的代码。
unit coolhint;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TMyHintWindow = class(THintWindow)
private
FRegion: THandle;
FColor: TColor; //Hint中字体的颜色
procedure FreeCurrentRegion;
{ Private declarations }
public
procedure ActivateHint(Rect: TRect; const AHint: string);override;
procedure Paint;override;
procedure Createparams(var Params: TCreateParams);override;
constructor Create(AOwner: TComponent);override ;
destructor Destroy; override;
{ Public declarations }
published
end;
type
Tp = class(Tcomponent)
private
FMyHintWindow: TMyHintWindow;
FEnabled: Boolean;
procedure ChangHint(Enabled: Boolean);
procedure SetEnabled(const Value: Boolean);
function GetColor: TColor;
procedure SetColor(const Value: TColor);//改变Hint字体的颜色
protected
procedure Loaded;override;
public
constructor Create(AOwner: TComponent);override ;
destructor Destroy; override;
published
property Enabled: Boolean read FEnabled write SetEnabled;
property Color: TColor read GetColor write SetColor;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Ke', [Tp]);
end;
{ TMyHintWindow }
procedure TMyHintWindow.ActivateHint(Rect: TRect; const AHint: string);
begin
with Rect do
Right := Right + Canvas.TextWidth('wwww');
BoundsRect := Rect;
FreeCurrentRegion;
with BoundsRect do
Fregion := CreateRoundRectRgn(0,0,width,height,width,height);
if FRegion <> 0 then
SetWindowRgn(Handle, FRegion, True);
inherited;
end;
constructor TMyHintWindow.Create(AOwner: TComponent);
begin
inherited;
FColor := clRed;
end;
procedure TMyHintWindow.createparams(var Params: TCreateParams);
begin
inherited;
Params.Style := Params.Style and not ws_Border;
end;
destructor TMyHintWindow.Destroy;
begin
FreeCurrentRegion;
inherited;
end;
procedure TMyHintWindow.FreeCurrentRegion;
begin
if FRegion <> 0 then
begin
SetWindowRgn(Handle, 0, True);
DeleteObject(FRegion);
FRegion := 0;
end;
end;
procedure TMyHintWindow.paint;
var
R: TRect;
begin
r := ClientRect;
Inc(R.left,1);
Canvas.Font.Color:= FColor;
DrawText(Canvas.Handle,Pchar(Caption),Length(Caption),R,
DT_NOPREFIX OR DT_WORDBREAK OR DT_CENTER OR DT_VCENTER);
end;
{ Tp }
destructor Tp.Destroy;
begin
FMyHintWindow.Free;
inherited;
end;
procedure Tp.SetEnabled(const Value: Boolean);
begin
if Value<>FEnabled then
begin
FEnabled := Value;
changHint(FEnabled);
end;
end;
procedure Tp.Loaded;
begin
inherited;
changHint(FEnabled);
end;
procedure Tp.ChangHint(Enabled: Boolean);
begin
if not (csDesigning in ComponentState) then
if Enabled then
begin
Application.ShowHint := False;
HintWindowClass := TMyHintWindow;
Application.ShowHint := True;
end
else
begin
Application.ShowHint := False;
HintWindowClass := THintWindow;
Application.ShowHint := True;
end;
end;
constructor Tp.Create(AOwner: TComponent);
begin
inherited;
FMyHintWindow := TMyHintWindow.Create(self);
end;
function Tp.GetColor: TColor;
begin
Result := FMyHintWindow.FColor;
end;
procedure Tp.SetColor(const Value: TColor);
begin
FMyHintWindow.FColor := Value;
end;
end.
以下是我写的代码。
unit coolhint;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TMyHintWindow = class(THintWindow)
private
FRegion: THandle;
FColor: TColor; //Hint中字体的颜色
procedure FreeCurrentRegion;
{ Private declarations }
public
procedure ActivateHint(Rect: TRect; const AHint: string);override;
procedure Paint;override;
procedure Createparams(var Params: TCreateParams);override;
constructor Create(AOwner: TComponent);override ;
destructor Destroy; override;
{ Public declarations }
published
end;
type
Tp = class(Tcomponent)
private
FMyHintWindow: TMyHintWindow;
FEnabled: Boolean;
procedure ChangHint(Enabled: Boolean);
procedure SetEnabled(const Value: Boolean);
function GetColor: TColor;
procedure SetColor(const Value: TColor);//改变Hint字体的颜色
protected
procedure Loaded;override;
public
constructor Create(AOwner: TComponent);override ;
destructor Destroy; override;
published
property Enabled: Boolean read FEnabled write SetEnabled;
property Color: TColor read GetColor write SetColor;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Ke', [Tp]);
end;
{ TMyHintWindow }
procedure TMyHintWindow.ActivateHint(Rect: TRect; const AHint: string);
begin
with Rect do
Right := Right + Canvas.TextWidth('wwww');
BoundsRect := Rect;
FreeCurrentRegion;
with BoundsRect do
Fregion := CreateRoundRectRgn(0,0,width,height,width,height);
if FRegion <> 0 then
SetWindowRgn(Handle, FRegion, True);
inherited;
end;
constructor TMyHintWindow.Create(AOwner: TComponent);
begin
inherited;
FColor := clRed;
end;
procedure TMyHintWindow.createparams(var Params: TCreateParams);
begin
inherited;
Params.Style := Params.Style and not ws_Border;
end;
destructor TMyHintWindow.Destroy;
begin
FreeCurrentRegion;
inherited;
end;
procedure TMyHintWindow.FreeCurrentRegion;
begin
if FRegion <> 0 then
begin
SetWindowRgn(Handle, 0, True);
DeleteObject(FRegion);
FRegion := 0;
end;
end;
procedure TMyHintWindow.paint;
var
R: TRect;
begin
r := ClientRect;
Inc(R.left,1);
Canvas.Font.Color:= FColor;
DrawText(Canvas.Handle,Pchar(Caption),Length(Caption),R,
DT_NOPREFIX OR DT_WORDBREAK OR DT_CENTER OR DT_VCENTER);
end;
{ Tp }
destructor Tp.Destroy;
begin
FMyHintWindow.Free;
inherited;
end;
procedure Tp.SetEnabled(const Value: Boolean);
begin
if Value<>FEnabled then
begin
FEnabled := Value;
changHint(FEnabled);
end;
end;
procedure Tp.Loaded;
begin
inherited;
changHint(FEnabled);
end;
procedure Tp.ChangHint(Enabled: Boolean);
begin
if not (csDesigning in ComponentState) then
if Enabled then
begin
Application.ShowHint := False;
HintWindowClass := TMyHintWindow;
Application.ShowHint := True;
end
else
begin
Application.ShowHint := False;
HintWindowClass := THintWindow;
Application.ShowHint := True;
end;
end;
constructor Tp.Create(AOwner: TComponent);
begin
inherited;
FMyHintWindow := TMyHintWindow.Create(self);
end;
function Tp.GetColor: TColor;
begin
Result := FMyHintWindow.FColor;
end;
procedure Tp.SetColor(const Value: TColor);
begin
FMyHintWindow.FColor := Value;
end;
end.