Copyright: https://blog.csdn.net/yuanwow/article/details/89439077
Pure C language to set the window size
If you want to change the screen buffer can:
COORD size = {w, h};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
If you only need to change the window size can:
SMALL_RECT rc = {1,1, w, h};
SetConsoleWindowInfo(hOut ,true ,&rc);
Note: The window size can not exceed the buffer size, or modification will fail!
Function modeset(int w,int h)
:
void modeset(int w,int h) {
// 此函数设置窗口大小为 w*h
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD size = {w, h};
SetConsoleScreenBufferSize(hOut,size);
SMALL_RECT rc = {1,1, w, h};
SetConsoleWindowInfo(hOut ,true ,&rc);
return;
}
The complete code is:
#include <Windows.h>
void modeset(int w,int h) {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD size = {w, h};
SetConsoleScreenBufferSize(hOut,size);
SMALL_RECT rc = {1,1, w, h};
SetConsoleWindowInfo(hOut ,true ,&rc);
system("cls");
return;
}
int main(){
modeset(100,50);
return 0;
}