How a console application can set the size of its window

This is an example of a Win32 console application that sets the size of the window it’s running in. When you run it, it sets the size of the console window to 640 x 400 pixels. It actually seems to get rounded to an even multiple of the character width, which in this case produces a window width of 336 pixels.

It uses two Win32 functions to complete the task:

  • GetConsoleWindow is used to get a handle to the console window.
  • MoveWindow is then used to set the size and position of the window.

Here’s the source code:

// move_window.c - Move a console window to specific
// screen coordinates.
// Written by Ted Burke - last modified 17-4-2012
// To compile: gcc -o move_window.exe move_window.c

#define WINVER 0x0500
#include <windows.h>

int main (void)
	// Get console window handle
	HWND wh = GetConsoleWindow();

	// Move window to required position
	MoveWindow(wh, 100, 100, 640, 250, TRUE);

Here’s a screen shot of the console window I used to compile and run the program. In this screenshot, because the program has already been run, the window has already been resized.

By the way, to get rid of the scroll bars from the console window for this screenshot, I modified some of the console properties as follows (this settings dialog can be opened by clicking on the little icon on the left side of the window’s title bar and selecting “Properties”).


This entry was posted in Uncategorized and tagged , , , , , , , , . Bookmark the permalink.

2 Responses to How a console application can set the size of its window

  1. anil meravat says:

    can i set the size of the font in console output using a c or cpp program?

    • batchloaf says:

      Hi Anil, Yes I think it’s possible, but I’m afraid I’m not sure exactly how to do it. I suggest you google functions like GetCurrentConsoleFontEx and SetCurrentConsoleFontEx. I tried to compile an example on my laptop, but it seems like those functions are not available in MinGW and I don’t have Visual Studio installed.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s