Create small win32 window which is draggable

Shabari Pragash 126 Reputation points
2022-05-03T18:50:31.387+00:00

I want to create a small win32 window which is draggable.

I tried to create a small window by using the following code where i have specified the width and height of window to be 60, 60 resp. But the created window looks like it is 120 width and 60 height i.e., it has slightly larger width. I think it has added empty title bar/caption bar and hence width became larger. I have attached the image of window for your reference. This window is draggable.

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

198550-screenshot-287.png

Then i changed the window style to WS_POPUP, which created window with width and height as 60 and 60 resp. But this window is not draggable. It's position is fixed. I have attached the image of window for your reference.

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

198526-screenshot-289.png

Kindly let me know what i need to change so that i can create small draggable window.

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,429 questions
{count} vote

Accepted answer
  1. RLWA32 40,771 Reputation points
    2022-05-03T19:48:59.033+00:00

    Window procedure to drag a window without a caption (title bar) created with WS_POPUP | WS_BORDER. Hold down the left mouse button anywhere in the window and drag it.

    LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
     switch (msg)
     {
     case WM_NCHITTEST:
     return HTCAPTION;
     break;
     case WM_DESTROY:
     PostQuitMessage(0);
     break;
    
     default:
     return DefWindowProcW(hwnd, msg, wParam, lParam);
     }
    
     return 0;
    }
    
    0 comments No comments

0 additional answers

Sort by: Most helpful