WindowProcedure in a class

Started by
11 comments, last by _the_phantom_ 18 years, 10 months ago
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?
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.
Ok so it would be better to keep the Window Procedure outside of the class?
You have to make your WndProc static

I answered a similar question here
- Iliak -
[ ArcEngine: An open source .Net gaming framework ]
[ Dungeon Eye: An open source remake of Eye of the Beholder II ]
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
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 :)
- Iliak -
[ ArcEngine: An open source .Net gaming framework ]
[ Dungeon Eye: An open source remake of Eye of the Beholder II ]
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
Quote:Original post by Vampyre_Dark
There is an article about this here in the articles section.


This article here infact which was the basis for my own window wrapping code.

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
you need to define the variable WindowHandle in a translation unit so that the linker can find it.

This topic is closed to new replies.

Advertisement