Code:
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
#include <windows.h>
#include <tchar.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
class window
{
public:
//Global windows based declarations
HWND hWnd;
WNDCLASS wc;
MSG msg;
//Global class-needed declarations
int windowWidth;
int windowHeight;
bool bWindowed;
window()
{
//Class constructor with default values
windowWidth = 640;
windowHeight = 480;
bWindowed = true;
hWnd = 0;
}
HWND windowCreator(LPCSTR className, LPCSTR windowTitle, HINSTANCE hInst, WNDPROC WndProc, DWORD style=WS_OVERLAPPEDWINDOW)
{
memset(&wc, 0, sizeof(WNDCLASS));
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = ::LoadIcon(0, IDI_APPLICATION);
wc.hCursor = ::LoadCursor(0, IDC_ARROW);
wc.hbrBackground= static_cast<HBRUSH>(::GetStockObject(WHITE_BRUSH));
wc.lpszMenuName = NULL;
wc.lpszClassName= TEXT(className);
if(!RegisterClass(&wc))
{
MessageBox(0, TEXT("Failed to register windows class."), TEXT("Windows Error"), 0);
return 0;
}
hWnd = CreateWindow(
className,
windowTitle,
style,
CW_USEDEFAULT,
CW_USEDEFAULT,
windowWidth,
windowHeight,
0,
0,
hInst,
0);
if( hWnd == 0 )
{
MessageBox(0, TEXT("Create Window - Failed"), TEXT("Windows Error"), 0);
return 0;
}
else
{
return hWnd;
}
}
};
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine,int nCmdShow)
{
MSG Msg;
HWND hwnd;
TCHAR chClassName[]=TEXT("Testing");
window win1;
hwnd = win1.windowCreator(chClassName, "Mywindow", hInstance, WndProc );
if(hwnd==NULL)
{
MessageBox(0,"Window Creation Failed!","Error",MB_ICONSTOP|MB_OK);
return 0;
}
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}