home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / Source / GPCHAP06 / PROG6_2.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-25  |  5.2 KB  |  195 lines

  1. // PROG6_2.CPP - basic menus
  2.  
  3. // INCLUDES ///////////////////////////////////////////////
  4. #define WIN32_LEAN_AND_MEAN  
  5.  
  6. #include <windows.h>  
  7. #include <windowsx.h> 
  8. #include <winuser.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include "mymenu.h"
  13.  
  14. // DEFINES ////////////////////////////////////////////////
  15.  
  16. // defines for windows 
  17. #define WINDOW_CLASS_NAME "WINCLASS1"
  18. #define WINDOW_WIDTH  320
  19. #define WINDOW_HEIGHT 200
  20.  
  21. // GLOBALS ////////////////////////////////////////////////
  22. HWND main_window_handle = NULL; // save the window handle
  23. char buffer[80];                // used to print text
  24.  
  25. // FUNCTIONS //////////////////////////////////////////////
  26. LRESULT CALLBACK WindowProc(HWND hwnd, 
  27.                             UINT msg, 
  28.                             WPARAM wparam, 
  29.                             LPARAM lparam)
  30. {
  31. // this is the main message handler of the system
  32. PAINTSTRUCT    ps;           // used in WM_PAINT
  33. HDC            hdc;       // handle to a device context
  34.  
  35. // what is the message 
  36. switch(msg)
  37.     {    
  38.     case WM_CREATE: 
  39.         {
  40.         // do initialization stuff here
  41.         return(0);
  42.         } break;
  43.  
  44.     // process menu messages
  45.     case WM_COMMAND:
  46.          {
  47.          // get the dc
  48.          hdc = GetDC(hwnd);
  49.  
  50.          // set the color
  51.          SetTextColor(hdc,RGB(0,0,255));
  52.          SetBkColor(hdc,RGB(0,0,0));
  53.          SetBkMode(hdc,OPAQUE);
  54.  
  55.          // what menu item?
  56.          switch(wparam)
  57.                {
  58.                case ID_MYMENU_FILE_OPEN: 
  59.                     {
  60.                     // print message
  61.                     TextOut(hdc,0,100,"File->Open     ",strlen("File->Open     "));
  62.                     } break;
  63.                case ID_MYMENU_FILE_CLOSE:  
  64.                     {
  65.                     // print message
  66.                     TextOut(hdc,0,100,"File->Close     ",strlen("File->Close     "));
  67.                     } break;
  68.                case ID_MYMENU_FILE_EXIT:   
  69.                     { 
  70.                     // terminate the application
  71.                     PostMessage(hwnd,WM_DESTROY,0,0);
  72.                     } break;
  73.                case ID_MYMENU_HELP_ABOUT:
  74.                     {
  75.                     // print message
  76.                     TextOut(hdc,0,100,"Help->About     ",strlen("Help->About     "));    
  77.                     
  78.                     // put up a message box
  79.                     MessageBox(hwnd,"Poor Man's About","About Menus",MB_OK);
  80.  
  81.                     } break;
  82.            
  83.                default: break;
  84.                } // end switch
  85.  
  86.          // release the dc
  87.          ReleaseDC(hwnd,hdc);
  88.  
  89.          // inform windows we handled message
  90.          return(0);
  91.          } break;
  92.  
  93.     case WM_PAINT:
  94.          {
  95.          // start painting
  96.          hdc = BeginPaint(hwnd,&ps);
  97.  
  98.          // end painting
  99.          EndPaint(hwnd,&ps);
  100.          return(0);
  101.         } break;
  102.  
  103.     case WM_DESTROY: 
  104.         {
  105.         // kill the application            
  106.         PostQuitMessage(0);
  107.         return(0);
  108.         } break;
  109.  
  110.     default:break;
  111.  
  112.     } // end switch
  113.  
  114. // process any messages that we didn't take care of 
  115. return (DefWindowProc(hwnd, msg, wparam, lparam));
  116.  
  117. } // end WinProc
  118.  
  119. // WINMAIN ////////////////////////////////////////////////
  120. int WINAPI WinMain(    HINSTANCE hinstance,
  121.                     HINSTANCE hprevinstance,
  122.                     LPSTR lpcmdline,
  123.                     int ncmdshow)
  124. {
  125.  
  126. WNDCLASS winclass;    // this will hold the class we create
  127. HWND     hwnd;        // generic window handle
  128. MSG         msg;        // generic message
  129. HDC      hdc;       // generic dc
  130. PAINTSTRUCT ps;     // generic paintstruct
  131.  
  132. // first fill in the window class stucture
  133. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  134.                           CS_HREDRAW | CS_VREDRAW;
  135. winclass.lpfnWndProc    = WindowProc;
  136. winclass.cbClsExtra        = 0;
  137. winclass.cbWndExtra        = 0;
  138. winclass.hInstance        = hinstance;
  139. winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  140. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  141. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  142. winclass.lpszMenuName    = NULL; 
  143. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  144.  
  145. // register the window class
  146. if (!RegisterClass(&winclass))
  147.     return(0);
  148.  
  149. // create the window
  150. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  151.                           "Basic Menus",     // title
  152.                           WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  153.                            0,0,       // x,y
  154.                           WINDOW_WIDTH,  // width
  155.                           WINDOW_HEIGHT, // height
  156.                           NULL,       // handle to parent 
  157.                           NULL,       // handle to menu
  158.                           hinstance,// instance
  159.                           NULL)))    // creation parms
  160. return(0);
  161.  
  162. // save the window handle in a global
  163. main_window_handle = hwnd;
  164.  
  165. // load the menu in and attach it (method 3)
  166. SetMenu(hwnd,LoadMenu(hinstance, "MYMENU"));
  167.  
  168. // enter main event loop
  169. while(1)
  170.     {
  171.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  172.         { 
  173.         // test if this is a quit
  174.         if (msg.message == WM_QUIT)
  175.            break;
  176.     
  177.         // translate any accelerator keys
  178.         TranslateMessage(&msg);
  179.  
  180.         // send the message to the window proc
  181.         DispatchMessage(&msg);
  182.         } // end if
  183.     
  184.     // main game processing goes here
  185.  
  186.     } // end while
  187.  
  188. // return to Windows like this
  189. return(msg.wParam);
  190.  
  191. } // end WinMain
  192.  
  193. ///////////////////////////////////////////////////////////
  194.  
  195.