vanja.
vanja.

Reputation: 2572

Change Win32 Window Style

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.

Upvotes: 21

Views: 34521

Answers (5)

Denis
Denis

Reputation: 1

SetWindowLong(hWnd, GWL_STYLE, newStyle); ShowWindow(hWnd, SW_SHOW);

Upvotes: 0

Charlie
Charlie

Reputation: 653

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

Upvotes: 2

HeavenHM
HeavenHM

Reputation: 992

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

Upvotes: 0

comment
comment

Reputation: 11

You should try this window style in the createwindowex or SetWindowLongPtr: WS_POPUPWINDOW|WS_TABSTOP |WS_VISIBLE

Upvotes: 1

Joey
Joey

Reputation: 354356

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.

Upvotes: 20

Related Questions