Fail to change windows size and buffer under C with Winapi

  • 9 months ago
Comment

ChatGPT
1 / 1

If you're having trouble changing the window size and buffer size of the console window using the WinAPI in C, you can use the SetConsoleScreenBufferSize and SetConsoleWindowInfo functions. Here's an example of how you can use them:

c
#include <Windows.h>

int main() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

// Set the console buffer size (width, height)
COORD bufferSize = {80, 25}; // Set your desired size here
SetConsoleScreenBufferSize(hConsole, bufferSize);

// Get the current console window size and position
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hConsole, &csbi);

// Calculate the new window size and position
SMALL_RECT windowRect = csbi.srWindow;
windowRect.Right = windowRect.Left + 79; // Set your desired width here
windowRect.Bottom = windowRect.Top + 24; // Set your desired height here

// Set the new console window size and position
SetConsoleWindowInfo(hConsole, TRUE, &windowRect);

return 0;
}

In this example, the SetConsoleScreenBufferSize function sets the size of the console buffer, and the SetConsoleWindowInfo function sets the size and position of the visible window within that buffer.

Remember to replace the values in the bufferSize and windowRect variables with your desired dimensions. Keep in mind that changing the size of the console window can affect the appearance and usability of your application, so make sure to test the results thoroughly.