DipClass.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using System;
  2. using System.Drawing;
  3. using System.Runtime.InteropServices;
  4. using System.Windows.Forms;
  5. namespace Dongke.WinForm.Controls.InvoiceLayout
  6. {
  7. public class DipClass
  8. {
  9. [DllImport("user32.dll")]
  10. static extern IntPtr GetDC(IntPtr ptr);
  11. [DllImport("gdi32.dll")]
  12. static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
  13. [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
  14. static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
  15. const int HORZRES = 8;
  16. const int VERTRES = 10;
  17. const int LOGPIXELSX = 88;
  18. const int LOGPIXELSY = 90;
  19. const int DESKTOPVERTRES = 117;
  20. const int DESKTOPHORZRES = 118;
  21. /// <summary)
  22. /// 获取屏幕分辨率当前物理大小
  23. /// </summary)
  24. public static Size WorkingArea
  25. {
  26. get
  27. {
  28. IntPtr hdc = GetDC(IntPtr.Zero);
  29. Size size = new Size
  30. {
  31. Width = GetDeviceCaps(hdc, HORZRES),
  32. Height = GetDeviceCaps(hdc, VERTRES)
  33. };
  34. ReleaseDC(IntPtr.Zero, hdc);
  35. return size;
  36. }
  37. }
  38. /// <summary>
  39. /// 当前系统DPI_X 大小 一般为96
  40. /// </summary>
  41. public static int DpiX
  42. {
  43. get
  44. {
  45. IntPtr hdc = GetDC(IntPtr.Zero);
  46. int dpiX = GetDeviceCaps(hdc, LOGPIXELSX);
  47. ReleaseDC(IntPtr.Zero, hdc);
  48. return dpiX;
  49. }
  50. }
  51. /// <summary>
  52. /// 当前系统DPI_Y 大小 一般为96
  53. /// </summary>
  54. public static int DpiY
  55. {
  56. get
  57. {
  58. IntPtr hdc = GetDC(IntPtr.Zero);
  59. int dpiY = GetDeviceCaps(hdc, LOGPIXELSY);
  60. ReleaseDC(IntPtr.Zero, hdc);
  61. return dpiY;
  62. }
  63. }
  64. /// <summary>
  65. /// 当前系统DPI_XY 大小 一般为96
  66. /// </summary>
  67. public static Point Dpi
  68. {
  69. get
  70. {
  71. IntPtr hdc = GetDC(IntPtr.Zero);
  72. int dpiX = GetDeviceCaps(hdc, LOGPIXELSX);
  73. int dpiY = GetDeviceCaps(hdc, LOGPIXELSY);
  74. ReleaseDC(IntPtr.Zero, hdc);
  75. return new Point(dpiX, dpiY);
  76. }
  77. }
  78. /// <summary)
  79. /// 获取真实设置的桌面分辨率大小
  80. /// </summary)
  81. public static Size Desktop
  82. {
  83. get
  84. {
  85. IntPtr hdc = GetDC(IntPtr.Zero);
  86. Size size = new Size
  87. {
  88. Width = GetDeviceCaps(hdc, DESKTOPHORZRES),
  89. Height = GetDeviceCaps(hdc, DESKTOPVERTRES)
  90. };
  91. ReleaseDC(IntPtr.Zero, hdc);
  92. return size;
  93. }
  94. }
  95. /// <summary>
  96. /// 获取宽度缩放百分比
  97. /// </summary>
  98. public static float ScaleX
  99. {
  100. get
  101. {
  102. IntPtr hdc = GetDC(IntPtr.Zero);
  103. int t = GetDeviceCaps(hdc, DESKTOPHORZRES);
  104. int d = GetDeviceCaps(hdc, HORZRES);
  105. ReleaseDC(IntPtr.Zero, hdc);
  106. return (float)t / d;
  107. }
  108. }
  109. /// <summary>
  110. /// 获取高度缩放百分比
  111. /// </summary>
  112. public static float ScaleY
  113. {
  114. get
  115. {
  116. IntPtr hdc = GetDC(IntPtr.Zero);
  117. int t = GetDeviceCaps(hdc, DESKTOPVERTRES);
  118. int d = GetDeviceCaps(hdc, VERTRES);
  119. ReleaseDC(IntPtr.Zero, hdc);
  120. return (float)t / d;
  121. }
  122. }
  123. /// <summary>
  124. /// 当前系统DPI_XY 大小 一般为96
  125. /// </summary>
  126. public static PointF Scale
  127. {
  128. get
  129. {
  130. IntPtr hdc = GetDC(IntPtr.Zero);
  131. int t1 = GetDeviceCaps(hdc, DESKTOPHORZRES);
  132. int d1 = GetDeviceCaps(hdc, HORZRES);
  133. int t2 = GetDeviceCaps(hdc, DESKTOPVERTRES);
  134. int d2 = GetDeviceCaps(hdc, VERTRES);
  135. ReleaseDC(IntPtr.Zero, hdc);
  136. return new PointF((float)t1 / d1, (float)t2 / d2);
  137. }
  138. }
  139. public static Rectangle ToScale(Rectangle rect)
  140. {
  141. IntPtr hdc = GetDC(IntPtr.Zero);
  142. int t1 = GetDeviceCaps(hdc, DESKTOPHORZRES);
  143. int d1 = GetDeviceCaps(hdc, HORZRES);
  144. int t2 = GetDeviceCaps(hdc, DESKTOPVERTRES);
  145. int d2 = GetDeviceCaps(hdc, VERTRES);
  146. if (t1 != d1)
  147. {
  148. float sx = (float)t1 / d1;
  149. rect.X = (int)(rect.X * sx);
  150. rect.Width = (int)(rect.Width * sx);
  151. }
  152. if (t2 != d2)
  153. {
  154. float sy = (float)t2 / d2;
  155. rect.Y = (int)(rect.Y * sy);
  156. rect.Height = (int)(rect.Height * sy);
  157. }
  158. return rect;
  159. }
  160. public static RectangleF ToScale(RectangleF rect)
  161. {
  162. IntPtr hdc = GetDC(IntPtr.Zero);
  163. int t1 = GetDeviceCaps(hdc, DESKTOPHORZRES);
  164. int d1 = GetDeviceCaps(hdc, HORZRES);
  165. int t2 = GetDeviceCaps(hdc, DESKTOPVERTRES);
  166. int d2 = GetDeviceCaps(hdc, VERTRES);
  167. if (t1 != d1)
  168. {
  169. float sx = (float)t1 / d1;
  170. rect.X *= sx;
  171. rect.Width *= sx;
  172. }
  173. if (t2 != d2)
  174. {
  175. float sy = (float)t2 / d2;
  176. rect.Y *= sy;
  177. rect.Height *= sy;
  178. }
  179. return rect;
  180. }
  181. /// <summary>
  182. /// 多屏
  183. /// </summary>
  184. /// <param name="ctl"></param>
  185. /// <param name="rect"></param>
  186. /// <returns></returns>
  187. public static Rectangle ToScale2(Control ctl, Rectangle rect)
  188. {
  189. Screen cs = Screen.FromControl(ctl);
  190. //Rectangle rw = cs.WorkingArea;
  191. //Rectangle bb = cs.Bounds;
  192. DEVMODE dm = new DEVMODE
  193. {
  194. dmSize = (short)Marshal.SizeOf(typeof(DEVMODE))
  195. };
  196. EnumDisplaySettings(cs.DeviceName, ENUM_CURRENT_SETTINGS, ref dm);
  197. int t1 = dm.dmPelsWidth;
  198. int d1 = cs.Bounds.Width;
  199. int t2 = dm.dmPelsHeight;
  200. int d2 = cs.Bounds.Height;
  201. /*
  202. IntPtr hdc = GetDC(ctl.Handle);
  203. int t1 = GetDeviceCaps(hdc, DESKTOPHORZRES);
  204. int d1 = GetDeviceCaps(hdc, HORZRES);
  205. int t2 = GetDeviceCaps(hdc, DESKTOPVERTRES);
  206. int d2 = GetDeviceCaps(hdc, VERTRES);
  207. */
  208. if (t1 != d1)
  209. {
  210. float sx = (float)t1 / d1;
  211. rect.X = (int)(rect.X * sx);
  212. rect.Width = (int)(rect.Width * sx);
  213. }
  214. if (t2 != d2)
  215. {
  216. float sy = (float)t2 / d2;
  217. rect.Y = (int)(rect.Y * sy);
  218. rect.Height = (int)(rect.Height * sy);
  219. }
  220. return rect;
  221. }
  222. const int ENUM_CURRENT_SETTINGS = -1;
  223. [DllImport("user32.dll")]
  224. public static extern bool EnumDisplaySettings(string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode);
  225. [StructLayout(LayoutKind.Sequential)]
  226. public struct DEVMODE
  227. {
  228. private const int CCHDEVICENAME = 0x20;
  229. private const int CCHFORMNAME = 0x20;
  230. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
  231. public string dmDeviceName;
  232. public short dmSpecVersion;
  233. public short dmDriverVersion;
  234. public short dmSize;
  235. public short dmDriverExtra;
  236. public int dmFields;
  237. public int dmPositionX;
  238. public int dmPositionY;
  239. public ScreenOrientation dmDisplayOrientation;
  240. public int dmDisplayFixedOutput;
  241. public short dmColor;
  242. public short dmDuplex;
  243. public short dmYResolution;
  244. public short dmTTOption;
  245. public short dmCollate;
  246. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
  247. public string dmFormName;
  248. public short dmLogPixels;
  249. public int dmBitsPerPel;
  250. public int dmPelsWidth;
  251. public int dmPelsHeight;
  252. public int dmDisplayFlags;
  253. public int dmDisplayFrequency;
  254. public int dmICMMethod;
  255. public int dmICMIntent;
  256. public int dmMediaType;
  257. public int dmDitherType;
  258. public int dmReserved1;
  259. public int dmReserved2;
  260. public int dmPanningWidth;
  261. public int dmPanningHeight;
  262. }
  263. }
  264. }