Hi all,
I am trying to write a simple program to do all the hard work when initialising a window. Basically I want to just type a few small lines of code to make a window appear. I have written a class to try and handle this but it always fails at the HWND creation. I don't understand why so any help would be very much appreciated.
Thanks
Dylan
firstly the class file
Code:
#include <windows.h>
#include "easywindow.h"
CEasyWindow::CEasyWindow(HINSTANCE hInstance)
: m_wc()
, m_hwnd()
, m_style(NULL)
, m_hIcon(NULL)
, m_hCursor(NULL)
, m_hbrBackground(NULL)
, m_lpszMenuName(NULL)
, m_lpszClassName(NULL)
, m_hIconSm(NULL)
, m_WindowName("WINDOW")
, m_hInstance(hInstance)
{
}
CEasyWindow::~CEasyWindow(void)
{
}
HWND CEasyWindow::Register(int nCmdShow)
{
memset(&m_wc, 0, sizeof(m_wc)); // clear everything just to be safe
m_wc.cbSize = sizeof(m_wc);
m_wc.style = m_style;
m_wc.lpfnWndProc = ProcessMessage;
m_wc.cbClsExtra = 0;
m_wc.cbWndExtra = 0;
if(m_hIcon != NULL)
m_wc.hIcon = LoadIcon(m_hInstance, m_hIcon);
if(m_hIconSm != NULL)
m_wc.hIconSm = LoadIcon(m_hInstance, m_hIconSm);
if(m_hCursor != NULL)
m_wc.hCursor = LoadCursor(NULL, m_hCursor);
m_wc.hbrBackground = m_hbrBackground;
m_wc.lpszMenuName = m_lpszMenuName;
m_wc.lpszClassName = m_lpszClassName;
RegisterClassEx(&m_wc);
m_hwnd = CreateWindowEx(0, m_WindowName, m_WindowName, WS_POPUP | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME, 50, 50, 200, 100, NULL, NULL, m_hInstance, NULL);
//@@@@@@@@@@@@@@@@@@@@@@ This code is always executed @@@@@@@@@@@
if(!m_hwnd)
{
::MessageBox(NULL, "CREATE WINDOW FAILED", m_WindowName, MB_ICONSTOP);
exit(-1);
}
ShowWindow(m_hwnd, nCmdShow);
return(m_hwnd);
}
LRESULT CALLBACK CEasyWindow::ProcessMessage(HWND m_hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
// Add any case situations you want here
case WM_PAINT: // paint message
PaintWindow(m_hwnd);
return(0);
case WM_CHAR: // button pressed
PostQuitMessage(0);
return(0);
}
return(DefWindowProc(m_hwnd, uMsg, wParam, lParam));
}
void CEasyWindow::PaintWindow(HWND m_hwnd)
{
PAINTSTRUCT ps;
BeginPaint(m_hwnd, &ps);
HDC hdc = ps.hdc;
char buf[] = "Press a key to exit.";
TextOut(hdc, 0, 0, buf, strlen(buf));
EndPaint(m_hwnd, &ps);
}
and now the actual winmain function
Code:
#include <windows.h>
#include "EasyWindow.h"
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// create a window
CEasyWindow MyWindow(hInstance);
MyWindow.m_WindowName = "First Window";
MyWindow.m_style = CS_HREDRAW | CS_VREDRAW;
MyWindow.Register(nCmdShow);
}
and just in case you want to try and run it yourself here is the header file
Code:
#pragma once
#include <windows.h>
class CEasyWindow
{
public:
CEasyWindow(HINSTANCE);
virtual ~CEasyWindow(void);
WNDCLASSEX m_wc;
HWND m_hwnd;
UINT m_style;
LPCTSTR m_hIcon;
LPCTSTR m_hCursor;
HBRUSH m_hbrBackground;
LPCTSTR m_lpszMenuName;
LPCTSTR m_lpszClassName;
LPCTSTR m_hIconSm;
LPCTSTR m_WindowName;
HINSTANCE& m_hInstance;
HWND Register(int);
static LRESULT CALLBACK ProcessMessage(HWND, UINT, WPARAM, LPARAM);
static void PaintWindow(HWND);
};