12.2 实现键盘模拟按键( 二 )


#include #include using namespace std;HWND global_hwnd = 0;// 将字符串逆序char * Reverse(char str[]){int n = strlen(str);int i;char temp;for (i = 0; i < (n / 2); i++){temp = str[i];str[i] = str[n - i - 1];str[n - i - 1] = temp;}return str;}// 窗体枚举回调函数BOOL CALLBACK lpEnumFunc(HWND hwnd, LPARAM lParam){char WindowName[MAXBYTE] = { 0 };DWORD ThreadId, ProcessId = 0;GetWindowText(hwnd, WindowName, sizeof(WindowName));ThreadId = GetWindowThreadProcessId(hwnd, &ProcessId);printf("句柄: %-9d --> 线程ID: %-6d --> 进程ID: %-6d --> 名称: %s \n", hwnd, ThreadId, ProcessId, WindowName);// 首先逆序输出字符串,然后比较前13个字符if (strncmp(Reverse(WindowName), "emorhC elgooG", 13) == 0){global_hwnd = hwnd;}return TRUE;}int main(int argc, char* argv[]){// 枚举Google浏览器句柄EnumWindows(lpEnumFunc, 0);std::cout << "浏览器句柄: " << global_hwnd << std::endl;if (global_hwnd != 0){// 获取系统屏幕宽度与高度 (像素)int cx = GetSystemMetrics(SM_CXSCREEN);int cy = GetSystemMetrics(SM_CYSCREEN);std::cout << "屏幕X: " << cx << " 屏幕Y: " << cy << std::endl;// 传入指定的HWND句柄HWND hForeWnd = GetForegroundWindow();DWORD dwCurID = GetCurrentThreadId();DWORD dwForeID = GetWindowThreadProcessId(hForeWnd, NULL);AttachThreadInput(dwCurID, dwForeID, TRUE);// 将该窗体呼出到顶层ShowWindow(global_hwnd, SW_SHOWNORMAL);SetWindowPos(global_hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);SetWindowPos(global_hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);SetForegroundWindow(global_hwnd);AttachThreadInput(dwCurID, dwForeID, FALSE);// 发送最大化消息SendMessage(global_hwnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0);// 最大化// SendMessage(global_hwnd, WM_SYSCOMMAND, SC_MINIMIZE, 0); // 最小化// SendMessage(global_hwnd, WM_SYSCOMMAND, SC_CLOSE, 0);// 关闭}system("pause");return 0;}
读者可自行编译并运行上述程序,此时会将谷歌浏览器全屏并置顶显示,输出信息如下图所示;

12.2 实现键盘模拟按键

文章插图
12.2.3 读取与设置剪辑板
【12.2 实现键盘模拟按键】读取与设置剪辑版可用于对数据的拷贝与设置,调用函数并传入一段字符串可实现将传入字符串拷贝到剪辑版的功能,使用则可实现读取剪辑版中的内容到程序内 。
#include #include #include // 将字符串写入到剪切板BOOL setClipbar(const char* data){int contentSize = strlen(data) + 1;HGLOBAL hMemory; LPTSTR lpMemory;// 打开剪切板if (!OpenClipboard(NULL)){return FALSE;}// 清空剪切板if (!EmptyClipboard()){return FALSE;}// 对剪切板分配内存if (!(hMemory = GlobalAlloc(GMEM_MOVEABLE, contentSize))){return FALSE;}// 锁定内存区域if (!(lpMemory = (LPTSTR)GlobalLock(hMemory))){return FALSE;}// 复制数据到内存区域,解除内存锁定memcpy_s(lpMemory, contentSize, data, contentSize);GlobalUnlock(hMemory);// 设置剪切板数据if (!SetClipboardData(CF_TEXT, hMemory)){return FALSE;}// std::cout << "已复制: " << data << " 长度: " << contentSize << std::endl;return CloseClipboard();}// 获取剪切板内容char* getClipBoardValue(){// 初始化char* url, *pData;size_t length;// 打开剪切板if (!OpenClipboard(NULL)){return FALSE;}// 获取剪切板内的数据HANDLE hData = http://www.kingceram.com/post/GetClipboardData(CF_TEXT);if (hData == NULL){return FALSE;}// 获取数据长度length = GlobalSize(hData);url = (char*)malloc(length + 1);// 将数据转换为字符pData = (char*)GlobalLock(hData);strcpy_s(url, length, pData);// 清理工作url[length] = 0;GlobalUnlock(hData);CloseClipboard();// 返回结果并释放内存char* result = _strdup(url);free(url);return result;}int main(int argc, char *argv[]){Sleep(5000);for (size_t i = 0; i < 10; i++){// 填充字符串char MyData[256] = { 0 };sprintf(MyData,"hello lyshark --> %d", i);// 设置到剪辑版BOOL set_flag = setClipbar(MyData);if (set_flag == TRUE){// std::cout