可靠地从 Delphi VCL 应用程序中读取 Windows 缩放因子

5
在努力处理应用程序中的 DPI 变化时,我使用以下代码来读取当前的缩放因子:
TYPE TZoom = BYTE;

FUNCTION OldStyleGetDpiForSystem : TZoom; cdecl;
  VAR
    DC          : HDC;
    X,Y,Z       : LongWord;

  BEGIN
    DC:=GetDC(0);
    TRY
      X:=GetDeviceCaps(DC,LOGPIXELSX);
      Y:=GetDeviceCaps(DC,LOGPIXELSY)
    FINALLY
      ReleaseDC(0,DC)
    END;
    IF X>Y THEN Result:=X ELSE Result:=Y
  END;

FUNCTION GetDpiForSystem : TZoom;
  TYPE
    GetDpiForSystemFunc = FUNCTION : TZoom; cdecl;

  CONST
    GetDpiForSystem     : GetDpiForSystemFunc = NIL;

  BEGIN
    IF NOT Assigned(GetDpiForSystem) THEN BEGIN
      // Try to use official method (available from Windows 10, version 1607 [desktop apps only] and on)
      GetDpiForSystem:=GetProcAddress(LoadLibrary('USER32.DLL'),'GetDpiForSystem');
      // If not found, then use fall-back method with GetDeviceCaps of DeskTop
      IF NOT Assigned(GetDpiForSystem) THEN GetDpiForSystem:=OldStyleGetDpiForSystem
      // In any case, only determine method once, but call the method every time, as the DPI can change
      // while the application is running
    END;
    Result:=ROUND(GetDpiForSystem/USER_DEFAULT_SCREEN_DPI*100.0)
  END;

FUNCTION WindowsScaleFactor : TZoom;
  BEGIN
    Result:=GetDpiForSystem
  END;

我的问题是,无论我在 Windows(Windows 10)中设置什么样的设置,此代码始终返回 100(96 dpi)。
我使用默认项目设置编译了我的应用程序(即生成清单文件自动生成,要包括的标签:启用运行时主题和启用高 DPI)。
我还尝试关闭“启用高 DPI”,然后在应用程序中手动启用它(但我尝试时会出现错误,提示 DPI 模式已经设置,但这可能是另一个时间的另一个问题)。
有人可以指导我一个方向,允许我可靠地读取在 Windows 中设置的当前 DPI 比例因子吗?我还需要响应 DPI 更改,但似乎无法截取 WM_DPICHANGED 消息。我应该在应用程序级别还是表单级别拦截此消息?
为了重现我的测试设置,请创建一个名为 Button1 的单个按钮的空 VCL 应用程序。在 FormCreate 事件中,放置以下代码:
procedure TForm14.FormCreate(Sender: TObject);
begin
  Button1.Caption:=IntToStr(WindowsScaleFactor)
end;

将Button1.OnClick事件附加到FormCreate方法中,以便在启动时初始化按钮标题,并在每次单击按钮时刷新。
然后运行应用程序。按钮标题应该一开始显示为100(如果您正在以100%缩放运行)。然后尝试更改Windows中的缩放并单击按钮。它应该改为您选择的值,但是(在我的电脑上)它仍然返回100%。
清单(从编译的.EXE中提取)如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
  <asmv3:application>
    <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
      <dpiAware>True/PM</dpiAware>
    </asmv3:windowsSettings>
  </asmv3:application>
  <dependency>
    <dependentAssembly>
      <assemblyIdentity
        type="win32"
        name="Microsoft.Windows.Common-Controls"
        version="6.0.0.0"
        publicKeyToken="6595b64144ccf1df"
        language="*"
        processorArchitecture="*"/>
    </dependentAssembly>
  </dependency>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel
          level="asInvoker"
          uiAccess="false"
        />
        </requestedPrivileges>
    </security>
  </trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
        <application>
                <!--The ID below indicates app support for Windows Vista -->
                <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
                <!--The ID below indicates app support for Windows 7 -->
                <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
                <!--The ID below indicates app support for Windows 8 -->
                <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
                <!--The ID below indicates app support for Windows 8.1 -->
                <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
                <!--The ID below indicates app support for Windows 10 -->
                <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
        </application>
