后台进程在初始化的时候是无法通过设置顶层窗口弹到前台的,经测试,可以通过设置一个Timer,在Timer中通过SetWindowPos将对话框窗口拉到前台,先设置HWND_TOPMOST然后设置HWND_NOTOPMOST是为了将窗口弹到顶层但是不持续的在顶层。
{
const static int s_timer = 11234;
if (msg == WM_INITDIALOG)
{
HLOG_PRINTW(L"WM_INITDIALOG");
ms_hChild = hdlg;
HWND parent = GetParent(hdlg);
ms_hMainDlg = parent;
SetWindowTextW(parent, ms_DlgTitle.c_str());
ms_TimerCount = 0;
GdUiCenterWindow(parent);
SetForegroundWindow(parent);
SetTimer(hdlg, s_timer, 100, NULL);
}else if (msg == WM_TIMER)
{
ms_TimerCount++;
SetWindowPos(ms_hMainDlg, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
SetWindowPos(ms_hMainDlg, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
if (ms_TimerCount == 3)
{
KillTimer(ms_hChild, s_timer);
}
}
return 0;
}