Skip to main content
GameDev.net gamedev.net
🔒 Locked

Object-orientizing Windows API

Started by elendil67 Oct 24, 2002 at 8:00 PM 3 replies 600+ views
Original Post
elendil67
elendil67
Hey all. I am working on an object-oriented C++ way to encapsulate the basic parts of the Windows API, especially with windows and messages. I have been working on it for a couple days now, and I must say I think I have a beautiful project here, and once I get passed this problem, after a little more coding, I''ll do window skinning and shapes. But I have a problem right now, and I have no clue what it is. My source is pretty long, and I have no idea where it might be, but if anyone can help me, I would be very glad. I am very sorry I can''t point you to a certain place in the code that doesn''t work, it''s just it compiles fine, I only get a runtime error when I run it. Thanks!
  
/////////////////////////////////////////////////////////////////////////

// CWindow.h - This is the header file where the CWindow class is

// declared. The definition of CWindow is in CWindow.cpp

/////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////

// Inclusion guards to make sure that this header file is not included

// infinitely.

/////////////////////////////////////////////////////////////////////////

#ifndef _CWINDOW_H_
#define _CWINDOW_H_


/////////////////////////////////////////////////////////////////////////

// Exclude MFC from this application, thus making it smaller and faster

/////////////////////////////////////////////////////////////////////////

#define WIN32_LEAN_AND_MEAN

/////////////////////////////////////////////////////////////////////////

// Include all of the header files needed for this program. Since it is

// a window class, all that is needed is the Windows header file.

/////////////////////////////////////////////////////////////////////////

#include <windows.h>


// Name: CWindow

//

// Desc: A class to automate the task of creating a window in the Windows

//       API. Takes care of most tasks in creating, maintaining, and 

//       destroying a window.

class CWindow
{
public:

	// Name: CWindow()

	//

	// Input: None

	//

	// Output: None

	//

	// Desc: Just your basic constructor to initialize any otherwise unsafe

	//       variables, or call functions that should be called at startup.

	CWindow();

	
	// Name: ~CWindow()

	//

	// Input: None

	//

	// Output: None

	//

	// Desc: Your basic destructor to release any otherwise unsafe variables

	//       or to call functions that should be called at shutdown

	~CWindow();


	// Name: CreateWnd()

	//

	// Input: Width, height, handle to instance, handle to previous intance,

	//        command line parameters

	//

	// Output: None

	//

	// Desc: The function to create the window. Takes the requested window

	//       width, and height. It creates a window through the Windows API.

	//       Called in WinMain().

	int CreateWnd(int width, int height, HINSTANCE hInstance, HINSTANCE hPrevInstance, int nShowCmd);


	// Name: DeleteWnd()

	//

	// Input: None

	//

	// Output: None

	//

	// Desc: The function used to delete the window. Takes no parameters,

	//       and simply deletes the window.

	void DeleteWnd();


	// Name: MessageHandler()

	//

	// Input: Message structure, message parameter, message parameter

	//

	// Output: None

	//

	// Desc: A function for handling messages. Not the default window

	//       procedure.

	int MessageHandler(HWND hWnd, UINT Msg, LPARAM lParam, WPARAM wParam);


	// Name: OnWindowCreate(), OnWindowMain(), OnWindowShutdown(),

	//		 OnWindowPaint()

	//

	// Input: None

	// 

	// Output: None

	//

	// Desc: Virtual functions that take care of basic application

	//       house keeping; the user can overload to make his or her own 

	//       responses to the events.

	virtual void OnWindowCreate();
	virtual void OnWindowMain();
	virtual void OnWindowShutdown();
	virtual void OnWindowPaint();


	// Name: OnMouseDown(), OnMouseUp(), OnMousePress()

	// 

	// Input: None

	//

	// Output: None

	//

	// Desc: Virtual functions taking care of the mouse input that the can 

	//       overload to make his or her own responses to the events.

	virtual void OnMouseDown();
	virtual void OnMouseUp();
	virtual void OnMousePress();

private:

	/////////////////////////////////////////////////////////////////////////

	// Private member variables and functions only accessible to the owner 

	// class.

	/////////////////////////////////////////////////////////////////////////

	HWND	   m_hWnd;		        										 // Handle to window

	HDC		   m_hDC;														 // Handle to device context

	WNDCLASSEX m_sWC;														 // Window class structure

	int		   m_nWidth;													 // Window width

	int		   m_nHeight;													 // Window height

	bool	   m_bAlive;													 // Window life toggle-dead or alive

	HICON      m_hIcon;													     // Handle to window icon

	HICON      m_hIconSm;													 // Handle to window small icon

	HCURSOR    m_hCursor;													 // Handle to window cursor

};


/////////////////////////////////////////////////////////////////////////

// Inclusion guards to make sure that this header file is not included

// infinitely.

/////////////////////////////////////////////////////////////////////////

#endif

/////////////////////////////////////////////////////////////////////////

// CWindow.cpp - This is the source file where the CWindow class is

// defined. The declaration of CWindow is found in CWindow.h

/////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////

// Include all of the headers needed for this file. All that is needed 

// in this file is the corresponding CWindow header, because that file

// includes all other headers needed for this application that this file

// will need to deal with.

/////////////////////////////////////////////////////////////////////////

#include "CWindow.h"


/////////////////////////////////////////////////////////////////////////

// Make a pointer to CWindow, because the following default window 

// procedure needs to call a function in CWindow (MessageHandler()).

/////////////////////////////////////////////////////////////////////////

CWindow *pWnd;

/////////////////////////////////////////////////////////////////////////

// Declare and define a window procedure function. Since the message

// handler of CWindow can''t be the default window procedure function,

// this is a generic one that simply passes all of the messages to the

// message handler of CWindow.

/////////////////////////////////////////////////////////////////////////

LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	/////////////////////////////////////////////////////////////////////////

	// Return with CWindow''s message handler, which passes on all of the

	// messages that need to be handled to that function.

	/////////////////////////////////////////////////////////////////////////

	return pWnd->MessageHandler(hWnd, Msg, wParam, lParam);
}


/////////////////////////////////////////////////////////////////////////

// Name: CWindow

//

// Desc: A class to automate the task of creating a window in the Windows

//       API. Takes care of most tasks in creating, maintaining, and 

//       destroying a window.

/////////////////////////////////////////////////////////////////////////



	/////////////////////////////////////////////////////////////////////////

	// Name: CWindow()

	//

	// Input: None

	//

	// Output: None

	//

	// Desc: Just your basic constructor to initialize any otherwise unsafe

	//       variables, or call functions that should be called at startup.

	/////////////////////////////////////////////////////////////////////////

	CWindow::CWindow()
	{
		/////////////////////////////////////////////////////////////////////////

		// Initialize all of the members of the window class.

		/////////////////////////////////////////////////////////////////////////

		m_nWidth  = 0;
		m_nHeight = 0;
		m_bAlive  = true;
		m_hIcon   = LoadIcon(NULL, IDI_APPLICATION);
		m_hIconSm = LoadIcon(NULL, IDI_APPLICATION);
		m_hCursor = LoadCursor(NULL, IDC_ARROW);
	}


	/////////////////////////////////////////////////////////////////////////

	// Name: ~CWindow()

	//

	// Input: None

	//

	// Output: None

	//

	// Desc: Your basic destructor to release any otherwise unsafe variables

	//       or to call functions that should be called at shutdown

	/////////////////////////////////////////////////////////////////////////

	CWindow::~CWindow()
	{
		/////////////////////////////////////////////////////////////////////////

		// Release all members of the window class

		/////////////////////////////////////////////////////////////////////////

		
		/////////////////////////////////////////////////////////////////////////

		// Destroy the window if it is still alive

		/////////////////////////////////////////////////////////////////////////

		if(m_bAlive == true)
		{
			/////////////////////////////////////////////////////////////////////////

			// Call the function to delete the window

			/////////////////////////////////////////////////////////////////////////

			this->DeleteWnd();
		}
	}


	/////////////////////////////////////////////////////////////////////////

	// Name: CreateWnd()

	//

	// Input: Width, height, handle to instance, handle to previous intance,

	//        command line parameters

	//

	// Output: None

	//

	// Desc: The function to create the window. Takes the requested window

	//       width, and height. It creates a window through the Windows API.

	//       Called in WinMain().

	/////////////////////////////////////////////////////////////////////////

	int CWindow::CreateWnd(int width, int height, HINSTANCE hInstance, HINSTANCE hPrevInstance, int nShowCmd)
	{
		/////////////////////////////////////////////////////////////////////////

		// Initialize the window class

		/////////////////////////////////////////////////////////////////////////

		m_sWC.cbSize        = sizeof(WNDCLASSEX);								 // Size of the window class

		m_sWC.style         = CS_VREDRAW | CS_HREDRAW | CS_OWNDC | CS_DBLCLKS;	 // Window style

		m_sWC.lpfnWndProc   = WndProc;											 // Pointer to window procedure

		m_sWC.cbClsExtra    = 0;												 // Extra class variable

		m_sWC.cbWndExtra    = 0;												 // Extra window variable

		m_sWC.hInstance     = hInstance;										 // Window instance

		m_sWC.hIcon         = m_hIcon;											 // Icon

		m_sWC.hCursor       = m_hCursor;										 // Cursor

		m_sWC.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);				 // Background color

		m_sWC.lpszMenuName  = NULL;												 // Menu

		m_sWC.lpszClassName = "CWindow";										 // Class name

		m_sWC.hIconSm       = m_hIconSm;									     // Small icon


		/////////////////////////////////////////////////////////////////////////

		// Register the window class

		/////////////////////////////////////////////////////////////////////////

		RegisterClassEx(&m_sWC);

		/////////////////////////////////////////////////////////////////////////

		// Create the window

		/////////////////////////////////////////////////////////////////////////

		if(!(m_hWnd = CreateWindowEx(NULL,										 // Extended parameters

									 "CWindow",									 // Class name

									 "CWindow",									 // Window name

									 WS_OVERLAPPEDWINDOW | WS_VISIBLE,			 // Window style

									 0, 0,										 // Top-left corner of window

									 width, height,								 // Window width and height

									 NULL,										 // Handle to parent

									 NULL,										 // Handle to menu

									 hInstance,									 // Handle to instance

									 NULL)))									 // Advanced window creation data

		{
			return 0;
		}

		/////////////////////////////////////////////////////////////////////////

		// Force Windows to update the window

		/////////////////////////////////////////////////////////////////////////

		UpdateWindow(m_hWnd);


		return 1;
	}


	/////////////////////////////////////////////////////////////////////////

	// Name: DeleteWnd()

	//

	// Input: None

	//

	// Output: None

	//

	// Desc: The function used to delete the window. Takes no parameters,

	//       and simply deletes the window.

	/////////////////////////////////////////////////////////////////////////

	void CWindow::DeleteWnd()
	{

	}


	/////////////////////////////////////////////////////////////////////////

	// Name: MessageHandler()

	//

	// Input: Message structure, message parameter, message parameter

	//

	// Output: None

	//

	// Desc: A function for handling messages. Not the default window

	//       procedure.

	/////////////////////////////////////////////////////////////////////////

	int CWindow::MessageHandler(HWND hWnd, UINT Msg, LPARAM lParam, WPARAM wParam)
	{
		/////////////////////////////////////////////////////////////////////////

		// Switch on the message. If we come across a message that is useful to

		// us, we tell the program to put in an ''On...'' function.

		/////////////////////////////////////////////////////////////////////////

		switch(Msg)
		{
		case WM_CREATE:    { this->OnWindowCreate(); }
		case WM_PAINT:	   { this->OnWindowPaint(); }
		case WM_DESTROY:   { this->OnWindowShutdown(); }
		}
	
		return DefWindowProc(hWnd, Msg, lParam, wParam);
	}


	/////////////////////////////////////////////////////////////////////////

	// Name: OnWindowCreate(), OnWindowMain(), OnWindowShutdown(),

	//		 OnWindowPaint()

	//

	// Input: None

	// 

	// Output: None

	//

	// Desc: Virtual functions that take care of basic application

	//       house keeping; the user can overload to make his or her own 

	//       responses to the events.

	/////////////////////////////////////////////////////////////////////////

	// On window creation

	void CWindow::OnWindowCreate() 
	{

	}

	// On window main loop

	void CWindow::OnWindowMain()
	{

	}

	// On window shutdown

	void CWindow::OnWindowShutdown()
	{

	}

	// On window paint

	void CWindow::OnWindowPaint()
	{

	}


	/////////////////////////////////////////////////////////////////////////

	// Name: OnMouseDown(), OnMouseUp(), OnMousePress()

	// 

	// Input: None

	//

	// Output: None

	//

	// Desc: Virtual functions taking care of the mouse input that the can 

	//       overload to make his or her own responses to the events.

	/////////////////////////////////////////////////////////////////////////

	// On mouse down

	void CWindow::OnMouseDown()
	{

	}

	// On mouse up

	void CWindow::OnMouseUp()
	{

	}

	// On mouse press

	void CWindow::OnMousePress()
	{

	}


///////////////////

//test.cpp


#include "CWindow.h"

CWindow mywindow;


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	mywindow.CreateWnd(640, 480, hInstance, hPrevInstance, nShowCmd);

	MessageBox(NULL, "Hello world.", "", MB_OK);

	mywindow.DeleteWnd();

	return 0;
}
  
Sorry for the length of the source. Thanks!
When you go homeTell them of us, and say:For your tomorrow,We gave our today.
Evil Bill
Evil Bill
pWnd is not initialized. I assume you want to do that either in the constuctor, in the CWindow::CreateWnd() function, or in WinMain().

HTH, Steve


Steve
DirectX Programmer
Soon to be the new Bill Gates
Member of the Unban Mindwipe Society (UMWS)
Brobanx
Brobanx
In your switch statement, you forgot to use ''break'', that''s the only mistake that popped out to me, though there could be more.
elendil67
elendil67
Thanks, you guys. Evil Bill''s worked for me. And thanks, brobanx, you prevented a possible future bug.
When you go homeTell them of us, and say:For your tomorrow,We gave our today.
WayfarerX
WayfarerX
Aren''t you supposed to make the dtor of a class virtual if it has any virtual functions?
Also: I would suggest using the observer pattern to handle your window and mouse events. That way you don''t have to subclass CWindow every time you want to handle a new event.
"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.