我一直试图禁用ClickOnce应用程序上的DPI感知。
我很快发现,不可能在清单中指定它,因为ClickOnce不支持清单文件中的asm.v3。
我发现的下一个选项是调用新的SetProcessDpiAwareness函数。
根据这教程,
在创建应用程序窗口之前调用SetProcessDpiAwareness。
和这教程,
您必须在任何SetProcessDpiAwareness调用之前调用Win32API
你必须尽早调用这个函数。因此,为了进行测试,我创建了一个完全空白的WPF应用程序,并使其成为我的整个App类:
[DllImport("SHCore.dll", SetLastError = true)]
private static extern bool SetProcessDpiAwareness(PROCESS_DPI_AWARENESS awareness);
[DllImport("SHCore.dll", SetLastError = true)]
private static extern void GetProcessDpiAwareness(IntPtr hprocess, out PROCESS_DPI_AWARENESS awareness);
private enum PROCESS_DPI_AWARENESS
{
Process_DPI_Unaware = 0,
Process_System_DPI_Aware = 1,
Process_Per_Monitor_DPI_Aware = 2
}
static App()
{
var result = SetProcessDpiAwareness(PROCESS_DPI_AWARENESS.Process_DPI_Unaware);
var setDpiError = Marshal.GetLastWin32Error();
MessageBox.Show("Dpi set: " + result.ToString());
PROCESS_DPI_AWARENESS awareness;
GetProcessDpiAwareness(Process.GetCurrentProcess().Handle, out awareness);
var getDpiError = Marshal.GetLastWin32Error();
MessageBox.Show(awareness.ToString());
MessageBox.Show("Set DPI error: " + new Win32Exception(setDpiError).ToString());
MessageBox.Show("Get DPI error: " + new Win32Exception(getDpiError).ToString());
}3个消息框显示了以下内容:
新闻部集:真 Process_System_DPI_Aware 设置DPI错误: System.ComponentModel.Win32Exception (0x80004005):拒绝访问 System.ComponentModel.Win32Exception (0x80004005):操作成功完成
为什么应用程序仍然设置为DPI_Aware?打电话还不够早吗?
应用程序确实经历了新闻部的缩放。
当我使用清单定义时:
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">false</dpiAware>
</windowsSettings>
</application>它确实返回Process_DPI_Unaware。
编辑1:
现在直接在Marshal.GetLastWin32Error方法之后抓取pInvoke (),这实际上返回了一个错误。
发布于 2015-08-21 20:14:54
小心SetLastError和GetLastWin32Error,任何MessageBox.Show之间的调用都会影响其结果。请确保在调用本机方法后始终得到最后一个错误。
因此,您可以很好地得到预期的行为,但是被错误代码误导了。
请参阅这篇博文以获得完整的解释:http://blogs.msdn.com/b/oldnewthing/archive/2015/08/19/10636096.aspx
编辑
不太清楚是什么导致访问被拒绝的...but,有一个简单而有效的技巧,使新闻部无法意识到:
编辑您的AssemblyInfo.cs并添加以下内容:
[assembly: DisableDpiAwareness]资料来源:https://code.msdn.microsoft.com/windowsdesktop/Per-Monitor-Aware-WPF-e43cde33 (PerMonitorAwareWPFWindow.xaml.cs中的评论)
https://stackoverflow.com/questions/32148151
复制相似问题