窗体构造时调用windows的api
::SetWindowPos(HWND(this->winId()), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);//窗口置顶
::SetWindowPos(HWND(this->winId()), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);//如果不加这个会一直置顶
//::SwitchToThisWindow(HWND(this->winId()),true);//设置窗口焦点
this->show();
窗体析构时最好也清理下
DestroyWindow(HWND(this->winId()));
需要包含头文件
#include <windows.h>
#include <Winuser.h>
.pro文件中添加
LIBS +=User32.LIB
注意:如果用在子窗体上,则子窗体不能用主窗体作为parent
注意:一个程序的多个子窗体同时弹出时,会出现点别的程序界面但是子窗体无法退到后面的问题,需要选中当前窗口,再点别的才会退到后面去。还不知道怎么解决。
---------------------新方案-------------
.h及lib如上,在构造新窗体时加入如下代码 (完美解决~)
AttachThreadInput(GetWindowThreadProcessId(::GetForegroundWindow(),NULL), GetCurrentThreadId(),TRUE);
SetForegroundWindow(HWND(this->winId()));
SetFocus(HWND(this->winId()));
AttachThreadInput(GetWindowThreadProcessId(::GetForegroundWindow(),NULL), GetCurrentThreadId(),FALSE);
this->show();