Skeleton Window: Message Handler in a Class?
Hello hello :)
I'm on my quest to incorporate classes and skeleton windows into one handy, uh, class :) I'm one error message away from a functional app :)
Here's the layout (my beautiful Main.cpp file):
Code:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "Game.h"
int APIENTRY WinMain(HINSTANCE MyInstance, HINSTANCE PrevInstance, LPSTR kposzArgs, int nWinMode)
{
MSG Msg;
Game MyGame(MyInstance);
while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
That is all the code in my Main.cpp file.
The code in my Game.h file (relevant portions) looks like:
Code:
#pragma once
class Game
{
public:
Game(void);
Game(HINSTANCE hInstance);
~Game(void);
LRESULT CALLBACK WinFunc(HWND hWnd, unsigned int Msg, WPARAM wParam, LPARAM lParam);
private:
HWND MainWindow;
HINSTANCE MainInstance;
};
The key thing to note here is that my LRESULT CALLBACK... function is a method in my Game Class.
Finally, Game.cpp looks like:
Code:
#include "Game.h"
#using <mscorlib.dll>
Game::Game(HINSTANCE hInstance)
{
MainInstance = hInstance; //I'm hoping I can do that
Fullscreen = GetScreenMode(); //Works fine
InitWindow(); //Current problem lies inside here (below)
}
void Game::InitWindow(void)
{
WNDCLASS WCL;
DWORD Style;
WCL.cbClsExtra = 0;
WCL.cbWndExtra = 0;
WCL.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
WCL.hCursor = LoadCursor(MainInstance, IDC_ARROW);
WCL.hIcon = LoadIcon(MainInstance, IDI_APPLICATION);
WCL.hInstance = MainInstance;
WCL.lpfnWndProc = WinFunc; //Have also tried Game::WinFunc
WCL.lpszClassName = "MyClass";
WCL.lpszMenuName = NULL;
WCL.style = CS_OWNDC;
if (!RegisterClass(&WCL)) exit(10);
if (Fullscreen)
{
Width = GetSystemMetrics(SM_CXSCREEN);
Height = GetSystemMetrics(SM_CYSCREEN);
Style = WS_POPUP;
}
else
{
Width = GetSystemMetrics(SM_CXSCREEN);
Height = GetSystemMetrics(SM_CYSCREEN);
Style = WS_OVERLAPPED|WS_SYSMENU;
}
MainWindow = CreateWindow("MyClass", "RPG", Style, 0, 0, Width, Height, NULL, NULL, MainInstance, NULL);
if (!MainWindow) exit(10);
ShowWindow(MainWindow, SW_SHOW);
UpdateWindow(MainWindow);
SetFocus(MainWindow);
}
LRESULT CALLBACK Game::WinFunc(HWND hWnd, unsigned int Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
return 0;
}
Those last two functions are usually what appear in the WinMain function, but I've moved them into the class. So, my problem comes from the line:
WCL.lpfnWndProc = WinFunc;
I get an error saying:
error C2440: '=' : cannot convert from 'LRESULT (__stdcall Game::* )(HWND,unsigned int,WPARAM,LPARAM)' to 'WNDPROC'
So then, my question is:
Is there no way I can pass a Message Handling Function that is within a Class to be the default handler?
Even if you don't know the answer, I really appreciate you atleast reading that all and getting down here to the bottom :) If any ideas or advice can be given (am I doing something else wrong?) on how (if at all possible) to use my "Game::WinFunc" function, I'm open to any insight.