Created
April 4, 2017 22:32
-
-
Save emoacht/f3db6960fcd19835c0eaef602655773b to your computer and use it in GitHub Desktop.
Enable Non-Client Area DPI scaling in WinForms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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