Create small win32 window which is both draggable and clickable

Shabari Pragash 126 Reputation points
2022-05-10T14:41:40.137+00:00

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;
      break;

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.

case WM_COMMAND:
{
        MessageBoxW(NULL, L"WM_COMMAND", L"msgbox", MB_OK);
}
break;
case WM_CREATE:
{
        hButton = CreateWindow(L"BUTTON", NULL,
            WS_CHILD | WS_VISIBLE, 0, 0, 60, 60, hWnd, 0, NULL, NULL);
}
break;

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,431 questions
{count} votes

1 answer

Sort by: Most helpful
  1. RLWA32 40,856 Reputation points
    2022-05-10T14:49:51.403+00:00

    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