我想要一个更个性化的BalloonHint(背景颜色/圆角/透明度)

时间:2013-12-23 17:51:11

标签: delphi hint balloon

使用TballoonHint时,我需要在颜色,形状,透明度和动画外观方面更加个性化,我该怎么做?

1 个答案:

答案 0 :(得分:10)

创建自己的TBalloonHintTHintWindow后代。覆盖NCPaint方法以绘制外边(非客户区)和CalcHintRect(如果需要),并提供您自己的Paint方法来绘制内部,如您所愿出现。

然后在调用Application.HintWindowClass之前将其分配到.dpr文件中的Application.Run

这是一个(非常小的)示例,除了用绿色背景绘制标准提示窗口外什么都不做。

将其另存为 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.

使用它的示例项目:

  • 创建一个新的VCL表单应用程序。
  • 在对象检查器中将Form1.ShowHint属性设置为True
  • 删除表单上的所有控件(例如TEdit),并在其Hint属性中放入一些文本。
  • 使用项目 - >查看来源菜单显示项目来源。

将指示的行添加到项目源:

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.

示例输出(丑陋但可行):

Image of memo control with ugly green hint popup