Removing window border?
Asked Answered
L

4

34

I have a window with a solid border around it. How can I remove the border (all of the non-client area) by using SetWindowLong and GetWindowLong?

Lolanthe answered 8/3, 2010 at 1:10 Comment(0)
D
61

In C/C++

LONG lStyle = GetWindowLong(hwnd, GWL_STYLE);
lStyle &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU);
SetWindowLong(hwnd, GWL_STYLE, lStyle);

WS_CAPTION is defined as (WS_BORDER | WS_DLGFRAME). You can get away with removing just these two styles, since the minimize maximize and sytem menu will disappear when the caption disappears, but it's best to remove them as well.

It's also best to remove the extended border styles.

LONG lExStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
lExStyle &= ~(WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE);
SetWindowLong(hwnd, GWL_EXSTYLE, lExStyle);

And finally, to get your window to redraw with the changed styles, you can use SetWindowPos.

SetWindowPos(hwnd, NULL, 0,0,0,0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER);
Denishadenison answered 8/3, 2010 at 10:1 Comment(6)
@NathanGoings Can you please explain why this is outdated in your opinion and what is up to date instead?Juarez
@RamboNo5: SetWindowLong() has been superseded by the SetWindowLongPtr function()Allhallowtide
@Allhallowtide I see, thanks. But as long as you're targeting 32-bit Windows only, it's all the same.Juarez
As a remark, using WS_MAXIMIZE and WS_MINIMIZE seems to glitch the window under Windows 10 setting its state as Minimized/Maximized. You should replace these with WS_MAXIMIZEBOX and WS_MINIMIZEBOX to avoid this problemAtalya
WS_MAXIMIZE and WS_MINIMIZE affect the window's initial state.Disinfect
Any ideas on how to restore the borders?Zamindar
R
11

The following Delphi codes does it:

  SetWindowLong(Handle, GWL_STYLE, GetWindowLong(Handle, GWL_STYLE) and not WS_BORDER and not WS_SIZEBOX and not WS_DLGFRAME );
  SetWindowPos(Handle, HWND_TOP, Left, Top, Width, Height, SWP_FRAMECHANGED);

Of course, these API calls look the same in all languages.

Ribose answered 8/3, 2010 at 1:18 Comment(0)
T
8

This line of code below removes the border of any given window, and remains only its client:

SetWindowLong(hWnd /*The handle of the window to remove its borders*/, GWL_STYLE, WS_POPUP);

You can use WS_POPUPWINDOW instead in the third parameter of SetWindowLong function. It also removes the borders of the given window and works too, but the difference is that it also draws outlined black rectangle all over the remaining client of the window. The thickness of that outlined rectangle is 1 pixel. WS_POPUP doesn't draw that rectangle, actually it doesn't draw anything, just only remove window's borders.

If you are about to return back the borders of the window, before you use that line of code I posted above, call first that line of code below:

GetWindowLong(hWnd /*The handle of the window that you want to remove its borders and later return them back to it*/, GWL_STYLE);

but of course that this function retuns the styles of the window, so create new variable that will keep these styles, i.e. set this variable to the return value of that function.

Then you use SetWindowLong as I showen above to remove its borders, and when you want later to restore its borders back, just recall again SetWindowLong, the first two parameters are same (hWnd and GWL_STYLE), but the third parameter is the styles of the window that returned from GetWindowLong. If you don't want to call GetWindowLong, but still return the borders of the window, then you can use SetWindowLong with the same first two parameters, and in the third parameter, you can use one of the following: WS_OVERLAPPED or/and WS_OVERLAPPEDWINDOW or/and WS_SIZEFRAME.

NOTE: If you try my answer, but it doesn't work for you, this can be, because that the both functions SetWindowLong and GetWindowLong have been superseded and doesn't work for you, and that because they are compatible with only 32-bit version of Windows. Probably you are using 64-bit version of Windows, then use SetWindowLongPtr and GetWindowLongPtr instead, which are compatible with both 32-bit and 64-bit versions of Windows. MSDN informs that about these functions in the Note section. Just search for them in that site. Here are the links to them:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms633591(v=vs.85).aspx http://msdn.microsoft.com/en-us/library/windows/desktop/ms633584(v=vs.85).aspx

Hope that all this answers your question.

Toledo answered 24/8, 2014 at 11:16 Comment(1)
Thanks for reminding to use WS_POPUP, it is single method working with creating already borderless windows.Gavage
O
1

This removes the title bar, and the vertical scroll bars...

int main()
{
    HWND hwnd = GetConsoleWindow();
    // remove title bar
    LONG lStyle = GetWindowLong(hwnd, GWL_STYLE);
    lStyle &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU);
    SetWindowLong(hwnd, GWL_STYLE, lStyle);
    //remove vertical scrollbar
    ShowScrollBar(hwnd, SB_VERT, FALSE);

    cout << "Hello World! \n";
    system("pause");

    return 0;
}
Optic answered 13/6, 2018 at 9:22 Comment(2)
Do you know how to remove the border, but keep the title bar ?Premiership
just use ShowScrollBar(hwnd, SB_VERT, FALSE);Optic

© 2022 - 2024 — McMap. All rights reserved.