Erik Wiggins
March 21st, 2004, 09:03 AM
I'm still new to programming with c++ and I'm still working on my own style for programming. I've found a technique that works real well for me however I'm not sure it's a good practice as I've never seen anyone use anything like it in this forum or in any of the articals/tutorials I've read. Before I start using it alot I figure I better run it by you guys first. This is what I've done.
3 classses
class wnd
{
public:
BOOL Show(int nCmdShow);
void Create();
CREATESTRUCT cs;
wnd();
wnd(HINSTANCE hinstance);
virtual ~wnd();
HWND hWnd;
};
class cls
{
public:
ATOM Register();
WNDCLASSEX wcx;
cls();
virtual ~cls();
}
class wndcls :
public wnd,
public cls
{
public:
wndcls();
virtual ~wndcls();
};
Then in order to create a window I derive a class from one of those three classes and put the WNDPROC in the cpp file of the derived class.
//header
class wndclsFrame : public wndcls
{
public:
wndclsFrame(HINSTANCE hInst);
virtual ~wndclsFrame();
};
//cpp
#include "wndclsFrame.h"
#include "resource.h"
LRESULT CALLBACK MainWndProc(HWND,UINT,WPARAM,LPARAM);
wndclsFrame::wndclsFrame(HINSTANCE hInst)
{
wcx.cbSize = sizeof(wcx);
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_ICON1));
wcx.hCursor = LoadCursor(NULL,IDC_ARROW);
wcx.hbrBackground = GetStockObject(WHITE_BRUSH);
wcx.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wcx.hIconSm = NULL;
wcx.lpfnWndProc = MainWndProc;
wcx.lpszClassName = "FrameWndClass";
wcx.hInstance = hInst;
Register();
cs.lpCreateParams = NULL;
cs.hMenu = (HMENU) NULL;
cs.hwndParent = (HWND) NULL;
cs.cy = CW_USEDEFAULT;
cs.cx = CW_USEDEFAULT;
cs.y = CW_USEDEFAULT;
cs.x = CW_USEDEFAULT;
cs.style = WS_OVERLAPPEDWINDOW;
cs.dwExStyle = WS_EX_APPWINDOW;
cs.hInstance = hInst;
cs.lpszName = "Sample";
cs.lpszClass = "FrameWndClass";
Create();
Show(SW_SHOWNORMAL);
}
wndclsFrame::~wndclsFrame()
{
}
LRESULT CALLBACK MainWndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
break;
case WM_PAINT:
// Paint the window's client area.
break;
case WM_SIZE:
// Set the size and position of the window.
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
That way I can keep my WinMain really breif.
#include <windows.h>
#include "resource.h"
#include "wndclsFrame.h"
MSG msg;
// Application entry point.
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
wndclsFrame Frame(hinstance);
while(GetMessage(&msg, NULL, 0, 0))
{
DispatchMessage(&msg);
}
return msg.wParam;
UNREFERENCED_PARAMETER(lpCmdLine);
}
It also make it real simple for me to keep trake of my windows as all the information for the window is in it's own nice little file. Instead of having a bunch of Init functions in my App.cpp file containing WinMain.
3 classses
class wnd
{
public:
BOOL Show(int nCmdShow);
void Create();
CREATESTRUCT cs;
wnd();
wnd(HINSTANCE hinstance);
virtual ~wnd();
HWND hWnd;
};
class cls
{
public:
ATOM Register();
WNDCLASSEX wcx;
cls();
virtual ~cls();
}
class wndcls :
public wnd,
public cls
{
public:
wndcls();
virtual ~wndcls();
};
Then in order to create a window I derive a class from one of those three classes and put the WNDPROC in the cpp file of the derived class.
//header
class wndclsFrame : public wndcls
{
public:
wndclsFrame(HINSTANCE hInst);
virtual ~wndclsFrame();
};
//cpp
#include "wndclsFrame.h"
#include "resource.h"
LRESULT CALLBACK MainWndProc(HWND,UINT,WPARAM,LPARAM);
wndclsFrame::wndclsFrame(HINSTANCE hInst)
{
wcx.cbSize = sizeof(wcx);
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_ICON1));
wcx.hCursor = LoadCursor(NULL,IDC_ARROW);
wcx.hbrBackground = GetStockObject(WHITE_BRUSH);
wcx.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wcx.hIconSm = NULL;
wcx.lpfnWndProc = MainWndProc;
wcx.lpszClassName = "FrameWndClass";
wcx.hInstance = hInst;
Register();
cs.lpCreateParams = NULL;
cs.hMenu = (HMENU) NULL;
cs.hwndParent = (HWND) NULL;
cs.cy = CW_USEDEFAULT;
cs.cx = CW_USEDEFAULT;
cs.y = CW_USEDEFAULT;
cs.x = CW_USEDEFAULT;
cs.style = WS_OVERLAPPEDWINDOW;
cs.dwExStyle = WS_EX_APPWINDOW;
cs.hInstance = hInst;
cs.lpszName = "Sample";
cs.lpszClass = "FrameWndClass";
Create();
Show(SW_SHOWNORMAL);
}
wndclsFrame::~wndclsFrame()
{
}
LRESULT CALLBACK MainWndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
break;
case WM_PAINT:
// Paint the window's client area.
break;
case WM_SIZE:
// Set the size and position of the window.
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
That way I can keep my WinMain really breif.
#include <windows.h>
#include "resource.h"
#include "wndclsFrame.h"
MSG msg;
// Application entry point.
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
wndclsFrame Frame(hinstance);
while(GetMessage(&msg, NULL, 0, 0))
{
DispatchMessage(&msg);
}
return msg.wParam;
UNREFERENCED_PARAMETER(lpCmdLine);
}
It also make it real simple for me to keep trake of my windows as all the information for the window is in it's own nice little file. Instead of having a bunch of Init functions in my App.cpp file containing WinMain.