Sign in to follow this  
mengha

WindowProcedure in a class

Recommended Posts

mengha    130
Im trying to make a class for myself that will create a window. So far I have: (Header)
#include <windows.h>

class Win32Window 
{
public:
	void CreateWin32Window(HINSTANCE hInstance, int iWidth, int iHeight, int iShowCmd);
	LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

	HWND WindowHandle;
};

(CPP)
#include "win32_window.h"

void Win32Window::CreateWin32Window(HINSTANCE hInstance, int iWidth, int iHeight, int iShowCmd)
{
	WNDCLASS wcMain;

	wcMain.style = CS_HREDRAW | CS_VREDRAW;
	wcMain.cbClsExtra = 0;
	wcMain.cbWndExtra = 0;
	wcMain.lpszMenuName = 0;
	wcMain.hCursor = ::LoadCursor(0, IDC_ARROW);
	wcMain.hIcon = ::LoadIcon(0, IDI_APPLICATION);
	wcMain.hbrBackground = (HBRUSH) ::GetStockObject(WHITE_BRUSH);
	wcMain.hInstance = hInstance;
	wcMain.lpfnWndProc = WndProc;
	wcMain.lpszClassName = "My Window";
}

LRESULT CALLBACK Win32Window::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_LBUTTONDOWN:
		::MessageBox(0, "Left Mouse", "", 0);
		return 0;
	case WM_KEYDOWN:
		if(wParam == VK_ESCAPE)
		{
			::DestroyWindow(WindowHandle);
		}
		return 0;
	case WM_DESTROY:
		::PostQuitMessage(0);
		return 0;
	}

	return ::DefWindowProc(hWnd, msg, wParam, lParam);
}

Its not finished yet but when trying to compile this code I get the error: error C2440: '=' : cannot convert from 'LRESULT (__stdcall Win32Window::* )(HWND,UINT,WPARAM,LPARAM)' to 'WNDPROC' How can I pass the window procedure of the class to the WNDCLASS strcuture?

Share this post


Link to post
Share on other sites
Daaark    3553
There is an article about this here in the articles section. What I do for mine, is just pass the WNDPROC to my function, and paste my basic WndProc function into my new code. Then I can customize it easy for any app.

Share this post


Link to post
Share on other sites
BrianTheLlama    154
To incorporate a window procedure into class you need to declare the procedure as a static function. Unfortunately this introduces a number of limitations (static methods can only access static member variables) but this shouldn't be a problem in a game window since most (but not all) games use only a single window.

Adam

Share this post


Link to post
Share on other sites
iliak    278
With my method a single WndProc() can manage an infinite number of windows. When You create a window, you pass the 'this' as an user parameter and each window store this 'this'. And the job is done :)

Share this post


Link to post
Share on other sites
BrianTheLlama    154
Very cunning, it never occured to me to do it that way! :)

By passing the 'this' pointer, does the window procedure have the required privilege level to access private, as well as public members?

Adam

Share this post


Link to post
Share on other sites
mengha    130
I made WndProc static and it complained it couldnt access WindowHandle. So I made this static and private withn the class but now I get a link error:

main error LNK2001: unresolved external symbol "private: static struct HWND__ * Win32Window::WindowHandle" (?WindowHandle@Win32Window@@0PAUHWND__@@A)

1 Unresolved Externals

Share this post


Link to post
Share on other sites
iliak    278
In your .cpp

LRESULT CALLBACK cRender::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
// get back our this
cRender* pThis = (cRender *)GetWindowLong(hWnd, GWL_USERDATA);



// dispatch messages
switch (uMsg)
{

// Creation
case WM_CREATE:
{
// get our 'this'
pThis = (cRender *)((LPCREATESTRUCT)lParam)-&gt;lpCreateParams;

// store it
SetWindowLong(hWnd, GWL_USERDATA, (LONG )pThis);
}
break;

}
...
}




And you have full access to all datas in your class

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this