Is there a win32 function to change the style of a window after it has been created? I would like to change the style flags that are specified in CreateWindowEx
. Specifically, I would like to convert a standard window to a window with no border and no resize.
Asked
Active
Viewed 3.0k times
21
5 Answers
20
I think SetWindowLongPtr
should do that. Note that you need to call SetWindowPos
afterwards if you changed the border style, as pointed out in the remarks.
Some styles only take effect during window creation and so can not be set by this call. MSDN normally calls out styles that CAN be set afterwards.
2
HWND windowHandle = FindWindow(NULL, L"Various tests");
SetWindowLongPtr(windowHandle, GWL_STYLE, WS_SYSMENU); //3d argument=style
SetWindowPos(windowHandle, HWND_TOPMOST, 100, 100, Width, Height, SWP_SHOWWINDOW);
did it for me :D

Charlie
- 585
- 9
- 17
1
You should try this window style in the createwindowex or SetWindowLongPtr: WS_POPUPWINDOW|WS_TABSTOP |WS_VISIBLE
-
3If you change a windows properties after it is created, you will ned to use SetWindowPos with the correct flags for the update to take effect. – Gunner Sep 25 '12 at 01:52
0
The way i solved it by using combination of SetWindowPos and ShowWindow methods.
NOTE that calling showWindow is must here otherwise it won't work.
Here is the full source code below. Just call setConsoleWindowStyle() method and set new window style.
#define _WIN32_WINNT 0x0501
#include <stdio.h>
#include <windows.h>
LONG_PTR setConsoleWindowStyle(INT,LONG_PTR);
int main()
{
LONG_PTR new_style = WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL;
setConsoleWindowStyle(GWL_STYLE,new_style);
return 0;
}
LONG_PTR setConsoleWindowStyle(INT n_index,LONG_PTR new_style)
{
/*The function does not clear the last error information. if last value was zero.*/
SetLastError(NO_ERROR);
HWND hwnd_console = GetConsoleWindow();
LONG_PTR style_ptr = SetWindowLongPtr(hwnd_console,n_index,new_style);
SetWindowPos(hwnd_console,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_DRAWFRAME);
//show window after updating
ShowWindow(hwnd_console,SW_SHOW);
return style_ptr;
}

Haseeb Mir
- 928
- 1
- 13
- 22
-
`ShowWindow` is not a must, pass `SWP_SHOWWINDOW` to `SetWindowPos` instead. – metablaster May 05 '20 at 22:44
0
SetWindowLong(hWnd, GWL_STYLE, newStyle); ShowWindow(hWnd, SW_SHOW);

Denis
- 1
-
5Would be helpful if you explained why this worked rather than just posting the function – chevybow Oct 29 '18 at 19:26