I am a beginner in C ++. I recently read visual C ++ and directx9 3D Game Development Guide. Most of his programs are written in VC 6.0, the platform I use is vs2005, so many programs need to be modified. This is the second part of the book, and will be uploaded later. Please correct me.
First, create a new Win32 project and input the following source code:
# Include <tchar. h>
# Include <windows. h>
# Include "resource. H"
# Define apientry winapi
// Define global variables
Hinstance g_hinst;
Const tchar g_szwndclass [] = _ T ("game ");
Const tchar g_szwndtitle [] = _ T ("window sample program ");
Wndclassex g_wcex;
// Function prototype Declaration
Atom regwndclass (hinstance );
Bool initwnd (hinstance, INT );
Lresult callback wndproc (hwnd, uint, wparam, lparam );
// Function Definition
Int apientry winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int ncmdshow)
{
MSG;
If (! Hprevinstance)
{
Regwndclass (hinstance );
}
If (! Initwnd (hinstance, ncmdshow )){
Return false;
}
While (getmessage (& MSG, null, 0, 0 )){
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
Unregisterclass (g_szwndclass, g_wcex.hinstance );
Return msg. wparam;
}
Atom regwndclass (hinstance)
{
G_wcex.cbsize = sizeof (wndclassex );
G_wcex.style = cs_hredraw | cs_vredraw;
G_wcex.lpfnwndproc = (wndproc) wndproc;
G_wcex.cbclsextra = 0;
G_wcex.cbwndextra = 0;
G_wcex.hinstance = hinstance;
G_wcex.hicon = loadicon (hinstance, (lpctstr) idi_icon1); // modify the value according to "resource. H ".
G_wcex.hcursor = loadcursor (null, idc_arrow );
G_wcex.hbrbackground = (hbrush) getstockobject (black_brush );
G_wcex.lpszmenuname = 0;
G_wcex.lpszclassname = g_szwndclass;
G_wcex.hiconsm = NULL;
Return registerclassex (& g_wcex );
}
Bool initwnd (hinstance, int ncmdshow)
{
Hwnd;
Hwnd = createwindow (g_szwndclass, g_szwndtitle, ws_overlappedwindow, cw_usedefault, 0, cw_usedefault, 0, null, null, hinstance, null );
If (! Hwnd)
{
Return false;
}
Showwindow (hwnd, ncmdshow );
Updatewindow (hwnd );
Return true;
}
Lresult callback wndproc (hwnd, uint message, wparam, lparam)
{
Tchar sztitlemsg [] = _ T ("user input event prompt ");
Tchar szlmousemsg [] = _ T ("Left click ");
Tchar szrmousemsg [] = _ T ("right-click ");
Tchar szleftkeymsg [] = _ T ("press the escape arrow ");
Tchar szrightkeymsg [] = _ T ("Press → arrow keys ");
Tchar szupkeymsg [] = _ T ("press the escape arrow ");
Tchar szdownkeymsg [] = _ T ("press the escape arrow ");
Switch (Message ){
Case wm_lbuttondown:
MessageBox (null, szlmousemsg, sztitlemsg, mb_ OK | mb_iconinformation );
Break;
Case wm_rbuttondown:
MessageBox (null, szrmousemsg, sztitlemsg, mb_ OK | mb_iconinformation );
Break;
Case wm_keydown:
If (wparam = vk_left ){
MessageBox (null, szleftkeymsg, sztitlemsg, mb_ OK | mb_iconinformation );
}
If (wparam = vk_right ){
MessageBox (null, szrightkeymsg, sztitlemsg, mb_ OK | mb_iconinformation );
}
If (wparam = vk_up ){
MessageBox (null, szupkeymsg, sztitlemsg, mb_ OK | mb_iconinformation );
}
If (wparam = vk_down ){
MessageBox (null, szdownkeymsg, sztitlemsg, mb_ OK | mb_iconinformation );
}
Break;
Case wm_destroy:
Postquitmessage (0 );
Break;
Default:
Return defwindowproc (hwnd, message, wparam, lparam );
}
Return 0;
}
Right-click the "resource files" folder icon and choose "add"> "resource" from the shortcut menu ". In the "Add Resource" dialog box that appears, select "icon" and select "new". Enter a character with the mouse to act as the icon in the upper-left corner of the program window.
So far, a simple Win32 is complete.