</compatibility>
</assembly>
3个回答

5
我可以建议您从阅读这篇博客文章开始:VCL应用程序中的dpi感知
您可以通过设置TForm.OnBeforeMonitorDPIChanged和TForm.OnAfterMonitorDPIChanged事件处理程序来响应dpi更改。
procedure FormBeforeMonitorDpiChanged(Sender: TObject; OldDPI, NewDPI: Integer);
procedure FormAfterMonitorDpiChanged(Sender: TObject; OldDPI, NewDPI: Integer);

事件参数将告诉您新的DPI和旧的DPI。
您的清单设置是正确的。

1
在VCL应用程序中,您可以从表单中的每个TControl检索比例因子。
procedure TfrmMain.FormShow(Sender: TObject);
var
 currentScaleFactor : Single;
begin
 currentScaleFactor := TControl(btnDoResize).ScaleFactor;
 lblScaleFator.Caption := 'Scalefactor: ' + currentScaleFactor.ToString;
end;

在RAD Studio 11.1 Alexandria中无法正常运行。即使所有内容都按125%缩放,它的值仍为1。 - truthseeker

0
一个过程,为控件提供正确的缩放因子和相应的物理屏幕坐标。在Delphi 2007中进行了测试。 根据microsoft,此方法适用于Windows桌面8.1及更高版本,服务器2012 R2及更高版本。
该过程假设多个屏幕从左到右对齐,而不是从上到下。
type
  GetScaleFactorForMonitorFunc = function(hMonitor: HDC; var aFactor: Word): longword; cdecl;

var
  GetScaleFactorForMonitor: GetScaleFactorForMonitorFunc = nil;

class procedure TPrintScreen.GetZoom(aControl: TControl; var z: extended; var x, y: integer);
var
  aPoint: TPoint;
  aMonitor: TMonitor;
  HResult: longword;
  aFactor: word;
  i: integer;
  x1, y1: integer;
begin
  z := 1;
  if Assigned(aControl.Parent) then
  begin
    aPoint.x := aControl.Left;
    aPoint.y := aControl.Top;
    aPoint := aControl.Parent.ClientToScreen(aPoint);
    x := aPoint.x;
    y := aPoint.y;
  end
  else
  begin
    x := aControl.Left;
    y := aControl.Top;
  end;
  aPoint.x := aControl.Width div 2;
  aPoint.y := aControl.Height div 2;
  aPoint := aControl.ClientToScreen(aPoint);
  aMonitor := Screen.MonitorFromPoint(aPoint);
  if not Assigned(GetScaleFactorForMonitor) then
    GetScaleFactorForMonitor := GetProcAddress(LoadLibrary('SHCORE.DLL'), 'GetScaleFactorForMonitor');

  if Assigned(GetScaleFactorForMonitor) then
  begin
    HResult := GetScaleFactorForMonitor(aMonitor.Handle, aFactor);
    if HResult = 0 then
    begin
      z := 0.01 * aFactor;
      x1 := 0;
      if aMonitor.MonitorNum > 0 then
        for i := 0 to aMonitor.MonitorNum - 1 do
        begin
          HResult := GetScaleFactorForMonitor(Screen.Monitors[i].Handle, aFactor);
          if HResult = 0 then
          begin
            x := x - Screen.Monitors[i].width;
            x1 := x1 + round(0.01 * aFactor * Screen.Monitors[i].width);
          end;
        end;
      x := round(z * x) + x1;
      y := round(z * y);
    end;
  end;
end;

1
我相信你犯了一个错误。"ClientToScreen"假设坐标是相对于控件的左上角的。然而,你提取的坐标是相对于父控件的左上角的。另外,你应该使用控件的中心点作为参考点(就像Windows一样)。所以,x和y变量应该初始化为x:=aControl.Width DIV 2 / y:=aControl.Height DIV 2。 - undefined
另外一件事是你在入口点使用了"const"。你应该考虑使用"var",因为你在过程中对它进行了修改。或者,你可以在代码中加入{$J+},以表明这个编译器指令是必要的,才能修改一个类型常量。 - undefined
我明白你说的关于中心点的观点,所以我改变了用于找到显示器的TPoint的设置。你说的const也是对的。谢谢你的评论! - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接