So I was searching (a lot) and haven't find anything on how to prevent user from resizing my program's console window. I had found information for language C++
and C#
but not for C
. I already managed to set the size of the console but if the user changes it afterwards it is not good for my program's looking. Is there anything I can do to perfectly resize the console and keep it that way?
Asked
Active
Viewed 7,733 times
3

robinCTS
- 5,746
- 14
- 30
- 37

Marcell Zahorán
- 153
- 1
- 10
-
Which OS are you using? – Weather Vane Nov 17 '17 at 20:01
-
I am using Windows, yeah thank you for asking I forgot to put it there. – Marcell Zahorán Nov 17 '17 at 20:04
-
Many of the Windows API functions can be called from C even when the MS documentation tags C++. – Weather Vane Nov 17 '17 at 20:06
-
well you could include windows.h header file in your program and then look for a function that would make your program window size fixed. – Ahtisham Nov 17 '17 at 20:58
-
Take look at the following http://zetcode.com/gui/winapi/ it will teach you how use windows api with C. – Ahtisham Nov 17 '17 at 21:02
-
Yeah I have seen that one, maybe I give it some time and read the whole thing later on. – Marcell Zahorán Nov 17 '17 at 21:04
-
For c++ developers there is this cool framework know as Qt which uses windows api by default and you can create beautiful apps with it. Try it sometimes. – Ahtisham Nov 17 '17 at 21:08
-
@zett42 undefined reference to 'GetConsoleWindow' - it is a C++ solution. – Marcell Zahorán Nov 17 '17 at 21:15
-
`GetConsoleWindow` is a C function. As stated in the reference, define `_WIN32_WINNT` as 0x0500 or later and include "Windows.h". – zett42 Nov 17 '17 at 21:16
-
Then why is my compiler says 'undefined reference'? I already included windows.h. – Marcell Zahorán Nov 17 '17 at 21:18
-
define _WIN32_WINNT as 0x0500 or later – zett42 Nov 17 '17 at 21:21
-
Your edit is not required and just adds noise. – robinCTS Nov 18 '17 at 01:46
2 Answers
12
Okay so I managed to do the magic with combining codes. First of all you need a
#define _WIN32_WINNT 0x0500
and after that (the order is important)
#include <windows.h>
and after all this you need this code in your main:
HWND consoleWindow = GetConsoleWindow();
SetWindowLong(consoleWindow, GWL_STYLE, GetWindowLong(consoleWindow, GWL_STYLE) & ~WS_MAXIMIZEBOX & ~WS_SIZEBOX);

phuclv
- 37,963
- 15
- 156
- 475

Marcell Zahorán
- 153
- 1
- 10
-
Much better than my suggestion! I'll let my answer stand however since there are other useful things that can be done with console WinEvents. – Clifford Nov 17 '17 at 21:36
2
Trap the EVENT_CONSOLE_LAYOUT event by setting an event hook with SetWinEventHook, and in the WinEventProc callback call SetConsoleWindowInfo to immediately restore the desired size.
You might also set the window buffer to the exact dimensions of the visible window using SetWindowScreenBufferSize since it is not possible to make the visible window larger than the buffer size. This will not prevent making the windows smaller however.

Clifford
- 88,407
- 13
- 85
- 165