1、写回调函数:
static BOOL CALLBACK StaticEnumWindowsProc(HWND hwnd, LPARAM lParam)
{
GetWindowText(hwnd, app_name, sizeof(app_name)); //获取窗口名称
HICON hIcon = (HICON)GetClassLong(hwnd, GCL_HICON); //获取图标
if(hIcon)
{
//QPixmap pix = QtWin::fromHICON(hIcon); //图标转换为图片
}
return TRUE;
}
然后在主函数调用以下函数,每找到一个窗口都会调用一次EnumWindowsProc,然后选出所要的窗口句柄,获取图标
EnumWindows(StaticEnumWindowsProc,reinterpret_cast<LPARAM>(this));
2、
//通过窗口类名和窗口名获取窗口句柄
HWND MainWindow::GetwinId(void)
{
LPCWSTR app = NULL;
// QString ClassName("Qt5QWindowIcon");
// app = reinterpret_cast<LPCWSTR>(ClassName.data());
QString AppName = QStringLiteral("计算器");
LPCWSTR appCaption = reinterpret_cast<LPCWSTR>(AppName.data());
HWND hwnd = FindWindow(app, appCaption); //获取窗口句柄
SwitchToThisWindow(hwnd, TRUE); //设置窗口置顶
return hwnd;
}
下载地址
https://download.youkuaiyun.com/download/qq_28581781/11505343