Delphi – TMainMenu Not Shown When VCL Styles is Removed from NC Area

delphidelphi-xe2vcl-styles

I'm using this code to remove the vcl styles from the non client area of a form.

type
  TFormStyleHookNC= class(TMouseTrackControlStyleHook)
  protected
    procedure PaintBackground(Canvas: TCanvas); override;
    constructor Create(AControl: TWinControl); override;
  end;

constructor TFormStyleHookNC.Create(AControl: TWinControl);
begin
  inherited;
  OverrideEraseBkgnd := True;
end;

procedure TFormStyleHookNC.PaintBackground(Canvas: TCanvas);
var
  Details: TThemedElementDetails;
  R: TRect;
begin
  if StyleServices.Available then
  begin
    Details.Element := teWindow;
    Details.Part := 0;
    R := Rect(0, 0, Control.ClientWidth, Control.ClientHeight);
    StyleServices.DrawElement(Canvas.Handle, Details, R);
  end;
end;


initialization
 TStyleManager.Engine.RegisterStyleHook(TForm3, TFormStyleHookNC);

Before to apply this style hook the form looks like

enter image description here

and after

enter image description here

As you can see the menu disappears, The question is : how I can fix this? I mean how i can remove the vcl styles from the non client area of a form without remove the TMainMenu?

Best Answer

When you uses the vcl styles, the TMain menu is drawn by the TMainMenuBarStyleHook vcl style hook, which is defined inside of the TFormStyleHook (the hook of the forms), in this case because you are not using this hook there is not code to draw the TMainMenu.

Two possible solutions are

1) Implement the vcl style hook for the TMainMenu inside of the TFormStyleHookNC , just like the TFormStyleHook does.

2)or even better use a TActionMainMenuBar component instead of a TMainMenu, this component is very well integrated with the vcl styles (check the next sample image).

enter image description here

Related Question