Setting the precise dimensions of a window

I wrote this little program to let me set the exact dimensions of any window on my desktop. This is something I often need to do for taking screenshots that fit nicely in a blog post. The complete C code is provided below. I compiled it using gcc (from MinGW). Here’s a link to the executable version:

windowsize.exe (17KB)

// windowsize.c - Set the window underneath this one to 640x640px
// Written by Ted Burke - last updated 28-11-2012
// To compile:
//		gcc -o windowsize.exe windowsize.c
// To run (this example sets the window with the title
// "Untitled - Notepad" to size 640x480:
//		windowsize "Untitled - Notepad" 320 240

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

int main (int argc, char** argv)
	int w,h; // requested width and height
	HWND wh; // handle of specified window

	// Verify that command line arguments are valid
	if ((argc != 4) || !(w = atoi(argv[2])) || !(h = atoi(argv[3])))
		printf("Usage: windowsize WINDOW_TITLE WIDTH HEIGHT\n");
		return 1;
	// Get handle of window with matching title
	if (!(wh = FindWindowEx(NULL, NULL, NULL, argv[1])))
		printf("Error: window not found\n");
		return 1;

	// Resize window
	MoveWindow(wh, 100, 100, w, h, TRUE);

To use this program to resize a window with the title “Untitled – Notepad”, run it using the following command:

windowsize "Untitled - Notepad" 320 240

Here’s how my Notepad window looked after I ran that command:

Screenshot of resized Notepad window

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: Logo

You are commenting using your 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 )

Google+ photo

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

Connecting to %s