首先添加引用:using System.Management;
[DllImport("user32.dll")] static extern IntPtr GetDC(IntPtr ptr); [DllImport("user32.dll", EntryPoint = "ReleaseDC")] public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc); [DllImport("gdi32.dll")] public static extern IntPtr CreateDC( string lpszDriver, // 驱动名称 string lpszDevice, // 设备名称 string lpszOutput, // 可设置为null Int64 lpInitData // optional PRinter data ); [DllImport("gdi32.dll")] public static extern int GetDeviceCaps( IntPtr hdc, // handle to DC int nIndex // index of capability ); [DllImport("user32.dll")] internal static extern bool SetProcessDPIAware(); const int LOGPIXELSX = 88; const int LOGPIXELSY = 90;//在方法中添加如下代码进行获取SetProcessDPIAware(); //这句很重要 IntPtr screenDC = GetDC(IntPtr.Zero); int dpi_x = GetDeviceCaps(screenDC, LOGPIXELSX);//96 是100%、120 是125% int dpi_y = GetDeviceCaps(screenDC,LOGPIXELSY); ReleaseDC(IntPtr.Zero, screenDC);
新闻热点
疑难解答