C++ 将焦点更改为另一个程序Windows API

C++ 将焦点更改为另一个程序Windows API,c++,winapi,C++,Winapi,我正试图让我的应用程序将焦点转移到其他任何一个窗口上,只要鼠标刚好在上面。我正在尝试实现一些拖放功能,但似乎缺少的是当鼠标将我的应用程序移动到另一个应用程序时焦点的改变 下面是我当前的测试功能(我现在在主回调过程中的WM_MOUSEMOVE上进行测试,以供参考) case WM\u MOUSEMOVE: { 点pt; GetCursorPos(&pt); HWND newHwnd=窗口起点(pt); 如果(newHwnd!=g_hSelectedWindow) { coutallowSetFor

我正试图让我的应用程序将焦点转移到其他任何一个窗口上,只要鼠标刚好在上面。我正在尝试实现一些拖放功能,但似乎缺少的是当鼠标将我的应用程序移动到另一个应用程序时焦点的改变

下面是我当前的测试功能(我现在在主回调过程中的WM_MOUSEMOVE上进行测试,以供参考)

case WM\u MOUSEMOVE:
{
点pt;
GetCursorPos(&pt);
HWND newHwnd=窗口起点(pt);
如果(newHwnd!=g_hSelectedWindow)
{

cout
allowSetForeGroundIndow
没有帮助,除非另一个窗口试图通过调用
SetForeGroundIndow
成为前台窗口

我很好奇,如果您需要将另一个窗口放到前台,为什么不直接在它上面调用
setforegroundindow

更新:这是您需要的代码,以使其正常工作:

HWND ResolveWindow(HWND hWnd)
{ /* Given a particular HWND, if it's a child, return the parent. Otherwise, if
   * the window has an owner, return the owner. Otherwise, just return the window
   */
    HWND hWndRet = NULL;

    if(::GetWindowLong(hWnd, GWL_STYLE) & WS_CHILD)
        hWndRet = ::GetParent(hWnd);

    if(hWndRet == NULL)
        hWndRet = ::GetWindow(hWnd, GW_OWNER);

    if(hWndRet != NULL)
        return ResolveWindow(hWndRet);

    return hWnd;    
}

HWND GetTopLevelWindowFromPoint(POINT ptPoint)
{ /* Return the top-level window associated with the window under the mouse 
   * pointer (or NULL) 
   */
    HWND hWnd = WindowFromPoint(ptPoint);

    if(hWnd == NULL)
        return hWnd;    

    return ResolveWindow(hWnd);
}
只需从
WM\u MOUSEMOVE
处理程序中调用
GetTopLevelWindowFromPoint(pt)
,如果您得到一个有效的HWND,那么它将是一个顶级窗口,可以使用setforeforgrounddow将其带到前台


我希望这能有所帮助。

您遇到的问题是WindowFromPoint可能会返回一个无法成为前台的窗口(即顶级窗口的子窗口)。我已经用一对您可以使用的函数更新了我的帖子。非常感谢,Nik!我今天将尝试一下:D
HWND ResolveWindow(HWND hWnd)
{ /* Given a particular HWND, if it's a child, return the parent. Otherwise, if
   * the window has an owner, return the owner. Otherwise, just return the window
   */
    HWND hWndRet = NULL;

    if(::GetWindowLong(hWnd, GWL_STYLE) & WS_CHILD)
        hWndRet = ::GetParent(hWnd);

    if(hWndRet == NULL)
        hWndRet = ::GetWindow(hWnd, GW_OWNER);

    if(hWndRet != NULL)
        return ResolveWindow(hWndRet);

    return hWnd;    
}

HWND GetTopLevelWindowFromPoint(POINT ptPoint)
{ /* Return the top-level window associated with the window under the mouse 
   * pointer (or NULL) 
   */
    HWND hWnd = WindowFromPoint(ptPoint);

    if(hWnd == NULL)
        return hWnd;    

    return ResolveWindow(hWnd);
}