delphihintballoon

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


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


Solution

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

    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