Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C++

Win32 Simple Application AppWizard

Rate me:
Please Sign up or sign in to vote.
4.76/5 (7 votes)
22 Jun 20045 min read 92.4K   1K   37  
An article on creating an AppWizard to create Win32 based applications.
// $$Root$$.cpp : Defines the entry point for the application.
//

#include "stdafx.h"

LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
void Init(HWND);

$$IF(USE_WINSOCK)
WSADATA wsaData;
$$ENDIF
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	MSG msg;
	WNDCLASS wnd;
	HWND hWnd;

	static TCHAR szAppName[] = TEXT ("$$Root$$") ;

	wnd.style			= CS_HREDRAW | CS_VREDRAW;
$$IF(WINDOW_BASED)
	wnd.cbWndExtra		= 0;
$$ELSE
	wnd.cbWndExtra		= DLGWINDOWEXTRA;
$$ENDIF // WINDOW_BASED
	wnd.cbClsExtra		= 0;
	wnd.hCursor			= LoadCursor(NULL,MAKEINTRESOURCE(IDC_ARROW));
	wnd.hIcon			= LoadIcon(NULL,MAKEINTRESOURCE(IDI_WINLOGO));
	wnd.hInstance		= hInstance;
	wnd.lpfnWndProc		= WindProc;
	wnd.lpszClassName	= szAppName;
	wnd.lpszMenuName	= NULL;
	wnd.hbrBackground	= (HBRUSH)(COLOR_WINDOW);

	if(!RegisterClass(&wnd))
	{
		return 0;
	}
	
$$IF(WINDOW_BASED)
	hWnd = CreateWindow(wnd.lpszClassName,
						"$$Root$$",
						WS_BORDER |
						WS_OVERLAPPED|
						WS_VISIBLE|
						WS_SYSMENU,
						CW_USEDEFAULT,
						CW_USEDEFAULT,
						300,			// change to desired width
						300,			// change to desired height
						NULL,
						NULL,
						hInstance,
						0
						);
$$ELSE
	hWnd = CreateDialog(hInstance,szAppName,NULL,NULL);
$$ENDIF // WINDOW_BASED

$$IF(INIT_COMMON_CONTROLS)
	InitCommonControls();
$$ENDIF // INIT_COMMON_CONTROLS

	PostMessage(hWnd,WM_COMMAND,WM_INITAPPLICATION,0);

	ShowWindow(hWnd,nCmdShow);
	UpdateWindow(hWnd);

	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}

LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message) {
	
	    case WM_CREATE:
	        {
	            
	            break;
	        }

		case WM_DESTROY:
			{
$$IF(USE_WINSOCK)
				WSACleanup();
$$ENDIF
				PostQuitMessage(0);
				break;
			}
			
		case WM_COMMAND:
			{
				switch(LOWORD(wParam)) {
				
					case WM_INITAPPLICATION:   
						{
							Init(hwnd);
							break;
						}
				    
				    default:
						break;
				}

				break;
			}

		case WM_PAINT:
			{
				break;
			}
	    
	    default:
			return DefWindowProc(hwnd,message,wParam, lParam);
	}

	return DefWindowProc(hwnd,message,wParam, lParam);
}

void Init(HWND hWnd)
{
$$IF(USE_WINSOCK)
	WORD wVersionRequested;
	int err;
 
	wVersionRequested = MAKEWORD( 2, 2 );
 
	err = WSAStartup( wVersionRequested, &wsaData );
	if ( err != 0 ) {
		/* Tell the user that we could not find a usable */
		/* WinSock DLL.       
		*/
		MessageBox(hWnd,"Couldn't find a usable Winsock DLL","Winsock Error",MB_OK);
		return;
	}
 
	/* Confirm that the WinSock DLL supports 2.2.*/
	/* Note that if the DLL supports versions greater    */
	/* than 2.2 in addition to 2.2, it will still return */
	/* 2.2 in wVersion since that is the version we      */
	/* requested.                                        */
 
	if ( LOBYTE( wsaData.wVersion ) != 2 ||
			HIBYTE( wsaData.wVersion ) != 2 ) {
		/* Tell the user that we could not find a usable */
		/* WinSock DLL.                                  */
		WSACleanup( );
		MessageBox(hWnd,"Couldn't find a usable Winsock DLL","Winsock Error",MB_OK);
		return; 
	}
 
	/* The WinSock DLL is acceptable. Proceed. */
$$ENDIF

	// initialize data here
}



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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Italy Italy
Worked as a Java developer targeting J2EE and J2SE for half a decade.

Now he's employed for Avanade as a Senior Associate Consultant on Microsoft technologies.

His hobbies are Win32 programming using SDK, MFC, ATL and COM; playing guitar and listening to music.

Comments and Discussions