class CWin
{
public:
CWin( HINSTANCE app_inst );
virtual ~CWin();
bool MakeWindow( std::string title, RECT Res );
bool ProcessMessages( MSG &msg );
static LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param );
private:
WNDCLASSEX ClassInfo;
HWND hwnd;
};
CWin::CWin( HINSTANCE app_inst )
{
WNDCLASSEX WndClass =
{
sizeof( WNDCLASSEX ), // cbSize
CS_HREDRAW | CS_VREDRAW, // style
WndProc, // lpfnWndProc
0, 0, // cbClsExtra, cbWndExtra
app_inst, // hInstance
LoadIcon( NULL, IDI_APPLICATION ), // hIcon
LoadCursor( NULL, IDC_ARROW ), // hCursor
(HBRUSH)GetStockObject( WHITE_BRUSH ), // hbrBackground
NULL, // lpszMenuName
"lpszClassName", // lpszClassName
LoadIcon( NULL, IDI_WINLOGO ) // hIconSm
};
ClassInfo = WndClass;
}
CWin::~CWin()
{
DestroyWindow( hwnd );
}
bool CWin::MakeWindow( std::string title, RECT Res )
{
if( 0 == RegisterClassEx( &ClassInfo ) )
return false;
hwnd = CreateWindowEx(
NULL, // dwExStyle
ClassInfo.lpszClassName, // lpClassName
title.c_str(), // lpWindowName
WS_OVERLAPPEDWINDOW | WS_VISIBLE, // dwStyle
Res.left, Res.top, // x, y
Res.right, Res.bottom, // nWidth, nHeight
NULL, // hWndParent
NULL, // hMenu
ClassInfo.hInstance, // hInstance
NULL ); // lpParam
if( NULL == hwnd )
return false;
return true;
}
LRESULT CALLBACK CWin::WndProc( HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param )
{
switch( msg )
{
case WM_CLOSE: // Windows is closing
DestroyWindow( hwnd );
PostQuitMessage( 0 );
return 0;
break;
default:
break;
}
return DefWindowProc( hwnd, msg, w_param, l_param );
}
bool CWin::ProcessMessages( MSG &msg )
{
if( PeekMessage( &msg, hwnd, NULL, NULL, PM_REMOVE ) )
{
if( msg.message == WM_QUIT )
{
return false;
}
else
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
return true;
}
int APIENTRY WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow )
{
CWin MyWin( hInstance );
RECT rect = { 0, 0, 800, 600 };
if( false == MyWin.MakeWindow( "My Window", rect ) )
{
MessageBox( NULL, "MakeWindow", "ERROR", MB_OK );
return 0;
}
MSG msg;
while( MyWin.ProcessMessages( msg ) )
{
// Do Stuff
}
return msg.wParam;
}
MyWin.ProcessMessages( msg, UpdateScene )
void UpdateScene()
{
// Do Stuff
}
[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]Ripped off from various people