using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Windows; namespace DotNet.RevitUI.MVVM.Extension { public static class WindowExtension { /// /// 非模态显示窗体,并指定此窗体的宿主窗体. /// /// The main. /// The parent. public static void Show(this Window main, IntPtr parent) { if (parent == IntPtr.Zero) { parent = Process.GetCurrentProcess().MainWindowHandle; } var mainHelper = new System.Windows.Interop.WindowInteropHelper(main) { Owner = parent }; // self-adaption if (Math.Abs(PrimaryScreen.ScaleX - 1.0) >= 1e-7) { main.Width = main.Width * PrimaryScreen.ScaleX; main.Height = main.Height * PrimaryScreen.ScaleX; } main.Show(); } /// /// 模态显示窗体,并指定此窗体的宿主窗体. /// public static void ShowDialog(this Window main, IntPtr parent) { if (parent == IntPtr.Zero) { parent = Process.GetCurrentProcess().MainWindowHandle; } var mainHelper = new System.Windows.Interop.WindowInteropHelper(main) { Owner = parent }; // self-adaption if (Math.Abs(PrimaryScreen.ScaleX - 1.0) >= 1e-7) { main.Width = main.Width * PrimaryScreen.ScaleX; main.Height = main.Height * PrimaryScreen.ScaleX; } main.ShowDialog(); } /// /// 获取WPF窗口句柄. /// /// The main. /// public static IntPtr GetHandle(this Window main) { var helper = new System.Windows.Interop.WindowInteropHelper(main); #if !NET35 if(helper.Handle==IntPtr.Zero) { return helper.EnsureHandle(); } #endif return helper.Handle; } } class PrimaryScreen { #region Win32 API [DllImport("user32.dll")] static extern IntPtr GetDC(IntPtr ptr); [DllImport("gdi32.dll")] static extern int GetDeviceCaps(IntPtr hdc, int nIndex); [DllImport("user32.dll", EntryPoint = "ReleaseDC")] static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc); #endregion #region DeviceCaps常量 const int HORZRES = 8; const int VERTRES = 10; const int LOGPIXELSX = 88; const int LOGPIXELSY = 90; const int DESKTOPVERTRES = 117; const int DESKTOPHORZRES = 118; #endregion #region 属性 /// /// 获取屏幕分辨率当前物理大小 /// public static Size WorkingArea { get { var hdc = GetDC(IntPtr.Zero); var size = new Size { Width = GetDeviceCaps(hdc, HORZRES), Height = GetDeviceCaps(hdc, VERTRES) }; ReleaseDC(IntPtr.Zero, hdc); return size; } } /// /// 当前系统DPI_X 大小 一般为96 /// public static int DpiX { get { var hdc = GetDC(IntPtr.Zero); var DpiX = GetDeviceCaps(hdc, LOGPIXELSX); ReleaseDC(IntPtr.Zero, hdc); return DpiX; } } /// /// 当前系统DPI_Y 大小 一般为96 /// public static int DpiY { get { var hdc = GetDC(IntPtr.Zero); var DpiX = GetDeviceCaps(hdc, LOGPIXELSY); ReleaseDC(IntPtr.Zero, hdc); return DpiX; } } /// /// 获取真实设置的桌面分辨率大小 /// public static Size DESKTOP { get { var hdc = GetDC(IntPtr.Zero); var size = new Size(); size.Width = GetDeviceCaps(hdc, DESKTOPHORZRES); size.Height = GetDeviceCaps(hdc, DESKTOPVERTRES); ReleaseDC(IntPtr.Zero, hdc); return size; } } /// /// 获取宽度缩放百分比 /// public static float ScaleX { get { var hdc = GetDC(IntPtr.Zero); var t = GetDeviceCaps(hdc, DESKTOPHORZRES); var d = GetDeviceCaps(hdc, HORZRES); var ScaleX = (float)GetDeviceCaps(hdc, DESKTOPHORZRES) / (float)GetDeviceCaps(hdc, HORZRES); ReleaseDC(IntPtr.Zero, hdc); return ScaleX; } } /// /// 获取高度缩放百分比 /// public static float ScaleY { get { var hdc = GetDC(IntPtr.Zero); var scaleY = (float)GetDeviceCaps(hdc, DESKTOPVERTRES) / (float)GetDeviceCaps(hdc, VERTRES); ReleaseDC(IntPtr.Zero, hdc); return scaleY; } } #endregion } }