Skip to content

Instantly share code, notes, and snippets.

@emoacht
Created April 4, 2017 22:32
Show Gist options
  • Save emoacht/f3db6960fcd19835c0eaef602655773b to your computer and use it in GitHub Desktop.
Save emoacht/f3db6960fcd19835c0eaef602655773b to your computer and use it in GitHub Desktop.
Enable Non-Client Area DPI scaling in WinForms
// app.manifest which declares this application Monitor DPI aware is required.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
[DllImport("User32.dll", SetLastError = true)]
private static extern bool EnableNonClientDpiScaling(IntPtr hwnd);
private const int WM_NCCREATE = 0x0081;
private const int WM_DPICHANGED = 0x02E0;
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_NCCREATE:
var result = EnableNonClientDpiScaling(this.Handle);
Debug.WriteLine($"{nameof(EnableNonClientDpiScaling)} -> {result}");
break;
case WM_DPICHANGED:
Debug.WriteLine($"DPI Changed!");
break;
}
base.WndProc(ref m);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment