窗口显示在最前面

有时候需要是的窗口显示在最前面,经过验证,总结了以下两个方法:

(1)void ActivateAndShowWnd(CWnd *pWnd) 

DWORD tMe,tTop; 

  tMe=GetCurrentThreadId(); 
CWnd * pnd = GetForegroundWindow();
if ( pnd != NULL && pnd->GetSafeHwnd() != NULL )
{
tTop=GetWindowThreadProcessId(pnd->m_hWnd,NULL); 
if (tMe!=tTop)

AttachThreadInput(tTop,tMe,TRUE); 
pWnd->SetForegroundWindow(); 
pWnd->SetFocus(); 
AttachThreadInput(tTop,tMe,FALSE); 

  else

pWnd->SetForegroundWindow();

pWnd->SetFocus(); 

}

}

(2)void Ccat8637Dlg::ActivateAndShowWnd(CWnd *pWnd) 

//读取当前的主框架的style
DWORD dwStyle=::GetWindowLong(pWnd->m_hWnd,GWL_EXSTYLE);

::SetWindowPos(pWnd->m_hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);

//如果主框架原来已经被设置为topmost,则此处不重设,否则进行重设  
if( !( ( dwStyle & WS_EX_TOPMOST ) == WS_EX_TOPMOST ) )
{
//保证主框架前置,然后再恢复到正常状态
::SetWindowPos(pWnd->m_hWnd,HWND_NOTOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);
}

}

猜你喜欢

转载自blog.csdn.net/llfwdd/article/details/42123633
今日推荐