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

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