首先添加引用: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);
新聞熱點
疑難解答