From 123f615a6d9a27ef6ed67ca424b3ce4aa00fb309 Mon Sep 17 00:00:00 2001 From: Ondrej Pokorny Date: Wed, 16 Nov 2022 00:43:44 +0100 Subject: [PATCH] win32: fix TMenuItemHelper.GetDPI for popup menus --- lcl/interfaces/win32/win32wsmenus.pp | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/lcl/interfaces/win32/win32wsmenus.pp b/lcl/interfaces/win32/win32wsmenus.pp index 9b933b6ef1..7a373de204 100644 --- a/lcl/interfaces/win32/win32wsmenus.pp +++ b/lcl/interfaces/win32/win32wsmenus.pp @@ -104,16 +104,31 @@ end; function TMenuItemHelper.GetDPI: Integer; var - AWnd: HWND; - xMerged: TMenu; + xMenu: TMenu; + xMon: TMonitor; begin // PopupMenus can be all scaled, but main menu bar is scaled only since DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 if not IsInMenuBar or AreDpiAwarenessContextsEqual(GetThreadDpiAwarenessContext, DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2) then begin - AWnd := TCustomForm(GetMergedParentMenu.Parent).Handle; - if AWnd<>0 then - Exit(GetDpiForWindow(AWnd)); + xMenu := GetMergedParentMenu; + if Assigned(xMenu) and (xMenu is TMainMenu) and (xMenu.Parent is TCustomForm) then + begin + if TCustomForm(xMenu.Parent).HandleAllocated then + Exit(GetDpiForWindow(TCustomForm(xMenu.Parent).Handle)); + end else + begin + xMenu := Self.GetParentMenu; + if xMenu is TPopupMenu then + begin + if (TPopupMenu(xMenu).PopupComponent is TWinControl) + and (TWinControl(TPopupMenu(xMenu).PopupComponent).HandleAllocated) then + Exit(GetDpiForWindow(TWinControl(TPopupMenu(xMenu).PopupComponent).Handle)); + xMon := Screen.MonitorFromPoint(TPopupMenu(xMenu).PopupPoint); + if Assigned(xMon) then + Exit(xMon.PixelsPerInch); + end; + end; end; // default Result := ScreenInfo.PixelsPerInchX;