简体   繁体   中英

Creating dpi aware C# clickonce application with WinForms

In our C# clickonce application we want to get the user's screen size (width, height). 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. That's when I noticed we have a DPI problem with certain Windows versions on high resolution screens.

I know that moving on to WPF would be a good option, but we are in need of a quick and dirty fix. 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:

<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:

<?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. 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. 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)
  • New Windows Forms application called "Test"
  • Enabled "ClickOnce security settings"
  • 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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