【问题标题】:How to create a TButton or other controls inside a THintWindow?如何在 THintWindow 中创建 TButton 或其他控件?
【发布时间】:2011-12-31 06:53:45
【问题描述】:

我正在尝试创建一个 THintWindow 并在其上放置一个 TButton 或 TFrame。这是我的代码:

TForm1 = class(TForm)
  Button1: TButton;
  Button2: TButton;
  procedure FormCreate(Sender: TObject);
  procedure Button1Click(Sender: TObject);
  procedure Button2Click(Sender: TObject);
private
  HintWindow: THintWindow;
public
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  HintWindow := THintWindow.Create(Self);
  HintWindow.Color := clInfoBk;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  P: TPoint;
  R: TRect;
  Control: TControl;
begin
  Control := Button1;
  P := Control.ClientToScreen(Point(0, Control.Height));
  R := Rect(P.X, P.Y, P.x + 100, P.Y + 100);
  with TButton.Create(HintWindow) do
  begin
    Parent := HintWindow;
    Caption := 'My Button';
  end;
  HintWindow.ActivateHint(R, 'My Hint');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  HintWindow.ReleaseHandle;
end;

提示窗口已显示,但我没有看到 TButton。提示窗口内似乎没有子窗口(我用 Spy++ 测试了“第一个孩子”)。 我还尝试使用新的 CreateParams 将 THintWindow 子类化,即:

procedure TMyHintWindow.CreateParams(var Params: TCreateParams);
begin
  inherited;
  Params.Style := Params.Style or WS_CLIPCHILDREN;
  Params.ExStyle := Params.ExStyle or WS_EX_CONTROLPARENT;
end;

当我在提示窗口上创建一个 TFrame 作为子级时,Spy++ 显示提示窗口上有一个子级,但我看不到它(即使我强制它可见)。

对此有何反馈?

【问题讨论】:

  • 该代码对我来说非常有效,不需要子类化。你有什么版本的 Delphi?
  • @David Heffernan:你能看到那个按钮吗?我用 D5。
  • D5.啊!是的,我在 XE2 上,我可以很好地看到按钮。
  • 大卫,在你的控制单元中询问“THintWindow.Create”和“THintWindow.CreateParams”的源代码会不会太过分了?
  • 看来你我同时想出了同一个答案!我研究了XE2源代码,跳出来的最明显的区别是ActivateHint中发生的ParentWindow的设置。 XE2 代码总是将其设置为 Application.Handle,所以我认为这只是最简单的方法。无需尝试将其设置为ActiveForm.Handle

标签: delphi hint


【解决方案1】:

不要问我为什么,但是您可以在创建 THintWindow 实例后立即将 ParentWindow 设置为 Application.Handle 以使其在旧版本的 Delphi 中工作:

HintWindow := THintWindow.Create(Self);
HintWindow.ParentWindow := Application.Handle;

这个答案的灵感来自现代版本的 Delphi VCL 源代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多