home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / Source / GPCHAP04 / PROG4_1.CPP next >
C/C++ Source or Header  |  2002-04-24  |  3KB  |  132 lines

  1. // PROG4_1.CPP - A complete windows program
  2.  
  3. // INCLUDES ///////////////////////////////////////////////
  4. #define WIN32_LEAN_AND_MEAN  
  5.  
  6. #include <windows.h>  
  7. #include <windowsx.h>
  8. #include <stdio.h>
  9. #include <math.h>
  10.  
  11. // DEFINES ////////////////////////////////////////////////
  12.  
  13. // defines for windows 
  14. #define WINDOW_CLASS_NAME "WINCLASS1"
  15.  
  16. // GLOBALS ////////////////////////////////////////////////
  17. HWND main_window_handle = NULL; // save the window handle
  18.  
  19. // FUNCTIONS //////////////////////////////////////////////
  20. LRESULT CALLBACK WindowProc(HWND hwnd, 
  21.                             UINT msg, 
  22.                             WPARAM wparam, 
  23.                             LPARAM lparam)
  24. {
  25. // this is the main message handler of the system
  26. PAINTSTRUCT        ps;        // used in WM_PAINT
  27. HDC                hdc;    // handle to a device context
  28.  
  29. // what is the message 
  30. switch(msg)
  31.     {    
  32.     case WM_CREATE: 
  33.         {
  34.         // do initialization stuff here
  35.         return(0);
  36.         } break;
  37.  
  38.     case WM_PAINT: 
  39.         {
  40.         // simply validate the window
  41.         hdc = BeginPaint(hwnd,&ps);     
  42.         EndPaint(hwnd,&ps);
  43.         return(0);
  44.            } break;
  45.  
  46.     case WM_DESTROY: 
  47.         {
  48.         // kill the application            
  49.         PostQuitMessage(0);
  50.         return(0);
  51.         } break;
  52.  
  53.     default:break;
  54.  
  55.     } // end switch
  56.  
  57. // process any messages that we didn't take care of 
  58. return (DefWindowProc(hwnd, msg, wparam, lparam));
  59.  
  60. } // end WinProc
  61.  
  62. // WINMAIN ////////////////////////////////////////////////
  63. int WINAPI WinMain(    HINSTANCE hinstance,
  64.                     HINSTANCE hprevinstance,
  65.                     LPSTR lpcmdline,
  66.                     int ncmdshow)
  67. {
  68.  
  69. WNDCLASS winclass;    // this will hold the class we create
  70. HWND     hwnd;        // generic window handle
  71. MSG         msg;        // generic message
  72.  
  73. // first fill in the window class stucture
  74. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  75.                           CS_HREDRAW | CS_VREDRAW;
  76. winclass.lpfnWndProc    = WindowProc;
  77. winclass.cbClsExtra        = 0;
  78. winclass.cbWndExtra        = 0;
  79. winclass.hInstance        = hinstance;
  80. winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  81. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  82. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  83. winclass.lpszMenuName    = NULL;
  84. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  85.  
  86. // register the window class
  87. if (!RegisterClass(&winclass))
  88.     return(0);
  89.  
  90. // create the window
  91. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  92.                           "Hello Dave",         // title
  93.                           WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  94.                            0,0,       // x,y
  95.                           320,200, // width, height
  96.                           NULL,       // handle to parent 
  97.                           NULL,       // handle to menu
  98.                           hinstance,// instance
  99.                           NULL)))    // creation parms
  100. return(0);
  101.  
  102. // save the window handle in a global
  103. main_window_handle = hwnd;
  104.  
  105. // enter main event loop
  106. while(1)
  107.     {
  108.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  109.         { 
  110.         // test if this is a quit
  111.         if (msg.message == WM_QUIT)
  112.            break;
  113.     
  114.         // translate any accelerator keys
  115.         TranslateMessage(&msg);
  116.  
  117.         // send the message to the window proc
  118.         DispatchMessage(&msg);
  119.         } // end if
  120.     
  121.     // main game processing goes here
  122.     
  123.     } // end while
  124.  
  125. // return to Windows like this
  126. return(msg.wParam);
  127.  
  128. } // end WinMain
  129.  
  130. ///////////////////////////////////////////////////////////
  131.  
  132.