Search code examples
delphilazarus

Create TPanel which looks like TMainMenu?


I want to create a component which looks similar to TMainMenu but is based on TPanel. TMainMenu under Windows 7 has a gradient of colors.

How can I achieve similar effect for TPanel? Not any gradient but a Windows gradient so it looks like a native component.

I also tried TMainMenu.PaintTo but this method is not available.


Solution

  • This is actually easy thanks to the UxTheme API (uses UxTheme).

    Using OpenThemeData and DrawThemeBackground,

    procedure TForm1.FormPaint(Sender: TObject);
    begin
    
      var R := Rect(0, 0, ClientWidth, 32);
    
      var h := OpenThemeData(Handle, 'MENU');
      if h <> 0 then
        try
          DrawThemeBackground(h, Canvas.Handle, MENU_BARBACKGROUND, MB_ACTIVE, R, nil);
        finally
          CloseThemeData(h);
        end;
    
    end;
    

    produces

    Screenshot of the drawn element in a new VCL form.

    Of course, in a real application, you would refactor this. For instance, you wouldn't hardcode the 32 constant; instead, you'd determine the appropriate menu bar height given current DPI scaling,