废话不多说,直接贴代码。有想要解析的可以继续往下翻。
完整解决方案:KawaiiSh1zuku/CtYunAgentWindow
#include <iostream>
#include <windows.h>
#include <string.h>
#include <tchar.h>
#pragma comment(lib, "user32.lib")
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam) {
char className[256];
GetClassNameA(hwnd, className, sizeof(className));
std::cout << "Child Window: " << className << std::endl;
RECT clientRect;
GetClientRect(hwnd, &clientRect);
RECT windowRect;
GetWindowRect(hwnd, &windowRect);
std::cout << "Client Area Resolution: " << clientRect.right - clientRect.left
<< " x " << clientRect.bottom - clientRect.top << std::endl;
std::cout << "Window Resolution: " << windowRect.right - windowRect.left
<< " x " << windowRect.bottom - windowRect.top << std::endl;
if (windowRect.right - windowRect.left < screenWidth && windowRect.bottom - windowRect.top < screenHeight) {
std::cout << "You are just the window I'm looking for!" << std::endl;
HWND hd = reinterpret_cast<HWND>(lParam);
if (IsWindowVisible(hd)) {
ShowWindow(hd, SW_HIDE);
std::cout << "Hid" << std::endl;
}
else {
ShowWindow(hd, SW_SHOW);
std::cout << "Shown" << std::endl;
}
return false;
}
return true;
}
int main(int argc, _TCHAR* argv[])
{
HWND hd = GetDesktopWindow();
hd = GetWindow(hd, GW_CHILD);
char s[200] = { 0 };
while (hd != NULL)
{
memset(s, 0, 200);
GetWindowText(hd, s, 200);
if (strstr(s,"CtyunClouddeskUniversal"))
{
EnumChildWindows(hd, EnumChildProc, reinterpret_cast<LPARAM>(hd));
}
hd = GetNextWindow(hd, GW_HWNDNEXT);
}
return 0;
}
解析:用遍历取得客户端的窗口,然后判断是不是我们想要隐藏/显示的窗口(有一个覆盖了全屏的窗口,显示后会在顶层阻断鼠标操作)
至于为什么用遍历?不知道为什么用FindWindow
找不到天翼云的窗口。