When using TballoonHint
, I need it more personalized in colors, shape, transparency and animated appearance, how can I do that?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Create your own descendant of TBalloonHint
or THintWindow
. Override the NCPaint
method to draw the outer edges (non-client area) and CalcHintRect
(if needed), and provide your own Paint
method to draw the interior as you'd like it to appear.
Then assign it to Application.HintWindowClass
in your .dpr file just before the call to Application.Run
.
Here's a (very minimal) example that does nothing but paint the standard hint window with a green background.
Save this as MyHintWindow.pas:
unit MyHintWindow;
interface
uses
Windows, Controls;
type
TMyHintWindow=class(THintWindow)
protected
procedure Paint; override;
procedure NCPaint(DC: HDC); override;
public
function CalcHintRect(MaxWidth: Integer; const AHint: string; AData: Pointer): TRect;
override;
end;
implementation
uses
Graphics;
{ TMyHintWindow }
function TMyHintWindow.CalcHintRect(MaxWidth: Integer;
const AHint: string; AData: Pointer): TRect;
begin
// Does nothing but demonstrate overriding.
Result := inherited CalcHintRect(MaxWidth, AHint, AData);
// Change window size if needed, using Windows.InflateRect with Result here
end;
procedure TMyHintWindow.NCPaint(DC: HDC);
begin
// Does nothing but demonstrate overriding. Changes nothing.
// Replace drawing of non-client (edges, caption bar, etc.) with your own code
// here instead of calling inherited. This is where you would change shape
// of hint window
inherited NCPaint(DC);
end;
procedure TMyHintWindow.Paint;
begin
// Draw the interior of the window. This is where you would change inside color,
// draw icons or images or display animations. This code just changes the
// window background (which it probably shouldn't do here, but is for demo
// purposes only.
Canvas.Brush.Color := clGreen;
inherited;
end;
end.
A sample project to use it:
- Create a new VCL forms application.
- Set the
Form1.ShowHint
property toTrue
in the Object Inspector. - Drop any control (a
TEdit
, for instance) on the form, and put some text in it'sHint
property. - Use the Project->View Source menu to display the project source.
Add the indicated lines to the project source:
program Project1;
uses
Forms,
MyHintWindow, // Add this line
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
HintWindowClass := TMyHintWindow; // Add this line
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
Sample output (ugly, but it works):