Qt调用win32 API 把第三方工具的窗口设置为最顶层
LIBS += User32.LIB
LIBS += Gdi32.LIB
#include <windows.h>
#include <conio.h>
bool bFinishShow;
BOOL MyEnumProc(HWND hwnd, LPARAM param)
{
LPWSTR lpString = (LPWSTR)malloc(sizeof(WCHAR) * MAX_PATH);
if (IsWindow(hwnd) &&IsWindowEnabled(hwnd) &&IsWindowVisible(hwnd))
{
if (GetWindowTextW(hwnd, lpString, MAX_PATH) > 0) {
if(QString::fromStdWString(lpString) == "test.exe"){
bFinishShow = true;
}
}
}
free(lpString);
return TRUE;
}
- 主函数,QProcess打开并且检测是否已经显示出来,显示出来则设置最顶层
bool Test::startTest(){
QProcess process= new QProcess();
bFinishShow = false;
QString sCommand = QString("%1/test/test.exe").arg(QCoreApplication::applicationDirPath());
process->start(sCommand);
process->waitForStarted();
while(EnumWindows(MyEnumProc, 0) && (!bFinishShow));
HWND hwnds = FindWindowA(NULL,"test.exe");
ShowWindow(hwnds,SW_SHOWNORMAL);
SetWindowPos(
hwnds,
HWND_TOPMOST,
0,
0,
0,
0,
SWP_NOMOVE|SWP_NOSIZE
);
return true;
}
以上步骤即可实现设置第三方工具界面最上层。
|