简体   繁体   English

使用WinForms创建dpi感知C#clickonce应用程序

[英]Creating dpi aware C# clickonce application with WinForms

In our C# clickonce application we want to get the user's screen size (width, height). 在我们的C#clickonce应用程序中,我们希望获得用户的屏幕大小(宽度,高度)。 At first everything worked fine with 起初一切都很好

int width = Screen.FromControl(this).Bounds.Width;
int height= Screen.FromControl(this).Bounds.Height;

Later on I noticed that on our Windows 8.1 laptop the values returned are 1.25 times less than the real dimensions of the screen. 后来我注意到,在我们的Windows 8.1笔记本电脑上,返回的值比屏幕的实际尺寸小1.25倍。 That's when I noticed we have a DPI problem with certain Windows versions on high resolution screens. 就在那时我注意到我们在高分辨率屏幕上遇到某些Windows版本的DPI问题。

I know that moving on to WPF would be a good option, but we are in need of a quick and dirty fix. 我知道继续使用WPF将是一个不错的选择,但我们需要快速而肮脏的修复。 After searching by Google I found several approaches as described here and I decided to go with this solution. 经过谷歌搜索后,我发现了这里描述的几种方法,我决定采用这种解决方案。 After all, even the MSDN Blog entries suggest to add the following snippet to the app.manifest: 毕竟,即使MSDN博客条目建议将以下代码段添加到app.manifest:

<asmv3:application xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
    <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
        <dpiAware>true</dpiAware>
    </asmv3:windowsSettings>
</asmv3:application>

Unfortunately I do not understand how and where to add this code. 不幸的是,我不明白如何以及在何处添加此代码。 My manifest (automatically generated by Visual Studio) looks like this: 我的清单(由Visual Studio自动生成)如下所示:

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
        <security>
            <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
                <requestedExecutionLevel level="asInvoker" uiAccess="false" />
            </requestedPrivileges>
        </security>
    </trustInfo>   
    <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
        <application>
        </application>
    </compatibility> 
</asmv1:assembly>

Note that I have removed commented parts to make the app manifest look smaller here. 请注意,我删除了注释部分,以使应用清单看起来更小。 I have tried adding the snippet from above before </asm1:assembly> on the last line. 我尝试在最后一行添加</asm1:assembly>之前的上面的代码片段。 Whenever I tried this, I get an exception before my application starts: 每当我尝试这个时,我在应用程序启动之前都会遇到异常:

An unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dll.
Additional information: Value cannot be null.

Oddly, someone else on stackoverflow.com suggested to uncheck Enable clickonce security settings in the app settings. 奇怪的是,stackoverflow.com上的其他人建议取消选中应用设置中的Enable clickonce security settings设置。 This made the application start again and it worked on my laptop as expected: The app was upscaled and the text sharp. 这使得应用程序重新开始,它按照预期在我的笔记本电脑上工作:应用程序已升级,文本清晰。 Also, I was able to get the correct screen size in pixels through my code. 此外,通过我的代码,我能够以像素为单位获得正确的屏幕尺寸。

Of course disabling the security settings is not an option if you want to deploy an application. 当然,如果要部署应用程序,则无法禁用安全设置。 So how can I fix this problem in order to get what I want? 那么如何解决这个问题才能得到我想要的东西? What am I missing? 我错过了什么? I am totally lost at this point. 我完全迷失在这一点上。


Trying to start fresh: 试着重新开始:

  • Opened Visual Studio (2013 Pro) 打开Visual Studio(2013 Pro)
  • New Windows Forms application called "Test" 新的Windows窗体应用程序称为“测试”
  • Enabled "ClickOnce security settings" 启用“ClickOnce安全设置”
  • Edited the manifest file again as suggest on other posts like here 再次编辑清单文件作为此处其他帖子的建议
  • Clicked on Start (or Build) 点击开始(或构建)

The following error appears. 出现以下错误。 I don't know how to get more information about the error, becuase there is none... 我不知道如何获得有关错误的更多信息,因为没有...

在此输入图像描述

在此输入图像描述

Maybe it is too late for answer, but according to the answer of the same problem 也许现在回答太迟了,但根据同样问题的答案

Project Options > Debug > Enable the Visual Studio hosting process(disable it run app later enable it again) 项目选项>调试>启用Visual Studio托管过程(稍后禁用它运行应用程序再次启用它)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM