/**********************************************************************
- File Name: VsWindow.cpp
- Date: 12/7/2003 12:31:17 AM
- Author: Michael Sheinin
- Description :
- Notes:
**********************************************************************/
#include "StdAfx.h"
#include "vswindow.h"
//specifies whether the window class was registered
BOOL CVsWindow::m_bClassRegistered = FALSE;
TMsgEntry CVsWindow::m_pMsgEntries[] =
{
{WM_CREATE,OnCreatePriv},
{WM_NCDESTROY,OnNcDestroyPriv},
{0xFFFFF,NULL},
};
TMsgMap CVsWindow::m_MsgMap = { NULL, CVsWindow::m_pMsgEntries };
TMsgMap* CVsWindow::GetMessageMap()
{
return &(CVsWindow::m_MsgMap);
}
CVsWindow::CVsWindow(void)
{
m_hWnd = NULL;
}
CVsWindow::~CVsWindow(void)
{
if ( m_hWnd != NULL )
{
::DestroyWindow(m_hWnd);
}
}
LONG CVsWindow::OnCreatePriv( WPARAM , LPARAM pCS )
{
if ( OnCreate((LPCREATESTRUCT )pCS) )
{
return 0;
}
return -1;
}
LONG CVsWindow::OnNcDestroyPriv( WPARAM, LPARAM )
{
OnNcDestroy();
m_hWnd = NULL;
return 0;
}
void CVsWindow::OnPaintPriv()
{
PAINTSTRUCT ps;
HDC hDC = ::BeginPaint(m_hWnd,&ps);
OnPaint(hDC);
::EndPaint(m_hWnd,&ps);
}
#pragma warning(disable:4312)
#pragma warning(disable:4244)
LRESULT CALLBACK CVsWindow::_MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
CVsWindow* pThis = (CVsWindow*)::GetWindowLongPtrW(hWnd,GWLP_USERDATA);
if ( pThis != NULL )
{
for( TMsgMap* pMsgMap = pThis->GetMessageMap(); pMsgMap != NULL;
pMsgMap = pMsgMap->m_pBaseMap )
{
for( TMsgEntry* pMsgEntry = pMsgMap->m_pMsgEntries; pMsgEntry->m_pFn != NULL;
pMsgEntry ++ )
{
if ( pMsgEntry->m_uMsg == uMsg )
{
return (pThis->*pMsgEntry->m_pFn)(wParam,lParam);
}
}
}
}
else if ( uMsg == WM_CREATE )
{
if ( lParam != 0 )
{
pThis = (CVsWindow*)(((LPCREATESTRUCT)lParam)->lpCreateParams);
pThis->m_hWnd = hWnd;
::SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pThis );
return _MainWndProc(hWnd,WM_CREATE,wParam,lParam);
}
return -1;
}
if ( WM_PAINT == uMsg )
{
pThis = (CVsWindow*)::GetWindowLongPtrW(hWnd,GWLP_USERDATA);
if ( pThis )
{
pThis->OnPaintPriv();
}
return 0;
}
return ::DefWindowProc(hWnd,uMsg,wParam, lParam);
}
#pragma warning(default:4312)
#pragma warning(default:4244)
void CVsWindow::Close()
{
if ( m_hWnd )
{
::DestroyWindow(m_hWnd );
}
}
BOOL CVsWindow::Create(HWND hParent, int iX, int iY, int iCX, int iCY)
{
if ( !m_bClassRegistered )
{
WNDCLASSEX wcx;
wcx.cbSize = sizeof(wcx); // size of structure
wcx.style = CS_HREDRAW | CS_VREDRAW; // redraw if size changes
wcx.lpfnWndProc = (WNDPROC)_MainWndProc; // points to window procedure
wcx.cbClsExtra = 0; // no extra class memory
wcx.cbWndExtra = 0; // no extra window memory
wcx.hInstance = NULL; // handle to instance
wcx.hIcon = ::LoadIcon(NULL, IDI_APPLICATION); // predefined app. icon
wcx.hCursor = LoadCursor(NULL,IDC_ARROW); // predefined arrow
wcx.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH); // white background brush
wcx.lpszMenuName = NULL; // name of menu resource
wcx.lpszClassName = "MichaelVsWindow"; // name of window class
wcx.hIconSm = NULL;
m_bClassRegistered = RegisterClassEx(&wcx);
}
if ( m_bClassRegistered )
{
HWND hwnd = CreateWindowEx( 0,
"MichaelVsWindow",
"Sample", // title-bar string
(hParent==NULL?WS_OVERLAPPEDWINDOW:WS_CLIPSIBLINGS|WS_CHILD|WS_VISIBLE),
iX, // default horizontal position
iY, // default vertical position
iCX, // default width
iCY, // default height
(HWND) hParent, // no owner window
(HMENU) NULL, // use class menu
NULL, // handle to application instance
this); // no window-creation data
if ( hwnd )
{
return TRUE;
}
}
return FALSE;
}