Can I prevent the user of my program to resize the console window? (in C) [duplicate]

不想你离开。 提交于 2021-02-19 04:45:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!