主程序创建新的进程后失去焦点,如何避免,创建后焦点仍留在主程序窗口上呢?
网上好多方法我都试过
SetWindowPos
SetWindowForeground
SetFocus
SetActiveWindow 
都不行啊

解决方案 »

  1.   

    用HOOK吧,勾住WM_ACTIVATE消息有个比较山寨的方法,响应WM_TIMER消息
    OnTimer函数中:HWND hWnd = ::GetForegroundWindow();
    if(hWnd != GetSafeHwnd())
    {
    SetWindowPos(&wndTop, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
    SetForegroundWindow();
    CPoint pt;
    ::GetCursorPos(&pt);
    CRect rc;
    GetWindowRect(rc);
    CPoint newPt(rc.CenterPoint());
    ShowCursor(FALSE); SetCursorPos(newPt.x, newPt.y);
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    SetCursorPos(pt.x, pt.y);
    ShowCursor(TRUE);
    }
      

  2.   


    //- 初始化进程所需参数
    STARTUPINFO si;    
        PROCESS_INFORMATION pi;    
        ZeroMemory(&si,sizeof(STARTUPINFO));    
        ZeroMemory(&pi,sizeof(PROCESS_INFORMATION));
    si.cb = sizeof(STARTUPINFO);
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_SHOWNOACTIVATE;
    //- 创建服务进程,完成服务的注册
    if (!::CreateProcess(NULL, m_strSvcFullPath.GetBuffer(m_strSvcFullPath.GetLength()), NULL, NULL, false, 0, NULL, NULL, &si, &pi))
    {
    m_strSvcFullPath.ReleaseBuffer();
    return false;
    } m_strSvcFullPath.ReleaseBuffer();