0

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

这是我当前的测试功能(我现在在主回调过程中的 WM_MOUSEMOVE 上执行此操作以供笑)

case WM_MOUSEMOVE:
{
    POINT pt;
    GetCursorPos(&pt);
    HWND newHwnd = WindowFromPoint(pt);

    if (newHwnd != g_hSelectedWindow)
    {
        cout << "changing windows" << endl;
        cout << SetWindowPos(newHwnd, HWND_TOP, 0,0,0,0, SWP_NOMOVE|SWP_NOSIZE) << endl;
        g_hSelectedWindow = newHwnd;
    }

    CallWindowProc(listproc, hwnd,message,wParam,lParam);
    break;
}

我尝试使用 AllowSetForegroundWindow 但它帮助它在给定范围内找不到它,但我已经包含了 .

任何帮助或建议将不胜感激。

4

1 回答 1

1

AllowSetForegroundWindow不会有帮助,除非另一个窗口尝试通过调用SetForegroundWindow.

我很好奇,如果您需要将另一个窗口带到前台,为什么不直接调用SetForegroundWindow它呢?

更新:所以这是你需要让它正常工作的代码:

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);
}

只需GetTopLevelWindowFromPoint(pt)从您的WM_MOUSEMOVE处理程序调用,如果您返回一个有效的 HWND,那么它将是一个顶级窗口,可以使用 SetForegroundWindow 将其带到前台。

我希望这有帮助。

于 2012-11-27T05:36:30.953 回答