码迷,mamicode.com
首页 > Windows程序 > 详细

Windows 消息机制

时间:2020-10-10 16:54:42      阅读:39      评论:0      收藏:0      [点我收藏+]

标签:abc   ESS   apt   param   应用   前台   sage   获取   鼠标   

#include <Windows.h>
#include <iostream>
#include <tchar.h>


#ifdef UNICODE
#define Cout std::wcout
#else
#define Cout std::cout
#endif // UNICODE
using std::endl;


/* ******************** 关闭窗口 ******************** */
void onClose()
{
	auto hWnd = FindWindow(_T("Notepad"), nullptr);
	// or auto hWnd = FindWindow(nullptr, _T("temp.txt - Notepad"));
	if (hWnd == nullptr) Cout << "No" << endl;
	else
	{
		Cout << "Yes" << endl;
		SendMessage(hWnd, WM_CLOSE, 0, 0);
	}
}


/* ******************** 执行程序 ******************** */
void onExec()
{
	auto _ret = WinExec("notepad", SW_SHOW);
	if (_ret > 32)Cout << "Yes" << endl;
	else Cout << "No" << endl;
}


/* ******************** 修改标题 ******************** */
void onEditWnd()
{
	auto hWnd = FindWindow(nullptr, _T("temp.txt - Notepad"));
	if (hWnd == nullptr) Cout << "No" << endl;
	else
	{
		Cout << "Yes" << endl;
		TCHAR pCaptionText[] = _T("测试 - Test");
		SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)pCaptionText);
	}
}


/* ******************** 获取窗口 ******************** */
void onGetWnd()
{
	auto hWnd = FindWindow(_T("Notepad"), nullptr);
	if (hWnd == nullptr) Cout << "No" << endl;
	else
	{
		Cout << "Yes" << endl;
		TCHAR pCaptionText[MAXBYTE] = { 0 };
		SendMessage(hWnd, WM_GETTEXT, MAXBYTE, (LPARAM)pCaptionText);
		Cout << pCaptionText << endl;
	}
}


/* ******************** 向指定程序发送按键信息 ******************** */
void sendKeyBoard()
{
	auto hWnd = FindWindow(nullptr, _T("百度一下,你就知道 - vivaldi"));
	if (hWnd == nullptr) Cout << "No" << endl;
	else
	{
		Cout << "Yes" << endl;
		PostMessage(hWnd, WM_KEYDOWN, VK_F5, 1);
		Sleep(50);
		PostMessage(hWnd, WM_KEYUP, VK_F5, 1);
	}
}


/* ******************** keybd_event & mouse_event ******************** */
// 将指定应用设置到前台并且激活该窗口
auto findAndFocus()
{
	auto hWnd = FindWindow(nullptr, _T("百度一下,你就知道 - vivaldi"));
	if (hWnd == nullptr) Cout << "No" << endl;
	else
	{
		Cout << "Yes" << endl;
		SetForegroundWindow(hWnd);
	}
	return hWnd;
}
// 模拟键盘
void onKeybd()
{
	findAndFocus();
	Sleep(1000);

	keybd_event(VK_F5, 0, 0, 0);
	Sleep(1000);
	keybd_event(VK_F5, 0, 0, 0);
	Sleep(1000);
	keybd_event(VK_F5, 0, 0, 0);
}
// 模拟鼠标
void onMouse()
{
	auto hWnd = findAndFocus();

	POINT pos{ 0,0 };
	GetCursorPos(&pos);

	POINT pt{ 0,0 };
	ClientToScreen(hWnd, &pt);				// 得到指定窗口在屏幕中的坐标位置
	SetCursorPos(pt.x + 40, pt.y + 400);	// 将鼠标光标移动到指定的坐标位置

	mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
	Sleep(50);
	mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
	Sleep(1000);
	keybd_event(‘R‘, 0, 0, 0);

	SetCursorPos(pos.x, pos.y);
}
// 综合测试
void testButtonMouse()
{
	auto hWnd = findAndFocus();

	POINT pt{ 0,0 };
	ClientToScreen(hWnd, &pt);
	SetCursorPos(pt.x + 460, pt.y + 380);

	mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
	Sleep(50);
	mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
	Sleep(100);

	TCHAR str[] = _T("ABCDEFG");
	for (auto ch : str)
	{
		Sleep(50);
		keybd_event(ch, 0, 0, 0);
	}
}


int main()
{
	Cout << 01010 << endl;

	return 0;
}

Windows 消息机制

标签:abc   ESS   apt   param   应用   前台   sage   获取   鼠标   

原文地址:https://www.cnblogs.com/teternity/p/13789313.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!