[ create a new paste ] login | about

Link: http://codepad.org/elkXnBM1    [ raw code | output | fork ]

C++, pasted on Sep 26:
/* Test Cases
The program simply displays a static line of text in a window which has a caption.
The WM_CLOSE message is handled seprately and exit is confirmed by the user.The application is closed only if the user presses ok on the mesasgebox
*/


#include <windows.h>		// Header file for window API in c++

// Prototypes
LRESULT WINAPI MainWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
//Global Variables
HANDLE ghInstance;

// creates and displays a window with static text on it
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow )
{
	static int i=0;
		WNDCLASS wc;
		MSG msg;
		HWND hWnd;
		BOOL bRet;

	if( !hPrevInstance ) {
		
			wc.lpszClassName = "GenericAppClass";						//creating an instance of Window Class and initializing it
			wc.lpfnWndProc = MainWndProc;
			wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
			wc.hInstance = hInstance;
			wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
			wc.hCursor = LoadCursor( NULL, IDC_ARROW );
			wc.hbrBackground = (HBRUSH)( COLOR_WINDOW+1 );
			wc.lpszMenuName = NULL;
			wc.cbClsExtra = 0;
			wc.cbWndExtra = 0;
			RegisterClass( &wc );
	}
	
	ghInstance = hInstance;

	//Creating the window
	hWnd = CreateWindow("GenericAppClass","Generic Application",WS_OVERLAPPEDWINDOW|WS_HSCROLL|   
						WS_VSCROLL,0,0,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
	
	ShowWindow( hWnd, nCmdShow );
	while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0 ) {
		if (bRet == -1) {
		// handle the error and possibly exit;
		} else {

			if(msg.message==WM_NCLBUTTONDOWN)
				Sleep(1000*10);
			/*MessageBox(NULL,"Message goes through Message loop","WM_CLOSE",MB_OK);*/

			/*if(msg.message==WM_CLOSE)
				MessageBox(NULL,"Message a normal message","WM_CLOSE",MB_OK);*/
			
			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}
	}

	return (int) msg.wParam;
}

//function writing a static text on the window
void WriteHelloWorld(HDC hDC)
{
	TextOut(hDC,50,50,"Hello World",11);
}

// TODO :: add Function-comments
LRESULT CALLBACK MainWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
		PAINTSTRUCT ps;
		HDC hDC;
	switch( msg ) {
				//same as WM_DESTROY
				/*case CTRL_CLOSE_EVENT:MessageBox(hWnd, "Called from task manager","Yippee",MB_OK);
									break;*/

				case WM_PAINT: hDC = BeginPaint( hWnd, &ps );
							   WriteHelloWorld(hDC);
							   TextOut( hDC, 10, 10, "Hello, Windows!", 15 );
							   EndPaint( hWnd, &ps );
							   break;

				case WM_CLOSE: if (MessageBox(hWnd, "Do you want to quit", "Exit", MB_OKCANCEL | MB_ICONQUESTION) == IDOK)
								  PostQuitMessage (WM_QUIT);
							   else
								  ShowWindow (hWnd, SW_RESTORE);
							   break;
								
				
				case WM_CREATE:
								break;
				
				//case WM_LBUTTONDOWN:/*MessageBox(NULL,"modal","",MB_OK);*/

				default:
							  return (DefWindowProc (hWnd, msg, wParam, lParam));		//calling the default window handler
				}
	return 0;
}


/*===========================================================================================================================================
TEST CASES

1. Close the window.
2. Output: Message box to confirm exit appears
	a. On Yes, window exits
	b. On No, window does not exit
=============================================================================================================================================*/


Output:
1
2
3
Line 58: error: windows.h: No such file or directory
Line 12: error: 'LRESULT' does not name a type
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: