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”).

 

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

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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