win32.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-08-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="win32.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Drawing;
  19. using System.Linq;
  20. using System.Runtime.InteropServices;
  21. using System.Text;
  22. namespace HZH_Controls.Controls
  23. {
  24. /// <summary>
  25. /// Class win32.
  26. /// </summary>
  27. public class win32
  28. {
  29. /// <summary>
  30. /// The wm mousemove
  31. /// </summary>
  32. public const int WM_MOUSEMOVE = 0x0200;
  33. /// <summary>
  34. /// The wm lbuttondown
  35. /// </summary>
  36. public const int WM_LBUTTONDOWN = 0x0201;
  37. /// <summary>
  38. /// The wm lbuttonup
  39. /// </summary>
  40. public const int WM_LBUTTONUP = 0x0202;
  41. /// <summary>
  42. /// The wm rbuttondown
  43. /// </summary>
  44. public const int WM_RBUTTONDOWN = 0x0204;
  45. /// <summary>
  46. /// The wm lbuttondblclk
  47. /// </summary>
  48. public const int WM_LBUTTONDBLCLK = 0x0203;
  49. /// <summary>
  50. /// The wm mouseleave
  51. /// </summary>
  52. public const int WM_MOUSELEAVE = 0x02A3;
  53. /// <summary>
  54. /// The wm paint
  55. /// </summary>
  56. public const int WM_PAINT = 0x000F;
  57. /// <summary>
  58. /// The wm erasebkgnd
  59. /// </summary>
  60. public const int WM_ERASEBKGND = 0x0014;
  61. /// <summary>
  62. /// The wm print
  63. /// </summary>
  64. public const int WM_PRINT = 0x0317;
  65. //const int EN_HSCROLL = 0x0601;
  66. //const int EN_VSCROLL = 0x0602;
  67. /// <summary>
  68. /// The wm hscroll
  69. /// </summary>
  70. public const int WM_HSCROLL = 0x0114;
  71. /// <summary>
  72. /// The wm vscroll
  73. /// </summary>
  74. public const int WM_VSCROLL = 0x0115;
  75. /// <summary>
  76. /// The em getsel
  77. /// </summary>
  78. public const int EM_GETSEL = 0x00B0;
  79. /// <summary>
  80. /// The em lineindex
  81. /// </summary>
  82. public const int EM_LINEINDEX = 0x00BB;
  83. /// <summary>
  84. /// The em linefromchar
  85. /// </summary>
  86. public const int EM_LINEFROMCHAR = 0x00C9;
  87. /// <summary>
  88. /// The em posfromchar
  89. /// </summary>
  90. public const int EM_POSFROMCHAR = 0x00D6;
  91. /// <summary>
  92. /// Posts the message.
  93. /// </summary>
  94. /// <param name="hwnd">The HWND.</param>
  95. /// <param name="msg">The MSG.</param>
  96. /// <param name="wParam">The w parameter.</param>
  97. /// <param name="lParam">The l parameter.</param>
  98. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  99. [DllImport("USER32.DLL", EntryPoint = "PostMessage")]
  100. public static extern bool PostMessage(IntPtr hwnd, uint msg,
  101. IntPtr wParam, IntPtr lParam);
  102. /*
  103. BOOL PostMessage( HWND hWnd,
  104. UINT Msg,
  105. WPARAM wParam,
  106. LPARAM lParam
  107. );
  108. */
  109. // Put this declaration in your class //IntPtr
  110. /// <summary>
  111. /// Sends the message.
  112. /// </summary>
  113. /// <param name="hwnd">The HWND.</param>
  114. /// <param name="msg">The MSG.</param>
  115. /// <param name="wParam">The w parameter.</param>
  116. /// <param name="lParam">The l parameter.</param>
  117. /// <returns>System.Int32.</returns>
  118. [DllImport("USER32.DLL", EntryPoint = "SendMessage")]
  119. public static extern int SendMessage(IntPtr hwnd, int msg, IntPtr wParam,
  120. IntPtr lParam);
  121. /// <summary>
  122. /// Gets the caret blink time.
  123. /// </summary>
  124. /// <returns>System.UInt32.</returns>
  125. [DllImport("USER32.DLL", EntryPoint = "GetCaretBlinkTime")]
  126. public static extern uint GetCaretBlinkTime();
  127. /// <summary>
  128. /// The wm printclient
  129. /// </summary>
  130. const int WM_PRINTCLIENT = 0x0318;
  131. /// <summary>
  132. /// The PRF checkvisible
  133. /// </summary>
  134. const long PRF_CHECKVISIBLE = 0x00000001L;
  135. /// <summary>
  136. /// The PRF nonclient
  137. /// </summary>
  138. const long PRF_NONCLIENT = 0x00000002L;
  139. /// <summary>
  140. /// The PRF client
  141. /// </summary>
  142. const long PRF_CLIENT = 0x00000004L;
  143. /// <summary>
  144. /// The PRF erasebkgnd
  145. /// </summary>
  146. const long PRF_ERASEBKGND = 0x00000008L;
  147. /// <summary>
  148. /// The PRF children
  149. /// </summary>
  150. const long PRF_CHILDREN = 0x00000010L;
  151. /// <summary>
  152. /// The PRF owned
  153. /// </summary>
  154. const long PRF_OWNED = 0x00000020L;
  155. /* Will clean this up later doing something like this
  156. enum CaptureOptions : long
  157. {
  158. PRF_CHECKVISIBLE= 0x00000001L,
  159. PRF_NONCLIENT = 0x00000002L,
  160. PRF_CLIENT = 0x00000004L,
  161. PRF_ERASEBKGND = 0x00000008L,
  162. PRF_CHILDREN = 0x00000010L,
  163. PRF_OWNED = 0x00000020L
  164. }
  165. */
  166. /// <summary>
  167. /// Captures the window.
  168. /// </summary>
  169. /// <param name="control">The control.</param>
  170. /// <param name="bitmap">The bitmap.</param>
  171. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  172. public static bool CaptureWindow(System.Windows.Forms.Control control,
  173. ref System.Drawing.Bitmap bitmap)
  174. {
  175. //This function captures the contents of a window or control
  176. Graphics g2 = Graphics.FromImage(bitmap);
  177. //PRF_CHILDREN // PRF_NONCLIENT
  178. int meint = (int)(PRF_CLIENT | PRF_ERASEBKGND); //| PRF_OWNED ); // );
  179. System.IntPtr meptr = new System.IntPtr(meint);
  180. System.IntPtr hdc = g2.GetHdc();
  181. win32.SendMessage(control.Handle, win32.WM_PRINT, hdc, meptr);
  182. g2.ReleaseHdc(hdc);
  183. g2.Dispose();
  184. return true;
  185. }
  186. }
  187. }