Code:
//////////////////////////////////////////////////////////////////////
//
// Filename : Main.cpp
#include <windows.h>
HDC hDC = NULL;
HWND hWnd = NULL;
HINSTANCE hInstance = NULL;
LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
HWND main_CreateWindow(HINSTANCE hInst, WNDPROC* wndproc, HDC* hDevC,
char* ClassName, char* Title,int x, int y, int width, int height);
bool main_DestroyWindow(HWND* hWind,HDC* hDevC,HINSTANCE* hInst,char* ClassName);
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInstance,LPSTR lpCmpLine,int nShowCmd)
{
MSG message;
bool bProgramEnded = false;
hInstance = hInst;
hWnd = main_CreateWindow(hInst,(WNDPROC*)&WndProc,&hDC,"Window","Main Window",60,26,800,600);
while(!bProgramEnded)
{
if(PeekMessage(&message,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&message);
DispatchMessage(&message);
if(message.message == WM_QUIT)
bProgramEnded = true;
}
}
main_DestroyWindow(&hWnd,&hDC,&hInstance,"Window");
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
case WM_CLOSE:
case WM_QUIT:
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_SYSCOMMAND:
switch (wParam)
{
case SC_SCREENSAVE:
case SC_MONITORPOWER:
return 0;
}
break;
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
HWND main_CreateWindow(HINSTANCE hInst, WNDPROC* wndproc, HDC* hDevC,
char* ClassName, char* Title,int x, int y, int width, int height)
{
HWND hNewWindow = NULL;
*hDevC = NULL;
WNDCLASSEX wc;
RECT wRect;
wRect.top = 0;
wRect.left = 0;
wRect.bottom = height;
wRect.right = width;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = NULL;
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hInstance = hInst;
wc.lpfnWndProc = (WNDPROC) wndproc;
wc.lpszClassName = ClassName;
wc.lpszMenuName = NULL;
wc.hIconSm = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
if(!RegisterClassEx(&wc))
{
MessageBox(NULL,"Failed to Register Window Class","Window Creation Error",MB_OK | MB_ICONERROR);
main_DestroyWindow(&hNewWindow,hDevC,&hInst,ClassName);
return false;
}
AdjustWindowRectEx(&wRect, WS_OVERLAPPEDWINDOW, FALSE, WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
hNewWindow = CreateWindowEx( WS_EX_APPWINDOW | WS_EX_WINDOWEDGE,
ClassName,
Title,
WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
60,26,
wRect.right - wRect.left,
wRect.bottom - wRect.top,
NULL,NULL,
hInst,
NULL);
if(hNewWindow == NULL)
{
MessageBox(NULL,"Unable To Create Window","Window Creation Error",MB_OK | MB_ICONERROR);
main_DestroyWindow(&hNewWindow,hDevC,&hInst,ClassName);
return false;
}
if(!(*hDevC = GetDC(hNewWindow)))
{
MessageBox(NULL,"Unable To Obtain Device Context","Window Creation Error", MB_OK | MB_ICONERROR);
main_DestroyWindow(&hNewWindow,hDevC,&hInst,ClassName);
return false;
}
ShowWindow(hNewWindow,SW_SHOWNORMAL);
SetForegroundWindow(hNewWindow);
SetFocus(hNewWindow);
return hNewWindow;
}
bool main_DestroyWindow(HWND* hWind,HDC* hDevC,HINSTANCE* hInst,char* ClassName)
{
if(!ReleaseDC(*hWind,*hDevC))
{
MessageBox(NULL,"Unable To Release Device Context","Window Destruction Error",MB_OK | MB_ICONERROR);
*hDevC = NULL;
}
if(!DestroyWindow(*hWind))
{
MessageBox(NULL,"Unable To Destroy Window","Window Destruction Error",MB_OK | MB_ICONERROR);
*hWind = NULL;
}
if(!UnregisterClass(ClassName,*hInst))
{
MessageBox(NULL,"Unable To Unregister Window Class","Window Destruction Error",MB_OK | MB_ICONERROR);
*hInst = NULL;
}
return true;
}