在WPF中可以透過主視窗來取得形變的比率
再乘上預設的DPI(96)即可
void GetDpiSetting(
out double DpiX, out double DpiY)
{
const double DEFAULT_DPI = 96.0;
/// get transform matrix from current main window
Matrix m = PresentationSource
.FromVisual(Application.Current.MainWindow)
.CompositionTarget.TransformToDevice;
/// scale default dpi
DpiX = m.M11 * DEFAULT_DPI;
DpiY = m.M22 * DEFAULT_DPI;
}
另外習慣使用Win32的程式設計師
一樣可以使用GetDeviceCaps取得DPI的設定
const int LOGPIXELSX = 88;
const int LOGPIXELSY = 90;
[DllImport("gdi32.dll")]
static extern int GetDeviceCaps(IntPtr hdc, int Index);
[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr Hwnd);
void GetDpiSetting(
out double DpiX, out double DpiY)
{
/// get desktop dc
IntPtr h = GetDC(IntPtr.Zero);
/// get dpi from dc
DpiX = GetDeviceCaps(h, LOGPIXELSX);
DpiY = GetDeviceCaps(h, LOGPIXELSY);
}
--
參考資料
Getting system DPI in WPF app
GetDeviceCaps Function
沒有留言:
張貼留言