Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a TButton or other controls inside a THintWindow?

Tags:

hint

delphi

I am trying to create a THintWindow and place a TButton or a TFrame on it. here is my code:

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;

The Hint window is shown but I don't see the TButton. it seems that there are no child windows inside the Hint window (I tested with Spy++ for "first child"). I also tried to subclass THintWindow with new CreateParams ie:

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

When I create a TFrame as child on the Hint window, Spy++ shows that there is a child on the hint window but I cant see it (even after I force it to be visible).

Any feed-backs on this?

like image 327
kobik Avatar asked Nov 17 '11 11:11

kobik


People also ask

How do I add a button in Windows?

Use the CreateWindow function to create a button control. In the following C++ example, the m_hwnd parameter is the handle to the parent window. The BS_DEFPUSHBUTTON style specifies that a default push button should be created.

What design window do you use to add controls to a form?

The primary way a control is added to a form is through the Visual Studio Designer, but you can also manage the controls on a form at run time through code.


1 Answers

Don't ask me why, but you can make this work in old versions of Delphi by setting the ParentWindow to Application.Handle immediately after you create the THintWindow instance:

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

This answer was inspired by the modern versions of the Delphi VCL source.

like image 143
David Heffernan Avatar answered Oct 01 '22 20:10

David Heffernan