I want a more personalized BalloonHint (Background color/Rounded Corner/transparency)

≯℡__Kan透↙ 提交于 2019-11-28 14:22:06

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 to True in the Object Inspector.
  • Drop any control (a TEdit, for instance) on the form, and put some text in it's Hint 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):

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!