Search code examples
c#c++windowsmaximize-windowaccessibility-api

How determine if mouse points to maximise button of window under cursor


How determine if mouse points to(hover on) maximise button of window even if this window is not of my application. Is there API for that ?


Solution

  • You may send a WM_NCHITTEST to that window. The return value will correspond to the object type on the requested coordinates.

    Something like this:

    bool IsMouseOverMaxBtn(HWND hWnd)
    {
        POINT pt;
        VERIFY(GetCursorPos(&pt)); // get mouse position
    
        int retVal = SendMessage(hWnd, WM_NCHITTEST, 0, MAKELONG(pt.x, pt.y));
    
        return HTMAXBUTTON == retVal;
    }
    

    Edit:

    You may send this message to any window (not necessarily belong to your thread/process). Since no pointers are involved (such as string pointers) - there's no problem.

    However you should note that sending (not posting) a message to a window belonging to another thread is a pretty heavy operation, during which your thread is suspended. There may even happen a situation where your thread hangs, because the thread of the application that serves that window hangs.

    You may consider using SendMessageTimeout to guarantee your thread won't hang.