Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support DPI per Monitor V2 and Owner Draw of non VCL menus #317

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

*.bak
52 changes: 50 additions & 2 deletions Common/Vcl.Styles.Utils.Menus.pas
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,14 @@ procedure TSysPopupStyleHook.DrawItem(Canvas: TCanvas; const Index: integer; con
LParentMenu: TMenu;
ItemRect2: TRect;


LMenuItemInfo: TMenuItemInfo;
LGUIThreadinfo: TGUIThreadinfo;
DrawItemStruct: TDrawItemStruct;
begin
//AW: 27.11.2024 - Use DPI from Window instead of MainForm
if CheckPerMonitorV2SupportForWindow(Handle) then
LPixelsPerInch := GetDPIForWindow(Handle)
else
if Assigned(Application.Mainform) then
LPixelsPerInch := Application.MainForm.Monitor.PixelsPerInch
else
Expand All @@ -547,6 +553,48 @@ procedure TSysPopupStyleHook.DrawItem(Canvas: TCanvas; const Index: integer; con
{ Fast access . }
LSysPopupItem := Items[Index]; // Do not destroy !!
DC := Canvas.Handle;

if (Style <> isSep) and (LSysPopupItem.VCLMenuItems = nil) and
(ItemText = '') and LSysPopupItem.IsItemOwnerDraw then
begin
// redirect owner draw of non VCL MenuItems to Owner Window Handle
// assume they use themed methods to draw the MenuItem
// --> File Dialogs uses this
LGUIThreadinfo.cbSize := sizeof(TGUIThreadinfo);
if GetGUIThreadInfo(GetCurrentThreadID(), LGUIThreadinfo) then
begin
DrawItemStruct.CtlType := ODT_MENU;
DrawItemStruct.CtlID := 0;
DrawItemStruct.itemID := LSysPopupItem.GetItemID;
DrawItemStruct.itemAction := ODA_DRAWENTIRE;
DrawItemStruct.itemState := 0;

if isDefault in State then
DrawItemStruct.itemState := DrawItemStruct.itemState or ODS_DEFAULT;
if isHot in State then
DrawItemStruct.itemState := DrawItemStruct.itemState or ODS_HOTLIGHT; // ODS_SELECTED;
if isDisabled in State then
DrawItemStruct.itemState := DrawItemStruct.itemState or ODS_DISABLED;
if isChecked in State then
DrawItemStruct.itemState := DrawItemStruct.itemState or ODS_CHECKED;

DrawItemStruct.hwndItem := FMenu;
DrawItemStruct.hDC := DC;
DrawItemStruct.rcItem := ItemRect2;

FillChar(LMenuItemInfo, sizeof(TMenuItemInfo), Char(0));
LMenuItemInfo.cbSize := sizeof(TMenuItemInfo);
LMenuItemInfo.fMask := MIIM_DATA;
if GetMenuItemInfo(FMenu, LSysPopupItem.FIndex, True, LMenuItemInfo) then
DrawItemStruct.itemData := LMenuItemInfo.dwItemData
else
DrawItemStruct.itemData := 0;

SendMessage( LGUIThreadinfo.hwndMenuOwner, WM_DRAWITEM, 0, NativeInt(@DrawItemStruct) );
exit;
end;
end;

R := ItemRect2;
LThemedMenu := tmPopupItemNormal;
if isHot in State then
Expand Down Expand Up @@ -870,7 +918,7 @@ procedure TSysPopupStyleHook.DrawItem(Canvas: TCanvas; const Index: integer; con
DrawText(Canvas.Handle, LDetails, Copy(ItemText, 1, P), LTextRect, LTextFormat)
end
else
DrawText(Canvas.Handle, LDetails, ItemText, LTextRect, LTextFormat)
DrawText(Canvas.Handle, LDetails, ItemText, LTextRect, LTextFormat);
end;

{Draw vertical menu bar}
Expand Down