#include "Win32Util.h" #include #include void clickRunner(int delay) // º¯ÊýÃû×Ö¿ÉËæÒâ { Sleep(delay); mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); Sleep(10); mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); Sleep(10); } void moveRunner(int x, int y, int delay) // º¯ÊýÃû×Ö¿ÉËæÒâ { Sleep(delay); SetCursorPos(x, y); Sleep(20); } void kbNumRunner(string nums, int delay) // º¯ÊýÃû×Ö¿ÉËæÒâ { Sleep(delay); for (int i = 0; i < nums.length(); i++) { int code = int(nums.c_str()[i]); if (code == '.') { code = 110; } keybd_event(code, 0, 0, 0); Sleep(5); keybd_event(code, 0, KEYEVENTF_KEYUP, 0); Sleep(20); } } void kbKeyRunner(int code, int delay) // º¯ÊýÃû×Ö¿ÉËæÒâ { Sleep(delay); keybd_event(code, 0, 0, 0); keybd_event(code, 0, KEYEVENTF_KEYUP, 0); } std::string Win32Util::convertLPWSTRToUTF8(LPWSTR lpwstr) { int len = WideCharToMultiByte(CP_UTF8, 0, lpwstr, -1, NULL, 0, NULL, NULL); if (len == 0) { // ת»»Ê§°Ü return ""; } char* buffer = new char[len]; WideCharToMultiByte(CP_UTF8, 0, lpwstr, -1, buffer, len, NULL, NULL); std::string result(buffer); delete[] buffer; return result; } list Win32Util::searchWindow(string name) { auto hwnd = GetDesktopWindow(); HWND mainPage = HWND(); //»ñÈ¡×ÀÃæ×Ó´°¿Ú¾ä±ú hwnd = GetWindow(hwnd, GW_CHILD); list list; while (hwnd != NULL) { std::string str = getWindowName(hwnd); if (str.find(name) != string::npos) { list.push_back(hwnd); } hwnd = GetNextWindow(hwnd, GW_HWNDNEXT); } return list; } string Win32Util::getWindowName(HWND hwnd) { int length = GetWindowTextLength(hwnd); TCHAR getbuf[10240]; GetWindowText(hwnd, getbuf, length + 1); int iLen = WideCharToMultiByte(CP_ACP, 0, getbuf, -1, NULL, 0, NULL, NULL); //Ê×ÏȼÆËãTCHAR ³¤¶È¡£ char* chRtn = (char*)alloca(iLen * sizeof(char)); //¶¨ÒåÒ»¸ö TCHAR ³¤¶È´óСµÄ CHAR ÀàÐÍ¡£ WideCharToMultiByte(CP_ACP, 0, getbuf, -1, chRtn, iLen, NULL, NULL); //½«TCHAR ÀàÐ͵ÄÊý¾Ýת»»Îª CHAR ÀàÐÍ¡£ std::string str(chRtn); return str; } string Win32Util::getClassName(HWND hwnd) { TCHAR className[1024]; GetClassName(hwnd, className, 1024); string st = convertLPWSTRToUTF8(className); return st; } wstring Win32Util::getText(HWND hwnd) { int length = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0); if (length == -1) return L""; wchar_t* buffer = new wchar_t[length + 1]; SendMessage(hwnd, WM_GETTEXT, length + 1, (LPARAM)buffer); std::wstring str(buffer); delete[] buffer; return str; } void Win32Util::click(int delay) { std::thread tclickRunner(clickRunner, std::ref( delay)); tclickRunner.join(); } void Win32Util::click(int x, int y, int delay) { std::thread clickRunner(moveRunner, x, y, delay); clickRunner.join(); click(); } void Win32Util::mouseMove(int x, int y, int delay) { std::thread clickRunner(moveRunner, x, y, delay); clickRunner.join(); } void Win32Util::focus(HWND hwnd) { SetForegroundWindow(hwnd); SetFocus(hwnd); } //¼üÅÌÊäÈëÊý×Ö void Win32Util::keyboardNum(string num, int delay) { thread runner(kbNumRunner, num, delay); runner.join(); } //¼üÅÌÊäÈëÆäËû¼ü void Win32Util::keyboard(int code, int delay) { thread runner(kbKeyRunner, code, delay); runner.join(); } void Win32Util::virtualKeyboard(HWND hwnd, int code) { PostMessage(hwnd, WM_KEYDOWN, code, 0); PostMessage(hwnd, WM_KEYUP, code, 0); } void Win32Util::keyboardPaste() { keybd_event(VK_CONTROL, 0, 0, 0); // °´ÏÂCTRL¼ü keybd_event('V', 0, 0, 0); // °´ÏÂa¼ü Sleep(50); keybd_event('V', 0, KEYEVENTF_KEYUP, 0);// ËÉ¿ªa¼ü keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);// ËÉ¿ªCTRL¼ü } DEVMODE Win32Util::getL2ScreenInfo() { //»ñÈ¡ÆÁÄ»ÊýÁ¿ DEVMODE dm; int screenNUm = GetSystemMetrics(SM_CMONITORS); for (int i = 0; i < screenNUm; i++) { DISPLAY_DEVICE device; ZeroMemory(&device, sizeof(DISPLAY_DEVICE)); device.cb = sizeof(DISPLAY_DEVICE); EnumDisplayDevices(NULL, i, &device, NULL); ZeroMemory(&dm, sizeof(dm)); dm.dmSize = sizeof(dm); EnumDisplaySettings(device.DeviceName, ENUM_CURRENT_SETTINGS, &dm); if (dm.dmPelsWidth > 3000) { return dm; } } throw string("ÉÐδÕÒµ½ÊʺÏL2µÄÏÔʾÉ豸"); return DEVMODE(); } void Win32Util::moveWin(HWND win, int x, int y, int width, int height) { MoveWindow(win, x, y, width, height, TRUE); } bool Win32Util::isWindowShow(HWND win) { return IsWindowVisible(win); } void Win32Util::showWindow(HWND hwnd) { ShowWindow(hwnd, SW_SHOWNORMAL); } void Win32Util::rollMouseWheel(bool back, HWND win, int x, int y) { //back-ÊÇ·ñÍùºó¹ö PostMessage(win, WM_MOUSEWHEEL, back ? 0xFF880000 : 0x00780000, MAKEWORD(x, y)); } void Win32Util::getWindowRect(HWND hwnd, RECT* rect) { GetWindowRect(hwnd, rect); } void Win32Util::visualClick(HWND hwnd, LPARAM pos) { PostMessage(hwnd, WM_LBUTTONDOWN, 0x00000001, pos); PostMessage(hwnd, WM_LBUTTONUP, 0x00000000, pos); } string Win32Util::getNowTime() { // »ñȡϵͳʱ¼ä SYSTEMTIME sys; GetLocalTime(&sys); int h = sys.wHour; int m = sys.wMinute; int s = sys.wSecond; string st = ""; if (h < 10) { st.append("0"); } st.append(to_string(h)); st.append(":"); if (m < 10) { st.append("0"); } st.append(to_string(m)); st.append(":"); if (s < 10) { st.append("0"); } st.append(to_string(s)); return st; } void Win32Util::sendMessage(HWND hwnd, UINT msg, LPARAM p0, LPARAM p1) { SendMessage(hwnd, msg, p0, p1); } void Win32Util::postMessage(HWND hwnd, UINT msg, LPARAM p0, LPARAM p1) { PostMessage(hwnd, msg, p0, p1); } void Win32Util::input_num(HWND hwnd, string num_str) { for (int i = 0; i < num_str.size(); i++) { SendMessage(hwnd, WM_KEYDOWN, num_str.c_str()[i], 0); PostMessage(hwnd, WM_KEYUP, num_str.c_str()[i], 0); Sleep(10); } } void Win32Util::addToTHS(string code) { string leftCode = code.substr(1, 5); list hwnds = searchWindow("¿´ÅÌÒ³Ãæ"); HWND origin_hwnd = 0; for (list::iterator e = hwnds.begin(); e != hwnds.end(); ++e) { HWND hwnd = *e; string name = Win32Util::getWindowName(hwnd); if (name.find("ͬ»¨Ë³") >= 0) { origin_hwnd = hwnd; } } if (origin_hwnd == NULL) { throw string("ûÓÐÕÒµ½¿´ÅÌÒ³Ãæ"); return; } input_num(origin_hwnd, code.substr(0, 1)); Sleep(200); hwnds.clear(); HWND topHwnd = GetDesktopWindow(); HWND temp = 0; int f_count = 0; while (TRUE) { if (f_count > 100000) { break; } f_count += 1; if (temp !=NULL && isWindowShow(temp)) { string className = getClassName(temp); if (className.find("Afx:") == 0) { if (className.substr(className.length() - 2, 2).find( ":0")==0) { hwnds.push_back(temp); break; } } } temp = FindWindowEx(topHwnd, temp, NULL, NULL); if (temp == NULL) { break; } } if (hwnds.size() > 0) { HWND hwndroot = *(hwnds.begin()); HWND hwnd = FindWindowEx(hwndroot, NULL, L"Edit", L""); input_num(hwnd, leftCode); Sleep(400); focus(hwndroot); SendMessage(hwndroot, WM_KEYDOWN, VK_RETURN, 0); Sleep(10); PostMessage(hwndroot, WM_KEYUP, VK_RETURN, 0); } }