| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- 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;
- /// <summary)
- /// 获取屏幕分辨率当前物理大小
- /// </summary)
- public static Size WorkingArea
- {
- get
- {
- IntPtr hdc = GetDC(IntPtr.Zero);
- Size size = new Size
- {
- Width = GetDeviceCaps(hdc, HORZRES),
- Height = GetDeviceCaps(hdc, VERTRES)
- };
- ReleaseDC(IntPtr.Zero, hdc);
- return size;
- }
- }
- /// <summary>
- /// 当前系统DPI_X 大小 一般为96
- /// </summary>
- public static int DpiX
- {
- get
- {
- IntPtr hdc = GetDC(IntPtr.Zero);
- int dpiX = GetDeviceCaps(hdc, LOGPIXELSX);
- ReleaseDC(IntPtr.Zero, hdc);
- return dpiX;
- }
- }
- /// <summary>
- /// 当前系统DPI_Y 大小 一般为96
- /// </summary>
- public static int DpiY
- {
- get
- {
- IntPtr hdc = GetDC(IntPtr.Zero);
- int dpiY = GetDeviceCaps(hdc, LOGPIXELSY);
- ReleaseDC(IntPtr.Zero, hdc);
- return dpiY;
- }
- }
- /// <summary>
- /// 当前系统DPI_XY 大小 一般为96
- /// </summary>
- 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);
- }
- }
- /// <summary)
- /// 获取真实设置的桌面分辨率大小
- /// </summary)
- public static Size Desktop
- {
- get
- {
- IntPtr hdc = GetDC(IntPtr.Zero);
- Size size = new Size
- {
- Width = GetDeviceCaps(hdc, DESKTOPHORZRES),
- Height = GetDeviceCaps(hdc, DESKTOPVERTRES)
- };
- ReleaseDC(IntPtr.Zero, hdc);
- return size;
- }
- }
- /// <summary>
- /// 获取宽度缩放百分比
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 获取高度缩放百分比
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 当前系统DPI_XY 大小 一般为96
- /// </summary>
- 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;
- }
- /// <summary>
- /// 多屏
- /// </summary>
- /// <param name="ctl"></param>
- /// <param name="rect"></param>
- /// <returns></returns>
- 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;
- }
- }
- }
|