单击窗体的按钮

//*******************************************************************
// 类名:	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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值