Category Archives: C#

Publishing DPI aware .net windows forms applications

In order to indicate that your Windows Forms application is DPI aware, you should set in application manifest something like:

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

However, once this is inserted in the application manifest, you can still publish your application, but you’ll be unable to install it from the published result.

The fix is quite easy, you’ll have to remove the configuration from application manifest and do it programatically at application startup:

static class Program
    {

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            if (Environment.OSVersion.Version.Major >= 6) SetProcessDPIAware();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern bool SetProcessDPIAware();
    }

Happy coding!