Skip to content

Instantly share code, notes, and snippets.

@emoacht emoacht/MainForm.cs
Created Apr 4, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.