//*******************************************************************
// 类名: Confirm
// 功能: 单例,用于寻找确认窗口并单击确认按钮。
// 用例: Confirm* confirm = Confirm::getInstance();
// confirm->pressConfirm("Dialog", "OK");
// API: 1. Confirm* getInstance()
// 返回单例
// 2. BOOL pressConfirm(char* wndtext, char* btntext, int tstamp)
// 单击窗体(标题为wndtext)里的按钮(名称为btntext),按钮为窗体的直属
// 子控件,tstamp为等待时间。
//
//
// 作者: bagainu
// 日期: 2015年1月28日
//*******************************************************************
#ifndef CONFIRM_H
#define CONFIRM_H
#include <windows.h>
#include <iostream>
// LOG define
#define LOG(text) do {\
std::cout << "[" << __FILE__ << ":" << __LINE__ << "] " << text << std::endl; \
} while (0);
// Active the target window to receive message click
#define ActiveTargetWnd(hwnd) do { \
BringWindowToTop(hwnd); \
SetForegroundWindow(hwnd); \
SetActiveWindow(hwnd); \
ShowWindow(hwnd, SW_SHOW); \
} while (0)
// Active button to receive message click
#define ActiveController(hwnd) do { \
BringWindowToTop(hwnd); \
SetForegroundWindow(hwnd); \
} while (0)
const size_t CONFIRM_TEXT_BUF_SIZE = 1024;
class Confirm
{
public:
static Confirm* getInstance();
BOOL pressConfirm(char* wndtext, char* btntext, int tstamp);
private:
Confirm();
~Confirm();
static Confirm* pInstance;
public:
char WindowText[CONFIRM_TEXT_BUF_SIZE];
char ButtonText[CONFIRM_TEXT_BUF_SIZE];
};
Confirm::Confirm() {}
Confirm::~Confirm()
{
delete pInstance;
}
Confirm* Confirm::pInstance = new Confirm();
Confirm* Confirm::getInstance()
{
return pInstance;
}
// Find and return hwnd (lParam) by button text
static BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam)
{
Confirm* confirm = Confirm::getInstance();
char title[CONFIRM_TEXT_BUF_SIZE] = "\0";
GetWindowText(hwnd, title, CONFIRM_TEXT_BUF_SIZE);
if (0 == strcmp(title, confirm->ButtonText))
{
LOG("Get confirm button"); // log
LOG(title); // log
ActiveController(hwnd); // active button
// Return the button hwnd to click
*(HWND*)lParam = hwnd;
return FALSE;
}
return TRUE;
}
// Find and return hwnd (lParam) by window text
static BOOL CALLBACK EnumProc(HWND hwnd, LPARAM lParam)
{
Confirm* confirm = Confirm::getInstance();
char title[CONFIRM_TEXT_BUF_SIZE] = "\0";
GetWindowText(hwnd, title, CONFIRM_TEXT_BUF_SIZE);
if (0 == strcmp(title, confirm->WindowText))
{
LOG("Get confirm window"); // log
LOG(title); // log
ActiveTargetWnd(hwnd); // active window
EnumChildWindows(hwnd, EnumChildProc, (LPARAM)lParam);
return FALSE;
}
return TRUE;
}
// Create thread proc and return the hwnd (lpParameter)
static DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
HWND buttonhwnd = 0;
EnumWindows(EnumProc, (LPARAM)&buttonhwnd);
if (buttonhwnd != 0)
{
SendMessage(buttonhwnd, BM_CLICK, 0, 0);
*(bool*)lpParameter = TRUE;
LOG("Button clicked"); // log
}
else
{
*(bool*)lpParameter = FALSE;
LOG("Button not found"); // log
}
return 0;
}
// Preass the button (btntext) in window (wndtext)
BOOL Confirm::pressConfirm(char* wndtext, char* btntext, int tstamp)
{
strncpy_s(WindowText, wndtext, CONFIRM_TEXT_BUF_SIZE);
strncpy_s(ButtonText, btntext, CONFIRM_TEXT_BUF_SIZE);
bool isClicked = FALSE;
DWORD threadId = 0;
HANDLE hThread = CreateThread(NULL, 0, ThreadProc, (LPVOID)&isClicked, 0, &threadId);
if (!hThread)
{
LOG("Thread create error"); // log
return FALSE;
}
LOG("Thread created"); // log
WaitForSingleObject(hThread, tstamp);
HRESULT hr = CloseHandle(hThread);
if (FAILED(hr))
{
LOG("Thread exit error");
return FALSE;
}
LOG("Thread closed"); // log
return isClicked;
}
#endif
单击窗体的按钮
最新推荐文章于 2021-06-21 01:45:18 发布