1

When using TballoonHint, I need it more personalized in colors, shape, transparency and animated appearance, how can I do that?

0

1 Answer 1

11

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):

Image of memo control with ugly green hint popup

5
  • Thank you but could you give me the code , I am not an expert in Delphi. Dec 27, 2013 at 9:16
  • Thank you Ken White, it is very helpful . Dec 30, 2013 at 13:33
  • You do the same thing, except you descend from the balloon hint window instead. I'm not going to spoon-feed you the code; I've shown you what you need to do. You need to figure out how to make it work for your needs now. :-)
    – Ken White
    Dec 30, 2013 at 14:00
  • type TMyHintWindow=class(TBalloonHint) protected procedure Paint; override; procedure NCPaint(DC: HDC); override; Jan 1, 2014 at 10:43
  • <code>type TMyHintWindow=class(TBalloonHint) protected procedure Paint; override; procedure NCPaint(DC: HDC); override; </code> Jan 1, 2014 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.