Jump to content
  • Advertisement
Sign in to follow this  
mengha

WindowProcedure in a class

This topic is 4820 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!