biz_engine_side.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10. using Saraff.Twain;
  11. using HXX.Scanner.Common;
  12. namespace HXX.Scanner.Biz
  13. {
  14. /// <summary>
  15. /// 调用windows系统api,关闭驱动弹框
  16. /// </summary>
  17. public class biz_engine_side
  18. {
  19. private static Form main_window;
  20. const int WM_CLOSE = 0x10;
  21. const int WM_GETTEXT = 0x000D;
  22. const int WM_GETTEXTLENGTH = 0x000E;
  23. private const int WM_ACTIVATE = 0x006;
  24. private const int WM_ACTIVATEAPP = 0x01C;
  25. private const int WM_NCACTIVATE = 0x086;
  26. private const int WA_INACTIVE = 0;
  27. private const int WM_MOUSEACTIVATE = 0x21;
  28. private const int MA_NOACTIVATE = 3;
  29. private const int WA_ACTIVE = 1;
  30. private const int SW_HIDE = 0;
  31. private const int SW_NORMAL = 1;
  32. private const int SW_SHOWNORMAL = 1;
  33. private const int SW_MAXIMIZE = 3;
  34. private const int SW_SHOWNOACTIVATE = 4;
  35. private const int SW_SHOW = 5;
  36. private const int SW_MINIMIZE = 6;
  37. private const int SW_RESTORE = 9;
  38. private const int SW_SHOWDEFAULT = 10;
  39. // 查找窗口
  40. [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
  41. public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  42. // 遍历窗口的所有子窗口,通过CallBack回调
  43. [DllImport("user32.dll")]
  44. public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam);
  45. public delegate bool CallBack(IntPtr hwnd, int lParam);
  46. // 获取窗口的类名
  47. [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  48. static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
  49. // 判断窗口是否可见
  50. [DllImport("user32.dll")]
  51. public static extern bool IsWindowVisible(IntPtr hWnd);
  52. // 获取窗口文本长度
  53. [DllImport("user32.dll")]
  54. public static extern int GetWindowTextLength(IntPtr hWnd);
  55. // 获取窗口文本,文本会塞入StringBuilder中,需要指明字符串最大长度nMaxCount
  56. [DllImport("User32.dll", EntryPoint = "GetWindowText")]
  57. private static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int nMaxCount);
  58. // 给窗口发送消息
  59. [DllImport("user32.dll", EntryPoint = "SendMessageA")]
  60. public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
  61. // 给窗口发送消息,事件返回的数据通过Byte[]数组获得
  62. [DllImport("user32.dll", EntryPoint = "SendMessageA")]
  63. public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, Byte[] lParam);
  64. [DllImport("user32.dll", EntryPoint = "GetDlgItemTextA")]
  65. private static extern int GetDlgItemText(IntPtr hDlg, int nIDDlgItem, [Out] StringBuilder lpString, int nMaxCount);
  66. delegate bool deleWindowsProc(IntPtr hWnd, string lParam);
  67. [DllImport("user32.dll")]
  68. static extern int EnumWindows(deleWindowsProc hWnd, string lParam);
  69. [DllImport("user32.dll")]
  70. public static extern IntPtr SetActiveWindow(IntPtr handle);
  71. [DllImport("user32.dll")]
  72. public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
  73. [DllImport("user32.dll")]
  74. private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags);
  75. /// <summary>
  76. /// 置顶弹框扫描,废弃
  77. /// </summary>
  78. /// <param name="hWnd"></param>
  79. /// <param name="lParam"></param>
  80. /// <returns></returns>
  81. private static bool PrintWindow(IntPtr hWnd, string lParam)
  82. {
  83. var sb = new StringBuilder(50);
  84. GetWindowText(hWnd, sb, sb.Capacity);
  85. var title = sb.ToString().ToUpper();
  86. //if (sb.ToString() != String.Empty && sb.ToString().Contains(lParam))
  87. if (!string.IsNullOrEmpty(title) && (
  88. title.Contains("KV-") ||
  89. title.Contains("CANON") ||
  90. title.Contains("KODAK")
  91. ))
  92. {
  93. //ShowWindow(hWnd, SW_SHOW);
  94. //SendMessage(hWnd, WM_ACTIVATE, WA_ACTIVE, 0);
  95. //SendMessage(hWnd, WM_NCACTIVATE, WA_ACTIVE, 0);
  96. //ShowWindow(hWnd, SW_SHOWNORMAL);
  97. //SendMessage(hWnd, WM_NCACTIVATE, 1, 0);
  98. //SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE);
  99. //SendMessage(hWnd, WM_ACTIVATE, WA_ACTIVE, 0);
  100. //main_window.WindowState = FormWindowState.Normal;
  101. main_window.TopMost = true;
  102. main_window.TopMost = false;
  103. //main_window.WindowState = FormWindowState.Minimized;
  104. main_window.Activate();
  105. if (title.Contains("KODAK"))
  106. {
  107. ShowWindow(hWnd, SW_MINIMIZE);
  108. ShowWindow(hWnd, SW_SHOWNORMAL);
  109. }
  110. SendMessage(hWnd, WM_ACTIVATE, WA_ACTIVE, 0);
  111. SetWindowPos(hWnd, main_window.Handle, 0, 0, 0, 0, 3);
  112. //SetActiveWindow(hWnd);
  113. return false;
  114. }
  115. return true;
  116. }
  117. /// <summary>
  118. /// 置顶弹框扫描,废弃
  119. /// </summary>
  120. /// <param name="_main"></param>
  121. public static void active_3rd_form(Form _main)
  122. {
  123. main_window = _main;
  124. var r = EnumWindows(PrintWindow, "KV-");
  125. //var r = EnumWindows(PrintWindow, "微信图片");
  126. }
  127. /// <summary>
  128. /// 关闭驱动弹框
  129. /// </summary>
  130. public static void check()
  131. {
  132. check_cannon();
  133. check_panasonic();
  134. }
  135. /// <summary>
  136. /// 关闭驱动弹框
  137. /// </summary>
  138. public static void check_all()
  139. {
  140. check_cannon_all();
  141. check_panasonic();
  142. }
  143. /// <summary>
  144. /// 佳能除卡纸外,弹框全杀
  145. /// </summary>
  146. private static void check_cannon()
  147. {
  148. IntPtr msgHandle = FindWindow(null, "扫描仪错误");
  149. if (msgHandle != IntPtr.Zero)
  150. {
  151. var text = check_cannon_text(msgHandle);
  152. if (!text.Contains("卡纸"))
  153. {
  154. SendMessage(msgHandle, WM_CLOSE, 0, 0);
  155. }
  156. }
  157. }
  158. /// <summary>
  159. /// 检查佳能弹框文本
  160. /// </summary>
  161. /// <param name="mainHandle"></param>
  162. /// <returns></returns>
  163. private static string check_cannon_text(IntPtr mainHandle)
  164. {
  165. string result = "";
  166. if (mainHandle != IntPtr.Zero)
  167. {
  168. int i = EnumChildWindows(mainHandle, (h, l) =>
  169. {
  170. StringBuilder sb = new StringBuilder();
  171. GetClassName(h, sb, 255);
  172. string classname = sb.ToString();
  173. if ("Static" == classname)
  174. {
  175. // 是否可见
  176. if (IsWindowVisible(h))
  177. {
  178. // 取出窗口文本
  179. int textLen;
  180. textLen = SendMessage(h, WM_GETTEXTLENGTH, 0, 0);
  181. Byte[] byt = new Byte[textLen];
  182. SendMessage(h, WM_GETTEXT, textLen + 1, byt);
  183. result = Encoding.Default.GetString(byt);
  184. }
  185. }
  186. return true;
  187. }, 0);
  188. }
  189. return result;
  190. }
  191. /// <summary>
  192. /// 松下弹框全杀
  193. /// </summary>
  194. private static void check_panasonic()
  195. {
  196. IntPtr msgHandle = FindWindow(null, "Source信息");
  197. if (msgHandle != IntPtr.Zero)
  198. {
  199. SendMessage(msgHandle, WM_CLOSE, 0, 0);
  200. }
  201. }
  202. /// <summary>
  203. /// 佳能弹框全杀
  204. /// </summary>
  205. private static void check_cannon_all()
  206. {
  207. IntPtr msgHandle = FindWindow(null, "扫描仪错误");
  208. if (msgHandle != IntPtr.Zero)
  209. {
  210. SendMessage(msgHandle, WM_CLOSE, 0, 0);
  211. }
  212. }
  213. }
  214. }