Create small win32 window which is both draggable and clickable

Shabari Pragash 126 Reputation points

Created a small window

    HWND hWnd = CreateWindowW(szWindowClass, NULL, WS_POPUP | WS_BORDER,
        1200, 800, 60, 60,
        nullptr, nullptr, hInstance, nullptr);

Dragging window is working when the following code is added in window procedure,

      case WM_NCHITTEST:
      return HTCAPTION;

But unfortunately dragging stopped working after i added a button in the window. I need to capture click event so added a button. Click event is working, i.e., i get the message box when i click.

        MessageBoxW(NULL, L"WM_COMMAND", L"msgbox", MB_OK);
        hButton = CreateWindow(L"BUTTON", NULL,
            WS_CHILD | WS_VISIBLE, 0, 0, 60, 60, hWnd, 0, NULL, NULL);

I like to have both dragging and clicking to work.

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,508 questions
{count} votes

1 answer

Sort by: Most helpful
  1. RLWA32 42,876 Reputation points

    Implement the additional code described in the answer here - how-do-i-implement-dragging-a-window-using-its-client-area. If your button takes the entire client area of the parent window then you need to drag the window by using its border. So test for both HTCLIENT and HTBORDER.

    2 people found this answer helpful.
    0 comments No comments