Difference between revisions of "Win32MenuStyler"

From Lazarus wiki
(Usage)
(Details)
Line 15: Line 15:
  
 
* background, caption, also for disabled state
 
* background, caption, also for disabled state
* checked state, also for RadioItems
+
* checked state, also for radio-items
* shortcut
+
* shortcut text right-aligned
 
* sub-menu arrow
 
* sub-menu arrow
 +
* underlines for accelerators, and only when OS requires it (not painted until menu is activated)
  
 
What is not supported:
 
What is not supported:

Revision as of 20:04, 19 September 2020

Windows logo - 2012.svg

This article applies to Windows only.

See also: Multiplatform Programming Guide

About

This is helper unit win32menustyler, which helps to theme TMainMenu/TPopupMenu for Lazarus Windows apps. Sometimes app has dark theme, so it's needed to make TMainMenu also dark.

win32menustyler.png

Author: Alexey Torgashin

License: MPL 2.0 or LGPL

Details

What it paints for menu items:

  • background, caption, also for disabled state
  • checked state, also for radio-items
  • shortcut text right-aligned
  • sub-menu arrow
  • underlines for accelerators, and only when OS requires it (not painted until menu is activated)

What is not supported:

  • icons of menu items (patch welcome)
  • horizontal 1 pixel white line under menu bar (non-client area, application must handle WM_NCPAINT somehow)

About other OS: GitHub repo has cross-platform "lazmenustyler" unit, but it don't give any effect on gtk2 demo app. And it should not work on macOS, macOS has very special theming and menu is located on the screen top.

Usage

  • add win32menustyler to "uses"
  • in form's OnShow, call:
    • MenuStyler.ApplyToForm(Self, False) to theme MainMenu
    • MenuStyler.ApplyToMenu() for all needed PopupMenus
  • when needed to apply different colors later:
    • change them in global theme var
    • call MenuStyler.ApplyToForm(Self, True): second param should be True here, this forces form's resize to repaint the menu bar
    • not needed to call again MenuStyler.ApplyToMenu() for PopupMenus
  • to cancel theming, call MenuStyler.ResetForm() and MenuStyler.ResetMenu()

Unit gives global var to change all theming details:

type
  TWin32MenuStylerTheme = record
    ColorBk: TColor;
    ColorBkSelected: TColor;
    ColorFont: TColor;
    ColorFontDisabled: TColor;
    ColorFontShortcut: TColor;
    CharCheckmark: WideChar;
    CharRadiomark: WideChar;
    CharSubmenu: WideChar;
    FontName: string;
    FontSize: integer;
    IndentMinPercents: integer;
    IndentBigPercents: integer;
    IndentRightPercents: integer;
    IndentSubmenuArrowPercents: integer;
  end;

var
  MenuStylerTheme: TWin32MenuStylerTheme;

Download

GitHub: https://github.com/Alexey-T/Win32MenuStyler