确保SetForegroundWindow()成功
HWND hForeWnd = NULL;
HWND hWnd=::FindWindow(NULL,"QQ用户登录");
DWORD dwForeID;
DWORD dwCurID;
hForeWnd = ::GetForegroundWindow();
dwCurID = ::GetCurrentThreadId();
dwForeID = ::GetWindowThreadProcessId( hForeWnd, NULL );
::AttachThreadInput( dwCurID, dwForeID, TRUE);
::ShowWindow( hWnd, SW_SHOWNORMAL );
::SetWindowPos( hWnd, HWND_TOPMOST, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE );
::SetWindowPos( hWnd, HWND_NOTOPMOST, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE );
::SetForegroundWindow( hWnd );
::AttachThreadInput( dwCurID, dwForeID, FALSE);
运行单个实例
BOOL CInstanceDlg::FirstInstance(CString stTemp)
{
CWnd *pWndPrev,*pWndChild;
if(pWndPrev=CWnd::FindWindow(NULL,stTemp))
{
pWndChild=pWndPrev->GetLastActivePopup();// 函数确定指定窗口中的哪一个弹出式窗口是最近活动的窗口
if(pWndPrev->IsIconic()) // 判断窗口是否为最小化
pWndPrev->ShowWindow(SW_RESTORE);
pWndPrev->SetForegroundWindow(); // 窗口托到最前台
return FALSE;
}
else
return TRUE;
}