Code:
#include "stdSDK.h"
HWND createMainWindow(HINSTANCE hinst, int nCmdShow)
{
HWND hwnd;
TCHAR ClassName[MAX_RESOURCESTRING+1];
TCHAR title[MAX_RESOURCESTRING+1];
// load class name into ClassName
VERIFY(LoadString(hinst,IDR_MAINWINDOW,ClassName,DIM(ClassName)));
// load title name into title
VERIFY(LoadString(hinst,IDS_TITLE,title,DIM(title)));
hwnd = CreateWindowEx(0, // extended window style
ClassName, // class name
title, // title
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // default x-pos of window
0, // y-pos of window, set 0 and use showWindow() to determine later
400, // width of window
400, // height of window
NULL, // parent of window
LoadMenu(hinst, MAKEINTRESOURCE(IDR_MAINWINDOW)), // handle of menu
hinst, //handle of application
NULL); // user defined parameters, hardly any use for it
if(!hwnd)
return NULL;
// make window appear
ShowWindow(hwnd,nCmdShow);
// force window to update
UpdateWindow(hwnd);
return hwnd;
}
void mainWindow_onDestroy(HWND hwnd)
{
PostQuitMessage(0);
}
void mainWindow_onCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
switch(id)
{case ID_FILE_EXIT: DestroyWindow(hwnd);
return;
default: FORWARD_WM_COMMAND(hwnd,id,hwndCtl,codeNotify,DefWindowProc);
}
}
LRESULT CALLBACK wndProc(HWND hwnd, UINT message,WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY: // when window is close,
return HANDLE_WM_DESTROY (hwnd,wParam,lParam,mainWindow_onDestroy);
case WM_COMMAND: // when user select an item in the menu
return HANDLE_WM_COMMAND (hwnd,wParam,lParam,mainWindow_onCommand);
default: return DefWindowProc(hwnd, message, wParam, lParam);
}
}
// NOTE: DIFFERENCE BETWEEN WM_DESTROY AND WM_CLOSE
// WM_CLOSE REFERS THAT THERE IS REQUEST TO CLOSE THE WINDOW
// W_DESTROY REFERS THAT THE WINDOW IS CLOSED
bool registerWindowClasses(HINSTANCE hinst, UINT resPoolID)
{
WNDCLASSEX wc;
TCHAR ClassName[MAX_RESOURCESTRING+1];
// load class name into ClassName
VERIFY(LoadString(hinst,resPoolID,ClassName,DIM(ClassName)));
// fill in window classs structure with parameters that describe the main window
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW ;
wc.lpfnWndProc = (WNDPROC) wndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hinst;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszMenuName = MAKEINTRESOURCE(resPoolID);
wc.lpszClassName = ClassName;
wc.hIconSm = LoadIcon(NULL,IDI_WINLOGO);
// register class into Windows and return the ATOM result
// 0 if fail
if(RegisterClassEx(&wc))
return true;
else return false;
}
bool initInstance(HINSTANCE hinst, UINT resPoolID, int nCmdShow)
{
// register window class
if(!registerWindowClasses(hinst,resPoolID))
return false;
// initialize common controls DLL
// you must call this function beofre using any common control
InitCommonControls();
// create the application's main frame window
if(!createMainWindow(hinst,nCmdShow))
return false;
return true;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpszCmdLine,int nCmdShow)
{
MSG msg;
// instance initialization
if(!initInstance(hInstance,IDR_MAINWINDOW,nCmdShow))
return 0;
// GetMessage() :
// first parameter stores the message obtained from message loop
// second parameter specify the window handle for which to retrieve a msg
// if second parameter is NULL, the next message belonging to the application is obtained
// 3rd and 4th specify the range of message to obtain
// GetMessage() return 0 when WM_QUIT is retrieved
while(GetMessage(&msg,NULL,0,0))
{TranslateMessage(&msg); // translate the virtual keys for WinProc to intepret
DispatchMessage(&msg); // send message to WinProc
}
return msg.wParam;
}