如何在Delphi Seattle中运行时DPI更改后处理菜单缩放

Bra*_*ggs 16 delphi vcl multiple-monitors dpi

当对表单类添加了对运行时DPI切换的支持时,没有考虑像菜单这样的基本UI元素.

菜单绘图从根本上被打破,因为它依赖于Screen.MenuFont,这是一个系统范围的度量,不是特定于监视器.因此,虽然表单本身可以相对简单地进行适当缩放,但是显示在其上的菜单只有在缩放恰好匹配加载到Screen对象中的任何度量标准时才能正常工作.

这是主菜单栏,弹出菜单和表单上所有弹出菜单的问题.如果将表单移动到具有与系统指标不同的DPI的监视器,则这些都不会缩放.

真正做到这一点的唯一方法是修复VCL.等待Embarcadero充实多DPI并不是一个真正的选择.

查看VCL代码,基本问题是Screen.MenuFont属性被分配给菜单画布,而不是选择适合显示菜单的监视器的字体.只需在VCL源代码中搜索Screen.MenuFont即可找到受影响的类.

在不必完全重写所涉及的类的情况下,解决此限制的正确方法是什么?

我的第一个倾向是使用绕行来跟踪菜单弹出窗口并在用于设置菜单时覆盖Screen.MenuFont属性.这似乎太过分了.

Bra*_*ggs 5

这是一个目前正在使用的解决方案.使用Delphi Detours库,将此单元添加到dpr使用列表(我必须在其他表单之前将其放在我的列表顶部附近)导致正确的字体大小应用于菜单画布,基于保存任何弹出菜单中的菜单项.此解决方案故意忽略顶层菜单(主菜单栏),因为VCL没有正确处理所有者测量的项目.

unit slMenuDPIFix;

// add this unit to the main application dpr file BEFORE ANY FORMS in the uses list.

interface

implementation

uses
  Winapi.Windows, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Menus, slScaleUtils, Math,
  DDetours;

type
  TMenuClass = class(TMenu);
  TMenuItemClass = class(TMenuItem);

var
  TrampolineMenuCreate: procedure(const Self: TMenuClass; AOwner: TComponent) = nil;
  TrampolineMenuItemAdvancedDrawItem: procedure(const Self: TMenuItemClass; ACanvas: TCanvas; ARect: TRect; State: TOwnerDrawState; TopLevel: Boolean) = nil;
  TrampolineMenuItemMeasureItem: procedure(const Self: TMenuItemClass; ACanvas: TCanvas; var Width, Height: Integer) = nil;

function GetPopupDPI(const MenuItem: TMenuItemClass): Integer;
var
  pm: TMenu;
  pcf: TCustomForm;
begin
  Result := Screen.PixelsPerInch;
  pm := MenuItem.GetParentMenu;
  if Assigned(pm) and (pm.Owner is TControl) then
    pcf := GetParentForm(TControl(pm.Owner))
  else
    pcf := nil;
  if Assigned(pcf) and (pcf is TForm) then
    Result := TForm(pcf).PixelsPerInch;
end;

procedure MenuCreateHooked(const Self: TMenuClass; AOwner: TComponent);
begin
  TrampolineMenuCreate(Self, AOwner);
  Self.OwnerDraw := True;     // force always ownerdraw.
end;

procedure MenuItemAdvancedDrawItemHooked(const Self: TMenuItemClass; ACanvas: TCanvas; ARect: TRect; State: TOwnerDrawState; TopLevel: Boolean);
begin
  if (not TopLevel) then
  begin
    ACanvas.Font.Height := MulDiv(ACanvas.Font.Height, GetPopupDPI(Self), Screen.PixelsPerInch);
  end;
  TrampolineMenuItemAdvancedDrawItem(Self, ACanvas, ARect, State, TopLevel);
end;

procedure MenuItemMeasureItemHooked(const Self: TMenuItemClass; ACanvas: TCanvas; var Width, Height: Integer);
var
  lHeight: Integer;
  pdpi: Integer;
begin
  pdpi := GetPopupDPI(Self);
  if (Self.Caption <> cLineCaption) and (pdpi <> Screen.PixelsPerInch) then
  begin
    ACanvas.Font.Height := MulDiv(ACanvas.Font.Height, pdpi, Screen.PixelsPerInch);
    lHeight := ACanvas.TextHeight('|') + MulDiv(6, pdpi, Screen.PixelsPerInch);
  end else
    lHeight := 0;

  TrampolineMenuItemMeasureItem(Self, ACanvas, Width, Height);

  if lHeight > 0 then
    Height := Max(Height, lHeight);
end;

initialization

  TrampolineMenuCreate := InterceptCreate(@TMenuClass.Create, @MenuCreateHooked);
  TrampolineMenuItemAdvancedDrawItem := InterceptCreate(@TMenuItemClass.AdvancedDrawItem, @MenuItemAdvancedDrawItemHooked);
  TrampolineMenuItemMeasureItem := InterceptCreate(@TMenuItemClass.MeasureItem, @MenuItemMeasureItemHooked);

finalization

  InterceptRemove(@TrampolineMenuCreate);
  InterceptRemove(@TrampolineMenuItemAdvancedDrawItem);
  InterceptRemove(@TrampolineMenuItemMeasureItem);

end.
Run Code Online (Sandbox Code Playgroud)

人们可以轻松修补Vcl.Menus,但我不想这样做.

  • 添加链接到[RSP-12580 VCL菜单忽略每个监视器DPI缩放](https://quality.embarcadero.com/browse/RSP-12580)以进行交叉引用. (3认同)