CBaseWin.cpp

 /*

SimpleFC

These classes are intended to be a starting point for anyone to write their
own foundation 'type' classes or widgets. Just remember that any additional
class should directly or indirectly inherit from CBaseWin.

This is a powerful starting point to writing windows apps without MFC.

Written by Mike C. Nov 30 2005.
governortyrran@hotmail.com

*/

#if _MSC_VER > 1000
#pragma once
#endif

LRESULT CALLBACK CBaseWinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
//check to see if the id is that of our base class
//if not we dont want to call a function of the
//extra byte, because it wont be accurately set
if(GetWindowLong(hWnd, GWL_USERDATA) == __CBASEWIN__ID)
{
//retrieve a pointer to the base class
CBaseWin *pCBaseWin = NULL;
LONG l = GetWindowLong(hWnd, 0);
pCBaseWin = reinterpret_cast<CBaseWin*>((LONG_PTR)l);
if(pCBaseWin == NULL) //one last safety check
return (DefWindowProc(hWnd, uMsg, wParam, lParam));
//redirect to class proc
return pCBaseWin->WinProc(hWnd, uMsg, wParam, lParam);
}
return (DefWindowProc(hWnd, uMsg, wParam, lParam));
}

CBaseWin::CBaseWin(VOID) : m_hWnd(NULL)
{
//register the class
SetLastError(NO_ERROR);
if(!(RegisterWinExBytes((HINSTANCE)GetModuleHandle(NULL),
CBaseWinProc, __CBASEWIN__CLASSNAME, __CBASEWIN__EXTRABYTES)))
{
if((INT)(DWORD)GetLastError() != 1410)  //error code for already exists
{
MessageBox(
0,
_T("Unable to register class.\nIt is recommended to quit this app."),
_T("Unknown Error:"),
MB_OK | MB_ICONEXCLAMATION);
}
}
else
{ //for some reason we get an error on success..
SetLastError(NO_ERROR);
}
}

CBaseWin::~CBaseWin(VOID)
{
Release();
}

VOID CBaseWin::Release(VOID)
{
SetWindowLong(m_hWnd, GWL_USERDATA, 0L);
UnregisterClass(__CBASEWIN__CLASSNAME, (HINSTANCE)GetModuleHandle(NULL));
if(m_hWnd != NULL)
{
DestroyWindow(m_hWnd);
m_hWnd = NULL;
}
}

BOOL CBaseWin::Create(LPCTSTR szWinName, DWORD dwStyles, HWND hWndParent,
INT x, INT y, INT cx, INT cy)
{
if(szWinName != NULL)
{
if (!(m_hWnd = CreateWindowEx(0,
__CBASEWIN__CLASSNAME, szWinName,
dwStyles, x, y, cx, cy,
hWndParent, NULL, (HINSTANCE)GetModuleHandle(NULL), NULL)))
{
return FALSE;
}
}
else
{
if (!(m_hWnd = CreateWindowEx(0,
__CBASEWIN__CLASSNAME, __CBASEWIN__DEFNAME,
dwStyles, x, y, cx, cy,
hWndParent, NULL, (HINSTANCE)GetModuleHandle(NULL), NULL)))
{
return FALSE;
}
}

//set the id of the class
SetWindowLong(m_hWnd, GWL_USERDATA, __CBASEWIN__ID);
//set the extra bytes of the window
//here we set it to a safely cast ptr to this
SetWindowLong(m_hWnd, 0, (LONG)reinterpret_cast<LONG_PTR>(this));

//re-send intialisation messages that were missed
SendMessage(m_hWnd, WM_NCCREATE, 0 , 0);
SendMessage(m_hWnd, WM_CREATE, 0 , 0);
SendMessage(m_hWnd, WM_NCCALCSIZE, 0 , 0);

ShowWindow(m_hWnd, SW_SHOW);
UpdateWindow(m_hWnd);

return TRUE;
}

BOOL CBaseWin::Resize(INT cx, INT cy)
{
return SetWindowPos(m_hWnd, NULL, 0, 0, cx, cy, SWP_NOZORDER | SWP_NOMOVE);
}

LRESULT CBaseWin::WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if(hWnd != this->m_hWnd) return -1;
switch(uMsg)
{
case WM_CREATE:
{
//MessageBox(0, _T("Received create message."), _T("Status:"), 0);
break;
}
case WM_LBUTTONDOWN:
{
//MessageBox(0, _T("Received WM_LBUTTONDOWN message."), _T("Status:"), 0);
break;
}
case WM_DESTROY:
{
DestroyWindow(hWnd);
break;
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

ATOM CBaseWin::RegisterWinExBytes(HINSTANCE hInstance, WNDPROC WndProc,
TCHAR *szWinClassName, INT nExtraBytes)
{
WNDCLASSEX winclass;

winclass.cbSize         = sizeof(WNDCLASSEX);
winclass.style      = CS_OWNDC | CS_DBLCLKS | CS_BYTEALIGNWINDOW;
winclass.lpfnWndProc   = (WNDPROC) WndProc;
winclass.cbClsExtra   = 0;
winclass.cbWndExtra   = nExtraBytes;
winclass.hInstance   = hInstance;
winclass.hIcon      = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor     = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
winclass.lpszMenuName   = NULL;
winclass.lpszClassName = szWinClassName;
winclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);

return RegisterClassEx(&winclass);

}

Project Homepage: