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

  1. // PROG5_2.CPP - colored text printing
  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  400
  17. #define WINDOW_HEIGHT 400
  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.  
  33. // what is the message 
  34. switch(msg)
  35.     {    
  36.     case WM_CREATE: 
  37.         {
  38.         // do initialization stuff here
  39.         return(0);
  40.         } break;
  41.  
  42.     case WM_PAINT: 
  43.         {
  44.         // simply validate the window
  45.         hdc = BeginPaint(hwnd,&ps);     
  46.            EndPaint(hwnd,&ps);
  47.         return(0);
  48.            } break;
  49.  
  50.     case WM_DESTROY: 
  51.         {
  52.         // kill the application            
  53.         PostQuitMessage(0);
  54.         return(0);
  55.         } break;
  56.  
  57.     default:break;
  58.  
  59.     } // end switch
  60.  
  61. // process any messages that we didn't take care of 
  62. return (DefWindowProc(hwnd, msg, wparam, lparam));
  63.  
  64. } // end WinProc
  65.  
  66. // WINMAIN ////////////////////////////////////////////////
  67. int WINAPI WinMain(    HINSTANCE hinstance,
  68.                     HINSTANCE hprevinstance,
  69.                     LPSTR lpcmdline,
  70.                     int ncmdshow)
  71. {
  72.  
  73. WNDCLASS winclass;    // this will hold the class we create
  74. HWND     hwnd;        // generic window handle
  75. MSG         msg;        // generic message
  76. HDC      hdc;       // generic dc
  77. PAINTSTRUCT ps;     // generic paintstruct
  78. static int counter; // static counter
  79.  
  80. // first fill in the window class stucture
  81. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  82.                           CS_HREDRAW | CS_VREDRAW;
  83. winclass.lpfnWndProc    = WindowProc;
  84. winclass.cbClsExtra        = 0;
  85. winclass.cbWndExtra        = 0;
  86. winclass.hInstance        = hinstance;
  87. winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  88. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  89. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  90. winclass.lpszMenuName    = NULL;
  91. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  92.  
  93. // register the window class
  94. if (!RegisterClass(&winclass))
  95.     return(0);
  96.  
  97. // create the window
  98. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  99.                           "Simple Text!",     // title
  100.                           WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  101.                            0,0,       // x,y
  102.                           WINDOW_WIDTH,  // width
  103.                           WINDOW_HEIGHT, // height
  104.                           NULL,       // handle to parent 
  105.                           NULL,       // handle to menu
  106.                           hinstance,// instance
  107.                           NULL)))    // creation parms
  108. return(0);
  109.  
  110. // save the window handle in a global
  111. main_window_handle = hwnd;
  112.  
  113. // enter main event loop
  114. while(1)
  115.     {
  116.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  117.         { 
  118.         // test if this is a quit
  119.         if (msg.message == WM_QUIT)
  120.            break;
  121.     
  122.         // translate any accelerator keys
  123.         TranslateMessage(&msg);
  124.  
  125.         // send the message to the window proc
  126.         DispatchMessage(&msg);
  127.         } // end if
  128.     
  129.     // main game processing goes here
  130.     
  131.     // main game processing goes here /////////////////
  132.     // get the graphics device context
  133.     hdc = GetDC(hwnd);
  134.  
  135.     // create the output string and get its length 
  136.     int length = 
  137.     sprintf(buffer,"The Count is %d   ",(int)counter++);
  138.  
  139.     // print the text
  140.     TextOut(hdc,0,0,buffer,length);  
  141.  
  142.     // release the device context
  143.     ReleaseDC(hwnd,hdc);
  144.  
  145.     // wait a sec
  146.     Sleep(10);
  147.  
  148.     } // end while
  149.  
  150. // return to Windows like this
  151. return(msg.wParam);
  152.  
  153. } // end WinMain
  154.  
  155. ///////////////////////////////////////////////////////////
  156.  
  157.