using System; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; namespace Dongke.WinForm.Controls.InvoiceLayout { public class DipClass { [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); const int HORZRES = 8; const int VERTRES = 10; const int LOGPIXELSX = 88; const int LOGPIXELSY = 90; const int DESKTOPVERTRES = 117; const int DESKTOPHORZRES = 118; /// /// 当前系统DPI_X 大小 一般为96 /// public static int DpiX { get { IntPtr hdc = GetDC(IntPtr.Zero); int dpiX = GetDeviceCaps(hdc, LOGPIXELSX); ReleaseDC(IntPtr.Zero, hdc); return dpiX; } } /// /// 当前系统DPI_Y 大小 一般为96 /// public static int DpiY { get { IntPtr hdc = GetDC(IntPtr.Zero); int dpiY = GetDeviceCaps(hdc, LOGPIXELSY); ReleaseDC(IntPtr.Zero, hdc); return dpiY; } } /// /// 当前系统DPI_XY 大小 一般为96 /// public static Point Dpi { get { IntPtr hdc = GetDC(IntPtr.Zero); int dpiX = GetDeviceCaps(hdc, LOGPIXELSX); int dpiY = GetDeviceCaps(hdc, LOGPIXELSY); ReleaseDC(IntPtr.Zero, hdc); return new Point(dpiX, dpiY); } } /// /// 获取宽度缩放百分比 /// public static float ScaleX { get { IntPtr hdc = GetDC(IntPtr.Zero); int t = GetDeviceCaps(hdc, DESKTOPHORZRES); int d = GetDeviceCaps(hdc, HORZRES); ReleaseDC(IntPtr.Zero, hdc); return (float)t / d; } } /// /// 获取高度缩放百分比 /// public static float ScaleY { get { IntPtr hdc = GetDC(IntPtr.Zero); int t = GetDeviceCaps(hdc, DESKTOPVERTRES); int d = GetDeviceCaps(hdc, VERTRES); ReleaseDC(IntPtr.Zero, hdc); return (float)t / d; } } /// /// 当前系统DPI_XY 大小 一般为96 /// public static PointF Scale { get { IntPtr hdc = GetDC(IntPtr.Zero); int t1 = GetDeviceCaps(hdc, DESKTOPHORZRES); int d1 = GetDeviceCaps(hdc, HORZRES); int t2 = GetDeviceCaps(hdc, DESKTOPVERTRES); int d2 = GetDeviceCaps(hdc, VERTRES); ReleaseDC(IntPtr.Zero, hdc); return new PointF((float)t1 / d1, (float)t2 / d2); } } public static Rectangle ToScale(Rectangle rect) { IntPtr hdc = GetDC(IntPtr.Zero); int t1 = GetDeviceCaps(hdc, DESKTOPHORZRES); int d1 = GetDeviceCaps(hdc, HORZRES); int t2 = GetDeviceCaps(hdc, DESKTOPVERTRES); int d2 = GetDeviceCaps(hdc, VERTRES); if (t1 != d1) { float sx = (float)t1 / d1; rect.X = (int)(rect.X * sx); rect.Width = (int)(rect.Width * sx); } if (t2 != d2) { float sy = (float)t2 / d2; rect.Y = (int)(rect.Y * sy); rect.Height = (int)(rect.Height * sy); } return rect; } public static RectangleF ToScale(RectangleF rect) { IntPtr hdc = GetDC(IntPtr.Zero); int t1 = GetDeviceCaps(hdc, DESKTOPHORZRES); int d1 = GetDeviceCaps(hdc, HORZRES); int t2 = GetDeviceCaps(hdc, DESKTOPVERTRES); int d2 = GetDeviceCaps(hdc, VERTRES); if (t1 != d1) { float sx = (float)t1 / d1; rect.X *= sx; rect.Width *= sx; } if (t2 != d2) { float sy = (float)t2 / d2; rect.Y *= sy; rect.Height *= sy; } return rect; } /// /// 多屏 /// /// /// /// public static Rectangle ToScale2(Control ctl, Rectangle rect) { Screen cs = Screen.FromControl(ctl); //Rectangle rw = cs.WorkingArea; //Rectangle bb = cs.Bounds; DEVMODE dm = new DEVMODE { dmSize = (short)Marshal.SizeOf(typeof(DEVMODE)) }; EnumDisplaySettings(cs.DeviceName, ENUM_CURRENT_SETTINGS, ref dm); int t1 = dm.dmPelsWidth; int d1 = cs.Bounds.Width; int t2 = dm.dmPelsHeight; int d2 = cs.Bounds.Height; /* IntPtr hdc = GetDC(ctl.Handle); int t1 = GetDeviceCaps(hdc, DESKTOPHORZRES); int d1 = GetDeviceCaps(hdc, HORZRES); int t2 = GetDeviceCaps(hdc, DESKTOPVERTRES); int d2 = GetDeviceCaps(hdc, VERTRES); */ if (t1 != d1) { float sx = (float)t1 / d1; rect.X = (int)(rect.X * sx); rect.Width = (int)(rect.Width * sx); } if (t2 != d2) { float sy = (float)t2 / d2; rect.Y = (int)(rect.Y * sy); rect.Height = (int)(rect.Height * sy); } return rect; } const int ENUM_CURRENT_SETTINGS = -1; [DllImport("user32.dll")] public static extern bool EnumDisplaySettings(string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode); [StructLayout(LayoutKind.Sequential)] public struct DEVMODE { private const int CCHDEVICENAME = 0x20; private const int CCHFORMNAME = 0x20; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)] public string dmDeviceName; public short dmSpecVersion; public short dmDriverVersion; public short dmSize; public short dmDriverExtra; public int dmFields; public int dmPositionX; public int dmPositionY; public ScreenOrientation dmDisplayOrientation; public int dmDisplayFixedOutput; public short dmColor; public short dmDuplex; public short dmYResolution; public short dmTTOption; public short dmCollate; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)] public string dmFormName; public short dmLogPixels; public int dmBitsPerPel; public int dmPelsWidth; public int dmPelsHeight; public int dmDisplayFlags; public int dmDisplayFrequency; public int dmICMMethod; public int dmICMIntent; public int dmMediaType; public int dmDitherType; public int dmReserved1; public int dmReserved2; public int dmPanningWidth; public int dmPanningHeight; } } }