Brian Frost
Brian Frost

Reputation: 13454

How can I create a Delphi child window that has a main menu?

I am working with a Delphi form that contains a TMainMenu. In a particular situation I want to show this form parented and client-aligned inside another form. This works fine but the main menu of the parented form does not appear. I see a comment in this SO question that states "A child window cannot have a menu". Is there anything that I can do to override this behaviour and make a TMainMenu appear?

An aside: I've only just noticed this because where I have used this principle before, I've been using the Developer Express menu component and this is quite happy to show in a child form.

Later edit: Using the code from TLama below, this works (but the child menu is not themed, I,e very plain): This works:

procedure TForm65.FormShow(Sender: TObject);
begin
  Winapi.Windows.SetParent(ChildForm.Handle, Handle); // <<<<<<<<
  ChildForm.BorderStyle := bsNone;
  ChildForm.Align := alClient;
  ChildForm.Show;
end;

This code DOES NOT work. Why?

procedure TForm65.FormShow(Sender: TObject);
begin
  ChildForm.Parent := Self; // <<<<<<<<<
  ChildForm.BorderStyle := bsNone;
  ChildForm.Align := alClient;
  ChildForm.Show;
end;

Upvotes: 0

Views: 2838

Answers (1)

David Heffernan
David Heffernan

Reputation: 613013

MSDN makes this perfectly clear:

A child window has a client area but no other features, unless they are explicitly requested. An application can request a title bar, a window menu, minimize and maximize buttons, a border, and scroll bars for a child window, but a child window cannot have a menu.

This refers to the menu as drawn by Windows itself. If your component custom draws a menu bar, then of course it can have a menu, even if it is a child window.


Your call to SetParent does not make your window into a child window. This is explained in the documentation:

For compatibility reasons, SetParent does not modify the WS_CHILD or WS_POPUP window styles of the window whose parent is being changed. Therefore, if hWndNewParent is NULL, you should also clear the WS_CHILD bit and set the WS_ POPUP style after calling SetParent. Conversely, if hWndNewParent is not NULL and the window was previously a child of the desktop, you should clear the WS_POPUP style and set the WS_CHILD style before calling SetParent.

Upvotes: 3

Related Questions