问题
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?
回答1:
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.
回答2:
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);
Thank you guys for helping!
来源:https://stackoverflow.com/questions/47358043/can-i-prevent-the-user-of-my-program-to-resize-the-console-window-in-c