Click here to Skip to main content
12,253,114 members (53,550 online)
Click here to Skip to main content

Stats

100.1K views
1.9K downloads
67 bookmarked
Posted

Win32 Window Minimal

, 14 Aug 2012 CPOL
Getting started with Win32 programming through creating a window with minimum code and complexity
winbase
winbase.vcproj.merlin-PC.merlin.user
winbase.cbp
#include <windows.h>	/* This enables access to Microsoft Windows specific data types that are required to use the Graphical User Interface of Windows. (HWND, WNDCLASSEX, etc.)	*/

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); /* A 'Forward Declaration' of a function that will mainly handle user interaction with our window.	*/

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) /* The 'entry' point of the program. Console programs use the simple main(...)		*/
{
	MSG msg				= {0};							/* Required to detect user clicking on the X of the window which equals to sending a 'WM_CLOSE' 'message'					*/
	WNDCLASS wc			= {0};							/* Required information that the Microsoft Windows Operating System needs of a new window are stored in this struct.		*/
	wc.lpfnWndProc		= WndProc;						/* Set which function will receive this windows messages, for example: clicking on this windows X							*/
	wc.hInstance		= hInstance;					/* Windows uses this to keep track of multiple instances of this program with the same window class name.					*/
	wc.hbrBackground	= (HBRUSH)(COLOR_BACKGROUND);	/* This is the background color to use for this window. It conforms to custom window background colors defined by the user	*/
	wc.lpszClassName	= L"minwindowsapp";				/* This is the base name of this type of a window, and can be reused to create the same 'style' windows easier again.		*/
	if( !RegisterClass(&wc) )							/* This asks the operating system if it has resources to allow us to register a new 'style' of a window to be used.			*/
		return 1;										/* If there were no resources available, the program will terminate by returning the number 1. (0 usually means success)	*/

	if( !CreateWindow(	wc.lpszClassName,				/* the '...if(!...' part will perform the 'return 2;' command if there was an error during the creation of the window.*/
						L"Minimal Windows Application", /* This is the actual name shown in the 'title bar' of the window. The L before the quotes, simplified, means its UNICODE.	*/
						WS_OVERLAPPEDWINDOW|WS_VISIBLE, /* WS_OVERLAPPEDWINDOW gives the window a title bar, a window menu, a sizing border, and minimize and maximize buttons.		*/
						0,								/* Initial X position of the window.																						*/
						0,								/* Initial Y position of the window.																						*/
						640,							/* Initial Width of the window.																								*/
						480,							/* Initial Height of the window.																							*/
						0,								/* Here could be a 'handle' to the parent or owner window of this window.													*/
						0,								/* Here could be a 'handle' to a menu (usually constructed with the GUI tools of Visual Studio).							*/
						hInstance,						/* The 'handle' or 'unique id' of the instance of this program that will be associated with the to-be-created window(class).*/
						NULL))							/* This last parameter is used in advanced windows programming like abstracting the functionality of WndProc etc.			*/
		return 2;										/* Number 2 is returned to keep track in which part of this programs execution did the program fail. This style is optional.*/

	while( GetMessage( &msg, NULL, 0, 0 ) > 0 )			/* This checks user 'messages', and if our window is for example clicked, the 'message' will go to our WndProc function.	*/
	{
		DispatchMessage( &msg );						/* Sends the messages that were relevant to our window to the WndProc function we have defined for our window class.		*/
	}

	return 0;											/* Everything went ok, return 0 as a sign of successful program termination.												*/
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) /* The implementation of our forward declaration. A common 'message loop' or, 'Window Procedure'	*/
{
	switch(message)										/* The message that our window has received can be caught using various 'defines' like: WM_LBUTTONUP, WM_KEYDOWN, etc. :)	*/
	{
	case WM_CLOSE:										/* The user clicked the 'X' at the top right of the window, we caught the message with GetMessage() and processed it here :)*/
		PostQuitMessage(0);								/* This requests Windows the permission to terminate (quit). GetMessage catches that, returns false, and ends the while loop*/
		break;
	default:											/* If no 'case' condition matched the content of the message received, return DefWindowProc(...)							*/
		return DefWindowProc(hWnd, message, wParam, lParam); /* Our application specified no action for the received message(say for WM_KEYDOWN),pass the message along to 'others'.*/
	}
	return 0;											/* Zero return value from a window procedure usually means the message was not 'caught' or 'used' within the message switch.*/
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jani Mäkinen
Software Developer (Senior) Frozenbyte
Finland Finland
I've programmed since the 90's QBasic times and love programming tight and slick code.

Here are some links for my 'off the clock' programming: http://www.gfx0.com

Specialties: C, C++, Game Programming

I was born in the middle of Finland into a small farming town. Moved to Helsinki, then to Germany and am now back in Helsinki programming in the game industry Smile | :)

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 14 Aug 2012
Article Copyright 2011 by Jani Mäkinen
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid