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

Classes and windows

Started by shakazed Jul 21, 2002 at 4:14 PM 3 replies 600+ views
Original Post
shakazed
shakazed
I´m new to using classes so do´n´t spank me to hard when I ask this. I want to make this work so I can simplefy a window initialization. This is what my class looks like.
////////////////////////////////////////////////////////////////////////////////////////////////
//Windows initialization 
//////////////////////////////////////////////////////////////////////////////////////////////////

#include //Necessary windows funcs. and macros.

class Win
{
public:
	void WinInit(HINSTANCE hInstance);
	bool CreateWin(char *WinName);
	LRESULT WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

private:
	WNDCLASSEX m_wClass;
	HWND hWnd;

};

void Win::WinInit(HINSTANCE hInstance) 
{	
	m_wClass.cbSize = sizeof(WNDCLASSEX);
	m_wClass.cbClsExtra = 0;
	m_wClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	m_wClass.cbWndExtra = 0;
	m_wClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	m_wClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	m_wClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
	m_wClass.lpfnWndProc = WndProc;
	m_wClass.lpszClassName = "Window Class";
	m_wClass.lpszMenuName = NULL;
	m_wClass.style = CS_HREDRAW|CS_VREDRAW;
	m_wClass.hInstance = hInstance;
}

bool CreateWin(char *WinName);
{
	hWnd = CreateWindowEx(NULL,
		"Window Class", *WinName,
		WS_OVERLAPPEDWINDOW|WS_VISIBLE,100,100,
		640,480,NULL, NULL,hInstance, NULL);

	if(hWnd==NULL)
		return false;

	return true;
}

LRESULT Win::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_DESTROY:
		{
			PostQuitMessage(0);
			return 0;
		}break;

	}

	return DefWindowProc(hWnd, msg, wParam, lParam);
}
 
Now, I get an conversion error in the "m_wClass.lpfnWndProc = WndProc;" part. Any ideas what´s wrong? sHaKaZeD

http://www26.brinkster.com/shakazed/
Evil Bill
Evil Bill

  
class Win
{
public:
...
static LRESULT WndProcStub(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)

...

protected:
LRESULT WndProcStub(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
};

LRESULT Win::WndProcStub(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static Win* pWin = NULL;

if((!pWin) && (hWnd == NULL))
{
pWin = (Win*)lParam;
return 0;
}
return pWin->WndProc(hWnd,msg,wParam,lParam);
}

LRESULT Win::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
// The ''normal'' code //

}

Win::WinInit(HINSTANCE hInstance)
{
WndProcStub(NULL,0,0,(LPARAM)this); // Setup the pWin pointer

m_wClass.cbSize = sizeof(WNDCLASSEX);
m_wClass.cbClsExtra = 0;
m_wClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
m_wClass.cbWndExtra = 0;
m_wClass.hCursor = LoadCursor(NULL, IDC_ARROW);
m_wClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
m_wClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
m_wClass.lpfnWndProc = WndProcStub;
m_wClass.lpszClassName = "Window Class";
m_wClass.lpszMenuName = NULL;
m_wClass.style = CS_HREDRAW|CS_VREDRAW;
m_wClass.hInstance = hInstance;
}


You can''t have a member function as a function pointer there (see the links niyaw gave). What this does is just passes things through the static helper function.

HTH, Steve

Steve
DirectX Programmer
Soon to be the new Bill Gates
Member of the Unban Mindwipe Society (UMWS)
niyaw
niyaw
Evil Bill: are you sure that code works? from here, it looks like pWin will never be initialized.

---
Come to #directxdev IRC channel on AfterNET

Topic Locked

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

Sign in to reply to this topic.