Win32 API 如何 激活另一个应用程序,如VB的 AppActivate那样?
1. 应用程序A 要激活另一个应用程序B
如果 应用程序B的完整标题为titleB,
VB中语句
 AppActivate "titleB"
可激活应用程序B
2. C++(如VC) 如何实现?
---------------------------------------------------
HWND hwnd=
::FindWindow( AppB_WndClassName,NULL);
LPARAM thisThID=AfxGetApp()->m_nThreadID;
::SendMessage( hwnd,WM_ACTIVATEAPP,TRUE,thisThID);
----------------------- 不管用呢!------------------  

解决方案 »

  1.   

    LRESULT SendMessage(
      HWND hWnd,      // handle of destination window
      UINT Msg,       // message to send
      WPARAM wParam,  // first message parameter
      LPARAM lParam   // second message parameter
    );
     
    Parameters
    hWnd 
    Handle to the window whose window procedure will receive the message. If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows. 
    Msg 
    Specifies the message to be sent. 
    wParam 
    Specifies additional message-specific information. 
    lParam 
    Specifies additional message-specific information. WM_ACTIVATEAPP 
    fActive = (BOOL) wParam;        // activation flag 
    dwThreadID = (DWORD) lParam:    // thread identifier 
     
    Parameters
    fActive 
    Value of wParam. Specifies whether the window is being activated or deactivated. This parameter is TRUE if the window is being activated; it is FALSE if the window is being deactivated. 
    dwThreadID 
    Value of lParam. Specifies a thread identifier. If the fActive parameter is TRUE, dwThreadID is the identifier of the thread that owns the window being deactivated. If fActive is FALSE, dwThreadID is the identifier of the thread that owns the window being activated. 
    thisThID 这个应该是对方的 进程ID吧?
      

  2.   

    是那个将被ACTIVATE的窗口所属的线程id
      

  3.   


    不管是"什么"线程id, 经测试,应用程序B总能收到消息
    这是程序B对WM_ACTIVATEAPP响应的部分记录:
    [  1]: WM_ACTIVATEAPP,0x1,0xE98
    [  3]: WM_ACTIVATEAPP,0x1,0x680
    [  4]: WM_ACTIVATEAPP,0x0,0x20C
    [  9]: WM_ACTIVATEAPP,0x1,0xE98
    [ 12]: WM_ACTIVATEAPP,0x0,0x20C
    [ 13]: WM_ACTIVATEAPP,0x1,0x0
    [ 16]: WM_ACTIVATEAPP,0x1,0xBF8
    [ 29]: WM_ACTIVATEAPP,0x1,0x680
       可见,问题不是
    ::SendMessage( hwnd,WM_ACTIVATEAPP,TRUE,thisThID);
    语句行"失效",是否要对程序B的窗口"粗暴"一点,把它请到
    最上面?!
      

  4.   

    SetForegroundWindow
    BringWindowToTop
    这2个函数试过没有?
      

  5.   

    谢谢各位!
    问题已解决,直接用SetWindowPos, <<<对程序B的窗口"粗暴"一点 ::GetWindowRect(hwnd,&rect); ::SetWindowPos(hwnd,HWND_NOTOPMOST,
    rect.left ,
    rect.top ,
    rect.right-rect.left,
    rect.bottom -rect.top,
    SWP_NOREPOSITION|SWP_NOSIZE|SWP_SHOWWINDOW);
      

  6.   

    BringWindowToTop(hwnd) ;//最省事 !