admin
2022-07-12 c108e5ba42168841311b74034d89c31556d628c4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "Win32Util.h"
 
#include <thread>
 
 
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++)
    {
        keybd_event(nums.c_str()[i], 0, 0, 0);
        Sleep(5);
        keybd_event(nums.c_str()[i], 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);
}
 
 
list<HWND> Win32Util::searchWindow(string name) {
    auto hwnd = GetDesktopWindow();
    HWND mainPage = HWND();
    //»ñÈ¡×ÀÃæ×Ó´°¿Ú¾ä±ú
    hwnd = GetWindow(hwnd, GW_CHILD);
    list<HWND> 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[1000];
     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;
}
 
 
void Win32Util::click(int delay) {
    thread clickRunner(clickRunner,delay);
    clickRunner.join();
}
 
void Win32Util::click(int x, int y, int delay) {
    thread clickRunner(moveRunner, x, y,delay);
    clickRunner.join();
    click();
}
 
void Win32Util::mouseMove(int x, int y, int delay) {
    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();
}