using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Runtime.InteropServices; using System.Windows.Forms; using System.Threading; using Saraff.Twain; using HXX.Scanner.Common; namespace HXX.Scanner.Biz { /// /// 调用windows系统api,关闭驱动弹框 /// public class biz_engine_side { private static Form main_window; const int WM_CLOSE = 0x10; const int WM_GETTEXT = 0x000D; const int WM_GETTEXTLENGTH = 0x000E; private const int WM_ACTIVATE = 0x006; private const int WM_ACTIVATEAPP = 0x01C; private const int WM_NCACTIVATE = 0x086; private const int WA_INACTIVE = 0; private const int WM_MOUSEACTIVATE = 0x21; private const int MA_NOACTIVATE = 3; private const int WA_ACTIVE = 1; private const int SW_HIDE = 0; private const int SW_NORMAL = 1; private const int SW_SHOWNORMAL = 1; private const int SW_MAXIMIZE = 3; private const int SW_SHOWNOACTIVATE = 4; private const int SW_SHOW = 5; private const int SW_MINIMIZE = 6; private const int SW_RESTORE = 9; private const int SW_SHOWDEFAULT = 10; // 查找窗口 [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); // 遍历窗口的所有子窗口,通过CallBack回调 [DllImport("user32.dll")] public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam); public delegate bool CallBack(IntPtr hwnd, int lParam); // 获取窗口的类名 [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); // 判断窗口是否可见 [DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr hWnd); // 获取窗口文本长度 [DllImport("user32.dll")] public static extern int GetWindowTextLength(IntPtr hWnd); // 获取窗口文本,文本会塞入StringBuilder中,需要指明字符串最大长度nMaxCount [DllImport("User32.dll", EntryPoint = "GetWindowText")] private static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int nMaxCount); // 给窗口发送消息 [DllImport("user32.dll", EntryPoint = "SendMessageA")] public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); // 给窗口发送消息,事件返回的数据通过Byte[]数组获得 [DllImport("user32.dll", EntryPoint = "SendMessageA")] public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, Byte[] lParam); [DllImport("user32.dll", EntryPoint = "GetDlgItemTextA")] private static extern int GetDlgItemText(IntPtr hDlg, int nIDDlgItem, [Out] StringBuilder lpString, int nMaxCount); delegate bool deleWindowsProc(IntPtr hWnd, string lParam); [DllImport("user32.dll")] static extern int EnumWindows(deleWindowsProc hWnd, string lParam); [DllImport("user32.dll")] public static extern IntPtr SetActiveWindow(IntPtr handle); [DllImport("user32.dll")] public static extern int ShowWindow(IntPtr hwnd, int nCmdShow); [DllImport("user32.dll")] private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags); /// /// 置顶弹框扫描,废弃 /// /// /// /// private static bool PrintWindow(IntPtr hWnd, string lParam) { var sb = new StringBuilder(50); GetWindowText(hWnd, sb, sb.Capacity); var title = sb.ToString().ToUpper(); //if (sb.ToString() != String.Empty && sb.ToString().Contains(lParam)) if (!string.IsNullOrEmpty(title) && ( title.Contains("KV-") || title.Contains("CANON") || title.Contains("KODAK") )) { //ShowWindow(hWnd, SW_SHOW); //SendMessage(hWnd, WM_ACTIVATE, WA_ACTIVE, 0); //SendMessage(hWnd, WM_NCACTIVATE, WA_ACTIVE, 0); //ShowWindow(hWnd, SW_SHOWNORMAL); //SendMessage(hWnd, WM_NCACTIVATE, 1, 0); //SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE); //SendMessage(hWnd, WM_ACTIVATE, WA_ACTIVE, 0); //main_window.WindowState = FormWindowState.Normal; main_window.TopMost = true; main_window.TopMost = false; //main_window.WindowState = FormWindowState.Minimized; main_window.Activate(); if (title.Contains("KODAK")) { ShowWindow(hWnd, SW_MINIMIZE); ShowWindow(hWnd, SW_SHOWNORMAL); } SendMessage(hWnd, WM_ACTIVATE, WA_ACTIVE, 0); SetWindowPos(hWnd, main_window.Handle, 0, 0, 0, 0, 3); //SetActiveWindow(hWnd); return false; } return true; } /// /// 置顶弹框扫描,废弃 /// /// public static void active_3rd_form(Form _main) { main_window = _main; var r = EnumWindows(PrintWindow, "KV-"); //var r = EnumWindows(PrintWindow, "微信图片"); } /// /// 关闭驱动弹框 /// public static void check() { check_cannon(); check_panasonic(); } /// /// 关闭驱动弹框 /// public static void check_all() { check_cannon_all(); check_panasonic(); } /// /// 佳能除卡纸外,弹框全杀 /// private static void check_cannon() { IntPtr msgHandle = FindWindow(null, "扫描仪错误"); if (msgHandle != IntPtr.Zero) { var text = check_cannon_text(msgHandle); if (!text.Contains("卡纸")) { SendMessage(msgHandle, WM_CLOSE, 0, 0); } } } /// /// 检查佳能弹框文本 /// /// /// private static string check_cannon_text(IntPtr mainHandle) { string result = ""; if (mainHandle != IntPtr.Zero) { int i = EnumChildWindows(mainHandle, (h, l) => { StringBuilder sb = new StringBuilder(); GetClassName(h, sb, 255); string classname = sb.ToString(); if ("Static" == classname) { // 是否可见 if (IsWindowVisible(h)) { // 取出窗口文本 int textLen; textLen = SendMessage(h, WM_GETTEXTLENGTH, 0, 0); Byte[] byt = new Byte[textLen]; SendMessage(h, WM_GETTEXT, textLen + 1, byt); result = Encoding.Default.GetString(byt); } } return true; }, 0); } return result; } /// /// 松下弹框全杀 /// private static void check_panasonic() { IntPtr msgHandle = FindWindow(null, "Source信息"); if (msgHandle != IntPtr.Zero) { SendMessage(msgHandle, WM_CLOSE, 0, 0); } } /// /// 佳能弹框全杀 /// private static void check_cannon_all() { IntPtr msgHandle = FindWindow(null, "扫描仪错误"); if (msgHandle != IntPtr.Zero) { SendMessage(msgHandle, WM_CLOSE, 0, 0); } } } }