从 Delphi VCL应用程序可靠地读取Windows比例因子

vs91vp4v  于 2023-10-18  发布在  Windows
关注(0)|答案(3)|浏览(159)

为了正确处理应用程序中的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;

我的问题是,这总是返回100(96 dpi),无论我在Windows(Windows 10)中设置了什么设置。
我已经编译了我的应用程序与默认的项目设置(即。清单文件自动生成,标签包括:启用高分辨率主题和启用高DPI)。
我也试过关闭“启用高DPI”,然后在应用程序中手动启用它(但我得到一个错误,当我尝试,这表明DPI模式已经设置,但这可能是另一个问题,另一个时间)。
有人能指导我一个方向,让我可靠地读取 * 当前 * DPI比例因子设置在Windows中?我还需要响应DPI的变化,但似乎不能拦截WM_DPICHANGED消息。我应该在哪里拦截这条信息?是在应用程序级别还是在表单级别?
为了重现我的测试设置,创建一个空的VCL应用程序,其中包含一个名为Button1的按钮。在FormCreate事件中,放入以下代码:

procedure TForm14.FormCreate(Sender: TObject);
begin
  Button1.Caption:=IntToStr(WindowsScaleFactor)
end;

将Button1.OnClick事件附加到FormCreate方法,以便按钮标题在启动时初始化,并在每次单击时刷新。
然后运行应用程序。按钮标题应该读为100到开始(如果您使用100%缩放)。然后尝试在Windows中更改缩放比例并单击按钮。它 * 应该 * 更改为您选择的值,但(在我的PC上)它仍然返回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>
1bqhqjot

1bqhqjot1#

我建议你从阅读这篇博客文章开始
dpi awareness in vcl applications
您可以通过为TForm. OnBeforeManagerorDPIChanged和TForm. OnAfterManagerorDPIChanged设置事件处理程序来响应dpi更改

procedure FormBeforeMonitorDpiChanged(Sender: TObject; OldDPI, NewDPI: Integer);
procedure FormAfterMonitorDpiChanged(Sender: TObject; OldDPI, NewDPI: Integer);

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

qgzx9mmu

qgzx9mmu2#

在VCL应用程序中,您可以从表单中的每个TControl中检索比例因子。

procedure TfrmMain.FormShow(Sender: TObject);
var
 currentScaleFactor : Single;
begin
 currentScaleFactor := TControl(btnDoResize).ScaleFactor;
 lblScaleFator.Caption := 'Scalefactor: ' + currentScaleFactor.ToString;
end;
e3bfsja2

e3bfsja23#

一个给予正确缩放因子的过程,以及控件的相应物理屏幕坐标。在 Delphi 2007中测试。这适用于windows桌面8.1及更高版本,服务器2012 R2及更高版本(根据microsoft)。
该过程假定多个屏幕是从左到右对齐的,而不是从上到下对齐的

type
  GetScaleFactorForMonitorFunc = function(hMonitor: HDC; var aFactor: Word): longword; cdecl;

const
  GetScaleFactorForMonitor: GetScaleFactorForMonitorFunc = nil;

procedure GetZoom(aControl: TControl; var z: extended; var x, y: integer);
var
  aPoint: TPoint;
  aMonitor: TMonitor;
  HResult: longword;
  aFactor: word;
  i: integer;
  x1: integer;
begin
  z := 1;
  x := aControl.Left;
  y := aControl.Top;
  aPoint.x := x;
  aPoint.y := y;
  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;

相关问题