Hi everyone
I am having a problem with my win32 wrapper class in c++
here is my code
Display.h
#pragma once
#include <Windows.h>
class Display
{
private:
char* mWindowTitle;
WNDCLASSEX mWndClass;
HWND mHwnd;
MSG mMsg;
int mWidth;
int mHeight;
public:
Display(int width, int height, char* windowtitle, WNDPROC messagefunc, HINSTANCE hinst);
~Display(void);
bool InitializeWindow();
HWND GetWindowHandle();
MSG GetMessageHandler();
};
Display.cpp
#include "display.h"
Display::Display(int width, int height,char* windowtitle, WNDPROC messagefunc, HINSTANCE hinst)
{
mWindowTitle = windowtitle;
mWidth = width;
mHeight = height;
mHwnd = 0;
mWndClass.cbSize = sizeof(WNDCLASSEX); mWndClass.style = 0; mWndClass.lpfnWndProc = messagefunc; mWndClass.cbClsExtra = 0;
mWndClass.cbWndExtra = 0;
mWndClass.hIcon = LoadIcon(hinst, IDI_APPLICATION);
mWndClass.hCursor = LoadCursor(hinst,IDC_ARROW);
mWndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
mWndClass.lpszClassName = "EngineGameWindow";
mWndClass.lpszMenuName = NULL;
mWndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
mWndClass.hInstance = hinst;
}
Display::~Display(void)
{
}
bool Display::InitializeWindow()
{
if(!RegisterClassEx(&mWndClass))
MessageBox(NULL,"Sorry could not register window class",
"Window Registration Error", MB_ICONEXCLAMATION | MB_OK);
mHwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"EngineGameWindow", mWindowTitle,
WS_OVERLAPPEDWINDOW,GetSystemMetrics(SM_CXSCREEN)/4,
GetSystemMetrics(SM_CYSCREEN)/8,
mWidth, mHeight,NULL,NULL,mWndClass.hInstance,NULL);
if(mHwnd == NULL)
MessageBox(mHwnd,"Window not created","Window Creation Error",MB_ICONEXCLAMATION | MB_OK);
return true;
}
HWND Display::GetWindowHandle()
{
return mHwnd;
}
MSG Display::GetMessageHandler()
{
return mMsg;
}
main.cpp
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
if(msg == WM_CHAR && wparam == VK_ESCAPE)
{
PostQuitMessage(0);
return 0;
}
switch(msg)
{
case WM_CLOSE: DestroyWindow(hwnd);
break;
case WM_DESTROY: PostQuitMessage(0);
break;
case WM_PAINT:
ValidateRect(hwnd, 0);
break;
default:
return DefWindowProc(hwnd,msg,wparam,lparam);
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hPrevinstance, LPSTR lpcmdline, int ncmdshow)
{
Display display = Display(800, 600, "Engine",WndProc,hinst);
if(!display.InitializeWindow())
return 0;
Directx9Api api = Directx9Api();
if(!api.InitializeApi())
return 0;
ShowWindow(display.GetWindowHandle(), SW_SHOW);
UpdateWindow(display.GetWindowHandle());
while (GetMessage(&display.GetMessageHandler(),display.GetWindowHandle(),0,0) > 0)
{
TranslateMessage(&(display.GetMessageHandler()));
DispatchMessage(&(display.GetMessageHandler()));
}
return 0;
}
When i create a window from the class i cant interact with it that is, i cant close, minimize or maximize it. But when i create the window without it in a class, that is when i do everything in the main function like this
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprevinst,LPSTR lpcmdline, int ncmdshow)
{
WNDCLASSEX wndclassex;
HWND hwnd;
MSG msg;
wndclassex.cbSize = sizeof(WNDCLASSEX); wndclassex.style = CS_DROPSHADOW; wndclassex.lpfnWndProc = WndProc; wndclassex.cbClsExtra = 0;
wndclassex.cbWndExtra = 0;
wndclassex.hIcon = LoadIcon(hinst, IDI_APPLICATION);
wndclassex.hCursor = LoadCursor(hinst,IDC_ARROW);
wndclassex.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wndclassex.lpszClassName = wndclassname;
wndclassex.lpszMenuName = NULL;
wndclassex.hIconSm = LoadIcon(hinst, IDI_APPLICATION);
wndclassex.hInstance = hinst;
if(!RegisterClassEx(&wndclassex))
{
MessageBox(NULL, "Sorry could not register window class",
"Window Registration Error", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,wndclassname,"A simple window",
WS_OVERLAPPEDWINDOW,((GetSystemMetrics(SM_CXSCREEN)/2)/2), ((GetSystemMetrics(SM_CYSCREEN)/2)/2), 300, 300,NULL,NULL,hinst,NULL);
if(hwnd == NULL)
{
MessageBox(hwnd,"Sorry could not create a window","Window Creation Error",MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, ncmdshow);
UpdateWindow(hwnd);
while (GetMessage(&msg,hwnd,0,0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
it works perfectly.Can anyone help me
Thanks in advance.