如果我们想改变窗口z-order,是窗口置顶.

  1. BringWindowToTop
  2. SetWindowPos
  3. SetForegroundWindow
BringWindowToTop:调用这个函数类似于调用SetWindowPos函数来改变窗口在Z序中的位置,但是
BringWindowToTop函数并不能使一个窗口成为前台窗口。

SetWindowPos:TOPMOST 会使该窗口一直置顶,保持在最顶层。

SetForegroundWindow:指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的
记号。系统给创建前台窗口的线程分配的权限稍高于其他线程。(当次在最顶层)

SetForegroundWindow使窗口当前置顶

BOOL ForceForegroundWindow(HWND hWnd) 
{
if(!hWnd || !::IsWindow(hWnd))
return FALSE;

HWND hWndCurrentWindow = ::GetForegroundWindow();
if(hWndCurrentWindow == NULL)
{
return ::SetForegroundWindow(hWnd);
}

DWORD dwCurProcId, dwCurThreadId = ::GetWindowThreadProcessId(hWndCurrentWindow, &dwCurProcId);
DWORD dwHWndProcId;
::GetWindowThreadProcessId(hWnd, &dwHWndProcId);

BOOL bReturn = FALSE;

if(dwHWndProcId == dwCurProcId)
{
bReturn = ::SetForegroundWindow(hWnd);
}
else
{
DWORD dwMyThreadId = ::GetCurrentThreadId();

if(::AttachThreadInput(dwMyThreadId, dwCurThreadId, TRUE))
{
bReturn = ::SetForegroundWindow(hWnd);
::AttachThreadInput(dwMyThreadId, dwCurThreadId, FALSE);
}
else
{
bReturn = ::SetForegroundWindow(hWnd);
}
}

return bReturn;
}