[DllImport(shcore.dll")]static extern int SetProcessDpiAwareness(ProcessDPIAwareness value);枚举 ProcessDPIAwareness{DPI_Unaware = 0,System_DPI_Aware = 1,Per_Monitor_DPI_Aware = 2}Windows 10 版本 1703SetProcessDpiAwarenessContext()(选择 Per-Monitor DPI-Awareness 时,请使用 Context_PerMonitorAwareV2)另见:混合模式 DPI 缩放和 DPI 感知 API - MSDNWindows 10 版本 1809(2018 年 10 月)新的DPI_AWARENESS_CONTEXT 已添加:DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALEDDPI 不知道基于 GDI 的内容质量有所提高.这种模式行为类似于 DPI_AWARENESS_CONTEXT_UNAWARE,但也启用系统自动提高文本的渲染质量和当窗口显示在高 DPI 上时的其他基于 GDI 的基元监控.使用 GetWindowDpiAwarenessContext() 函数来检索窗口的 DPI_AWARENESS_CONTEXT 句柄和 GetThreadDpiAwarenessContext() 用于当前线程的 DPI_AWARENESS_CONTEXT 句柄.然后 GetAwarenessFromDpiAwarenessContext() 从 DPI_AWARENESS_CONTEXT 结构中检索 DPI_AWARENESS 值.[DllImport(user32.dll", SetLastError=true)]静态外部 IntPtr GetWindowDpiAwarenessContext(IntPtr hWnd);[DllImport(user32.dll", SetLastError=true)]静态外部 IntPtr GetThreadDpiAwarenessContext();[DllImport(user32.dll", SetLastError=true)]static extern int GetAwarenessFromDpiAwarenessContext(IntPtr DPI_AWARENESS_CONTEXT);[DllImport(user32.dll", SetLastError=true)]static extern int SetProcessDpiAwarenessContext(DpiAwarenessContext 值);//虚拟枚举:DPI_AWARENESS_CONTEXT 是 *contextual*.//该值由 GetWindowDpiAwarenessContext() 或 GetThreadDpiAwarenessContext() 返回//并由 GetAwarenessFromDpiAwarenessContext() 完成.请参阅文档.枚举 DpiAwarenessContext{Context_Undefined = 0,Context_Unaware = -1,Context_SystemAware = -2,Context_PerMonitorAware = -3,Context_PerMonitorAwareV2 = -4,Context_UnawareGdiScaled = -5}由于 DPI 感知是基于线程的,因此这些设置可以应用于特定线程.这在重新设计用户界面以实现 DPI 感知时非常有用,可以让系统扩展不太重要的组件,同时专注于更重要的功能.SetThreadDpiAwarenessContext(与 SetProcessDpiAwarenessContext() 相同的参数)Assemblyinfo.cs如果引用 WPF 程序集的第三方/外部组件重新定义应用程序的 DPI 感知状态,则可以禁用此自动行为,在项目的 Assemblyinfo.cs 中插入一个参数:>[程序集:System.Windows.Media.DisableDpiAwareness]So we have this really odd issue. Our application is a C#/WinForms app. In our 6.0 release, our application is not DPI aware. In our 6.1 release it has suddenly become DPI aware.In the 6.0 release, if you run it in high DPI, it uses Windows bitmap scaling, which is fine, because that does not affect the screen layouts. In the 6.1 release, because it has for some reason become DPI aware, the user interfaces get mangled.We are not in a position to fix this right now. We have hundreds of screens, so making them all work properly in DPI aware mode will take a lot of time.We have confirmed this using SysInternals Process Explorer. In our 6.0 release, it shows Unaware, but in our 6.1 release, it initially shows Unaware, but then changes to System Aware.The latter occurs when the code enters from the EXE into our assembly DLL that contains all of our user interface code (our EXE is basically a very thin shell; all it really does is call a Controller class on our presentation layer assembly.)We have confirmed the following:Both versions are built in Release Mode using VSS 2017.Both versions target the same .NET Framework (4.5)Both versions use the same DevExpress version.Both versions have the same application manifest, which does not have the DPI awareness setting enabled.Neither versions have any calls to any DPI related Windows APIs.Using Sys Internals and some message boxes we determined at what point the 6.1 release becomes aware (entry point into Presentation assembly) and what DLLs are loaded at that point (ours, DevExpress, other dependencies), and then we built a small dummy app that references the same DLLs, and confirmed that these are loaded. That dummy app does not become DPI aware.We have compared the main csproj files between both releases and there are no meaningful differences.Neither release references anything from WPF.We don't understand why our 6.1 release has suddenly become DPI aware. We are clueless what else to look at and we need a fix that puts this release back to DPI unaware mode. It's holding up our release. Would really appreciate any pointers. We are willing to try anything at this point. 解决方案 About the problem reported in this Question:An application, which is DPI-unaware by design, relying on Windows virtualization to scale its UI content, suddenly (although after some modifications, leading to a minor release update) - and apparently without an observable reason - becomes DPI-Aware (System Aware).The application also relies on an interpretation of the app.manifest <windowsSettings>, where the absence of a DPI-awareness definition, defaults (for backward compatibility) to DPI-Unaware.There are no direct references to WPF assemblies and no DPI-related API calls.The application includes third-party components (and, possibly, external dependencies).Since DPI-Awareness has become a relevant aspect of UI presentation, given the diversity of screens resolutions available (and related DPI scaling settings), most component producers have adapted to High-DPI and their products are DPI-Aware (scale when a DPI change is detected) and make use of DPI-Aware assemblies (often referencing WPF assemblies, DPI-Aware by definition).When one of these DPI-Aware components is referenced in a project (directly or indirectly), a DPI-Unaware application will become DPI-Aware, when DPI-Awareness has not been disabled explicitly.The more direct (and recommended) method to declare an assembly DPI-Awareness, is to declare it explicitly in the application manifest.Refer to Hans Passant's answer for an application manifest setting prior to Visual Studio 2017:How to configure an app to run on a machine with a high DPI settingSince Visual Studio 2015-Upd.1, this setting is already present in app.manifest, it just needs to be uncommented. Set the section: <dpiAware>false</dpiAware>.<?xml version="1.0" encoding="utf-8"?><assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"> <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/> //(...) <!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher DPI. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config. --> <application xmlns="urn:schemas-microsoft-com:asm.v3"> <windowsSettings> <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">false</dpiAware> </windowsSettings> </application>//(...)</assembly>Refer to these MSDN articles for more information:High DPI desktop application development on WindowsSetting the default DPI awareness for a processAnother method is to set the process context DPI-Awareness using these Windows API functions:Windows 7SetProcessDPIAware[DllImport("user32.dll", SetLastError=true)]static extern bool SetProcessDPIAware();Windows 8.1SetProcessDpiAwareness[DllImport("shcore.dll")]static extern int SetProcessDpiAwareness(ProcessDPIAwareness value);enum ProcessDPIAwareness{ DPI_Unaware = 0, System_DPI_Aware = 1, Per_Monitor_DPI_Aware = 2}Windows 10, version 1703SetProcessDpiAwarenessContext()(When opting for a Per-Monitor DPI-Awareness, use Context_PerMonitorAwareV2)Also see: Mixed-Mode DPI Scaling and DPI-aware APIs - MSDNWindows 10, version 1809 (October 2018)A new DPI_AWARENESS_CONTEXT has been added: DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALEDDPI unaware with improved quality of GDI-based content. This modebehaves similarly to DPI_AWARENESS_CONTEXT_UNAWARE, but also enablesthe system to automatically improve the rendering quality of text andother GDI-based primitives when the window is displayed on a high-DPImonitor.Use the GetWindowDpiAwarenessContext() function to retrieve the DPI_AWARENESS_CONTEXT handle of a Window and GetThreadDpiAwarenessContext() for the DPI_AWARENESS_CONTEXT handle of the current thread. Then GetAwarenessFromDpiAwarenessContext() to retrive the DPI_AWARENESS value from the DPI_AWARENESS_CONTEXT structure.[DllImport("user32.dll", SetLastError=true)]static extern IntPtr GetWindowDpiAwarenessContext(IntPtr hWnd);[DllImport("user32.dll", SetLastError=true)]static extern IntPtr GetThreadDpiAwarenessContext();[DllImport("user32.dll", SetLastError=true)]static extern int GetAwarenessFromDpiAwarenessContext(IntPtr DPI_AWARENESS_CONTEXT);[DllImport("user32.dll", SetLastError=true)]static extern int SetProcessDpiAwarenessContext(DpiAwarenessContext value);// Virtual enumeration: DPI_AWARENESS_CONTEXT is *contextual*.// This value is returned by GetWindowDpiAwarenessContext() or GetThreadDpiAwarenessContext()// and finalized by GetAwarenessFromDpiAwarenessContext(). See the Docs.enum DpiAwarenessContext{ Context_Undefined = 0, Context_Unaware = -1, Context_SystemAware = -2, Context_PerMonitorAware = -3, Context_PerMonitorAwareV2 = -4, Context_UnawareGdiScaled = -5}Since DPI-Awareness is thread-based, these settings can be applied to a specific thread. This can be useful when redesigning a user interface to implement DPI-Awareness, to let the System scale a less important component while focusing on the more important functionalities.SetThreadDpiAwarenessContext(Same parameter as SetProcessDpiAwarenessContext())Assemblyinfo.csIf a third-party/external component, which references WPF assemblies, redefines the DPI-Awareness status of an application, this automatic behavior can be disabled, inserting a parameter in the Project's Assemblyinfo.cs:[assembly: System.Windows.Media.DisableDpiAwareness] 这篇关于DPI 意识 - 一个版本不知道,另一个版本是系统感知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!