Help with closing Win32 app.

Hey all,

As an attempt to neaten my code and allow me to handle exceptions a little easier I tried to wrap most of the winapi calls into classes that i made. The program (just a window at the moment) mostly works.

My problems are this:

1) Upon closing, the window is destroyed, but the CPU usage will then jump to 50% and the process keeps running. - Solved
2) The cursor won't redraw, i.e. it goes to the 'hourglass' and won't change unless i click, or move it to somewhere where it changes (outside the window) and then bring it back to the window.

If anyone could shed some light on this, I would be much appreciated, and any Constructive criticism or advice regarding my code or methods are very much appreciated :)


Code is as below:

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "mainWnd.h"

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmdARgs, int nShowCmd )
{
	try
	{
		//Initialization of main window
		mainWin mainWindow( hInstance );

		//Show main window
		mainWindow.displayWindow();

		//Main loop
		mainWindow.msgLoop();

		return mainWindow.msg.wParam;
	}
	catch( int err )
	{
		handleError( err );
	}
}



mainWnd.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <windows.h>
#include "exception.h"

class mainWin
{
private:
	HWND hWnd;
	WNDCLASSEX wc;
	HINSTANCE hInstance;
	MSG msg;

	static LRESULT CALLBACK mWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
	void setWndProp();
	void createWin();

	char *szClassName;

public:
	mainWin( HINSTANCE hInstanceParam );
	~mainWin() {};
	
	//Main program loop
	void msgLoop();

	//Display & Update window
	void displayWindow();

	
	MSG returnMsg() { return msg; };

};


mainWnd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "mainWnd.h"

mainWin::mainWin( HINSTANCE hInstanceParam )
{
	szClassName = "mainClass";

	//Handle to the main program
	hInstance = hInstanceParam;
	
	//Register window class and create window
	setWndProp();
	createWin();
}

void mainWin::setWndProp()
{
	//Window class properties
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.style = 0;
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.hCursor = LoadCursor( hInstance, IDC_ARROW );
	wc.hIcon = LoadIcon( hInstance, IDI_APPLICATION );
	wc.hIconSm = LoadIcon( hInstance, IDI_APPLICATION );
	wc.hInstance = hInstance;
	wc.lpfnWndProc = mWndProc;
	wc.lpszClassName = szClassName;
	wc.lpszMenuName = NULL;

	//Throw error if class does not register
	if( !RegisterClassEx( &wc ) )
		throw CLASS_REG_FAIL;
}

void mainWin::createWin()
{
	hWnd = CreateWindowEx( WS_EX_CLIENTEDGE, szClassName, "Test Window",
			WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
			1024, 768, NULL, NULL, hInstance, NULL );
	
	//Throw error if window creation fails
	if( hWnd == NULL )
		throw WIN_CREATE_FAIL;
}

LRESULT CALLBACK mainWin::mWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch( msg )
	{
	case WM_CLOSE:
		DestroyWindow( hWnd );
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	case WM_LBUTTONDOWN:
		MessageBox( hWnd, "You Clicked on the screen", "CLICKETY CLICK", NULL );
		break;
	default:
		return DefWindowProc( hWnd, msg, wParam, lParam );
	}
	return 0;
}

void mainWin::msgLoop()
{
	while( GetMessage( &msg, hWnd, 0, 0 ) )
	{
		TranslateMessage( &msg );

		DispatchMessage( &msg );
	}
}

void mainWin::displayWindow()
{
	ShowWindow( hWnd, SW_SHOW );

	UpdateWindow( hWnd );
}


exception.h
1
2
3
4
#define CLASS_REG_FAIL 1
#define WIN_CREATE_FAIL 2

int handleError( int errorCode );


exception.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "exception.h"
#include <windows.h>

int handleError( int errorCode )
{
	switch( errorCode )
	{
	case CLASS_REG_FAIL:
		MessageBox( NULL, "Class Registration Failed", "Exception Raised", MB_OK | MB_ICONERROR );
		break;
	}
	return errorCode;
}

Last edited on
Looks good to me. Try setting no cursor at all. Set mainWnd.cpp:23 to NULL.
Topic archived. No new replies allowed.