admin
2022-09-09 60feedf43a35a9ca69d05095a01c5d1797b1bdc3
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "Win32Util.h"
 
#include <thread>
#include <vector>
 
 
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);
}
 
 
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::copy(string text)
{
    HWND hWnd = NULL;
    OpenClipboard(hWnd);//´ò¿ª¼ôÇаå
    EmptyClipboard();//Çå¿Õ¼ôÇаå
    HANDLE hHandle = GlobalAlloc(GMEM_FIXED, 1000);//·ÖÅäÄÚ´æ
    char* pData = (char*)GlobalLock(hHandle);//Ëø¶¨Äڴ棬·µ»ØÉêÇëÄÚ´æµÄÊ×µØÖ·
    strcpy(pData, text.c_str());
    SetClipboardData(CF_TEXT, hHandle);//ÉèÖüôÇаåÊý¾Ý
    GlobalUnlock(hHandle);//½â³ýËø¶¨
    CloseClipboard();//¹Ø±Õ¼ôÇаå
}
 
//¼üÅÌÊäÈëÊý×Ö
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::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::selectTexFileWin10(HWND hwnd, string path)
{
    //Ñ¡Ôñ·¾¶
    HWND pathWin = FindWindowExA(hwnd, NULL, "WorkerW", NULL);
    pathWin = FindWindowExA(pathWin, NULL, "ReBarWindow32", NULL);
    pathWin = FindWindowExA(pathWin, NULL, "Address Band Root", NULL);
    pathWin = FindWindowExA(pathWin, NULL, "msctls_progress32", NULL);
    pathWin = FindWindowExA(pathWin, NULL, "Breadcrumb Parent", NULL);
    pathWin = FindWindowExA(pathWin, NULL, "ToolbarWindow32", NULL);
    RECT p;
    GetWindowRect(pathWin, &p);
    Win32Util::focus(hwnd);
    Win32Util::click(p.right - 5, p.top + 5, 10);
    Sleep(1000);
 
    //¸´ÖÆÂ·¾¶
    Win32Util::copy(path);
    //Õ³Ìù·¾¶
    keyboardPaste();
 
    //°´enter°´¼ü
    keybd_event(VK_RETURN, 0, 0, 0);
    keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
 
 
 
 
    //Ñ¡×ŵ¼ÈëTxt
    HWND typeWin = FindWindowExA(hwnd, NULL, "ComboBox", NULL);
    GetWindowRect(typeWin, &p);
    Win32Util::focus(typeWin);
    Win32Util::click(p.right - 5, p.top + 5, 10);
    Win32Util::click(p.right - 5, p.bottom + 30, 100);
    Sleep(500);
 
 
 
    //Ñ¡ÖÐÎļþ
    HWND contentWin = FindWindowExA(hwnd, NULL, "DUIViewWndClassName", NULL);
    contentWin = FindWindowExA(contentWin, NULL, "DirectUIHWND", NULL);
    HWND contentWin_ = FindWindowExA(contentWin, NULL, "CtrlNotifySink", NULL);
 
    for (int i = 0;i < 5;i++)
    {
        if (FindWindowExA(contentWin_, NULL, "SHELLDLL_DefView", NULL) > 0)
            break;
        contentWin_ = FindWindowExA(contentWin, contentWin_, "CtrlNotifySink", NULL);
    }
    GetWindowRect(contentWin_, &p);
 
    
    Win32Util::click(p.left + 20, p.top + 10, 10);
    Win32Util::click(p.left + 20, p.top + 10, 10);
    Win32Util::click(p.left + 20, p.top + 10, 10);
 
    //Sleep(100);
 
    //µã»÷´ò¿ª
    //HWND openWin = FindWindowExA(hwnd, NULL, NULL, "´ò¿ª(&O)");
    //GetWindowRect(openWin, &p);
    //return;
    //Win32Util::click((p.left + p.right) / 2, (p.top + p.bottom) / 2, 100);
}
 
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);
}