cwindowswinapi

How to remove white border around client edge? (Win32 API)


Messing around with the Win32 API and I have this ugly white border around it when I apply styling to it that makes the window unresizable. How do I remove the border while still making the window unresizable?

void windowSetResizable(Window* window, bool canResize) {
    InternalWindow* internalWindow = getInternalWindow(window);
    
    int style = GetWindowLongPtr(internalWindow->handle, GWL_STYLE);
    if (!canResize) {
        SetWindowLongPtrA(internalWindow->handle, GWL_STYLE, style & ~(WS_THICKFRAME | WS_MAXIMIZEBOX | WS_EX_DLGMODALFRAME));
    } else {
        SetWindowLongPtrA(internalWindow->handle, GWL_STYLE, style | (WS_THICKFRAME | WS_MAXIMIZEBOX));
    }
    
}

Win32 unresizable window


Solution

  • After a bit of playing around with the code provided by @adrid, I was able to resolve it by simply stripping WS_THICKFRAME and WS_MAXIMIZEBOX from the Window style.

    If WS_BORDER is also stripped, the window can't have a title bar which is not explicitly stated in the documentation. Below shows a before and after.

    Window with WS_BORDER style stripped.

    Window with WS_BORDER style not stripped.

    Final Code:

    void windowSetResizable(Window* window, bool canResize) {
        InternalWindow* internalWindow = getInternalWindow(window);
        int style = GetWindowLongPtr(internalWindow->handle, GWL_STYLE);
    
        if (!canResize) {
            style &= ~(WS_THICKFRAME | WS_MAXIMIZEBOX);
        } else {
            style |= (WS_THICKFRAME | WS_MAXIMIZEBOX);
        }
        SetWindowLongPtrA(internalWindow->handle, GWL_STYLE, style);
        
        SetWindowPos(internalWindow->handle, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
    }