windows下其它软件的自动打开与点击

windows下其它软件的自动打开与点击

一,需求

项目要用到第三方软件,这个软件没有提供SDK,只提供了两个exe,每次开机后需要启动这两个exe,并且还要点击上边的两个按钮。这样的用户体验怎么能让人接受呢,如果查资料,发现windows提供了接口,可以自动化操作。

二,逻辑

三,实现

窗体标题 或者句柄 可以使用 Spy++ 这个工具获取。


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

//杀死窗口所在的进程
void killEXE(LPCWSTR title) {
HWND hwnd = FindWindow(NULL, title);
if (hwnd != NULL) {
DWORD processId;
GetWindowThreadProcessId(hwnd, &processId);

HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, processId);
if (hProcess != NULL) {
if (TerminateProcess(hProcess, 0)) {
std::cout << "Process terminated successfully!" << std::endl;
}
else {
std::cerr << "Failed to terminate process! Error code: " << GetLastError() << std::endl;
}
CloseHandle(hProcess);
}
else {
std::cerr << "Failed to open process handle!" << std::endl;
}
}
else {
std::cerr << "Window not found!" << std::endl;
}
}

//打开可执行文件
void openEXE(LPCWSTR applicationName) {
LPWSTR commandLine = NULL;
STARTUPINFOW startupInfo;
PROCESS_INFORMATION processInfo;
ZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);

if (CreateProcessW(applicationName, commandLine, NULL, NULL, FALSE, 0, NULL, NULL, &startupInfo, &processInfo)) {
std::cout << "Process created successfully!" << std::endl;
}
else {
std::cerr << "Failed to create process! Error code: " << GetLastError() << std::endl;
}
}

//最小化窗口
void minimizeDialog(LPCWSTR title) {
HWND hwnd = FindWindow(NULL, title);
if (hwnd != NULL) {
ShowWindow(hwnd, SW_MINIMIZE);
std::cout << "Window minimized successfully!" << std::endl;
}
else {
std::cerr << "Window not found!" << std::endl;
}
}

//点击窗口上的按钮
void clickBtn(LPCWSTR title, LPCWSTR btnName) {
HWND hwnd = FindWindow(NULL, title);
if (hwnd != NULL) {
HWND btnHandle = FindWindowEx(hwnd, NULL, L"Button", btnName);
if (btnHandle != NULL) {
SendMessage(btnHandle, BM_CLICK, 0, 0);
SendMessage(btnHandle, BM_CLICK, 0, 0);
}
else {
std::cerr << "Button not found!" << std::endl;
}
}
else {
std::cerr << "Window not found!" << std::endl;
}
}
int main()
{
//关闭进程
killEXE(L"Geomagic Control X Automation (Client)");
killEXE(L"Geomagic Control X Automation (Server)");

//打开客户端进程
openEXE(L"D:\\Program Files\\Oqton\\Geomagic Control X 2024.1\\Geomagic Control X Automation (Client).exe");
Sleep(1000);
//最小化客户端
minimizeDialog(L"Geomagic Control X Automation (Client)");

//打开服务器进程
openEXE(L"D:\\Program Files\\Oqton\\Geomagic Control X 2024.1\\Geomagic Control X Automation (Server).exe");
Sleep(1000);
//点击OK按钮
clickBtn(L"Geomagic Control X Automation (Server) - Mode Select",L"OK");
Sleep(1000);
//点击 Start Server按钮
clickBtn(L"Geomagic Control X Automation (Server)", L"Start Server");
Sleep(1000);
//最小化服务器客户端
minimizeDialog(L"Geomagic Control X Automation (Server)");


}

三,效果(需要管理员权限运行程序)

发表回复