Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing WM_NCHITTEST to return HTCAPTION, with a custom cursor..?

I have created a borderless window that uses a wndProc() function that forces the WM_NCHITTEST case to return HTCAPTION;, which allows the user to drag the window, no matter where his cursor is located.
The problem is that I have set up a custom cursor, but with the abovementioned method, the cursor is always set back to IDC_ARROW.
How do I fix this?

EDIT: I've also tried using SetCursor() in the WM_NCHITTEST case, but it didn't work.


1 Answers

You can use the WM_SETCURSOR message to override the cursor. The LOWORD of lParam indicates the hit test code, the one that you altered with the WM_NCHITTEST handler. This worked well:

static HCURSOR CustomCursor;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_NCHITTEST: {
        LRESULT result = DefWindowProc(hWnd, message, wParam, lParam);
        if (result == HTCLIENT) result = HTCAPTION;             
        return result;
    }
    case WM_SETCURSOR: 
        if (LOWORD(lParam) == HTCAPTION) {
            SetCursor(CustomCursor);
            return TRUE;
        }
        return DefWindowProc(hWnd, message, wParam, lParam);
    // etc...
}

Initialize CustomCursor in your window init. Say:

CustomCursor = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_SIZEALL));
like image 87
Hans Passant Avatar answered Oct 18 '25 14:10

Hans Passant