home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / gdi / gdidemo / draw.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-05  |  6.7 KB  |  238 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples. 
  4. *       Copyright (C) 1993-1997 Microsoft Corporation.
  5. *       All rights reserved. 
  6. *       This source code is only intended as a supplement to 
  7. *       Microsoft Development Tools and/or WinHelp documentation.
  8. *       See these sources for detailed information regarding the 
  9. *       Microsoft samples programs.
  10. \******************************************************************************/
  11.  
  12. /*---------------------------------------------------------------------------*\
  13. | RANDOM OBJECTS MODULE
  14. \*---------------------------------------------------------------------------*/
  15.  
  16. #include <windows.h>
  17. #include "gdidemo.h"
  18. #include "draw.h"
  19.  
  20. /*---------------------------------------------------------------------------*\
  21. | CREATE DRAW WINDOW PROCEDURE
  22. \*---------------------------------------------------------------------------*/
  23. HWND FAR CreateDrawWindow(HWND hWndClient, int nItem)
  24. {
  25.     HANDLE          hInstance;
  26.     MDICREATESTRUCT mcs;
  27.     static char buffer[256];
  28.  
  29.  
  30.     hInstance = GETINSTANCE(hWndClient);
  31.     LoadString (hInstance, DRAWTITLE, buffer, 256);
  32.  
  33.     /*
  34.     ** Initialize the MDI create struct for creation of the
  35.     ** test window.
  36.     */
  37.     mcs.szClass = DRAWCLASS;
  38.     mcs.szTitle = buffer;
  39.     mcs.hOwner  = hInstance;
  40.     mcs.x       = CW_USEDEFAULT;
  41.     mcs.y       = CW_USEDEFAULT;
  42.     mcs.cx      = CW_USEDEFAULT;
  43.     mcs.cy      = CW_USEDEFAULT;
  44.     mcs.style   = 0l;
  45.     mcs.lParam  = (LONG)nItem;
  46.  
  47.     return((HWND)SendMessage(hWndClient,WM_MDICREATE,0,(LONG)(LPMDICREATESTRUCT)&mcs));
  48. }
  49.  
  50.  
  51. /*---------------------------------------------------------------------------*\
  52. | DRAW WINDOW PROCEDURE
  53. \*---------------------------------------------------------------------------*/
  54. LONG APIENTRY DrawProc(HWND hWnd, UINT wMsg, WPARAM wParam, LONG lParam)
  55. {
  56.     switch(wMsg)
  57.     {
  58.         case WM_CREATE:
  59.             DrawCreateProc(hWnd);
  60.             break;
  61.  
  62.  
  63.         case WM_COMMAND:
  64.             DrawCommandProc(hWnd,wParam,lParam);
  65.             break;
  66.  
  67.  
  68.         case WM_TIMER:
  69.             DrawObject(hWnd);
  70.             break;
  71.  
  72.  
  73.         case WM_PAINT:
  74.             DrawPaintProc(hWnd);
  75.             break;
  76.  
  77.  
  78.         case WM_DESTROY:
  79.             DrawDestroyProc(hWnd);
  80.             break;
  81.  
  82.  
  83.         default:
  84.             return(DefMDIChildProc(hWnd,wMsg,wParam,lParam));
  85.     }
  86.     return(0l);
  87. }
  88.  
  89.  
  90. /*---------------------------------------------------------------------------*\
  91. | DRAW CREATE PROCEDURE
  92. \*---------------------------------------------------------------------------*/
  93. BOOL DrawCreateProc(HWND hWnd)
  94. {
  95.     PDRAWDATA pdd;
  96.  
  97.  
  98.     if(AllocWindowInfo(hWnd,sizeof(DRAWDATA)))
  99.     {
  100.         if(pdd = (PDRAWDATA)LockWindowInfo(hWnd))
  101.         {
  102.             pdd->nObject = 0;
  103.  
  104.             UnlockWindowInfo(hWnd);
  105.  
  106.             SetTimer(hWnd,1,50,NULL);
  107.             return(TRUE);
  108.         }
  109.         FreeWindowInfo(hWnd);
  110.     }
  111.     return(FALSE);
  112. }
  113.  
  114.  
  115. /*---------------------------------------------------------------------------*\
  116. | DRAW COMMAND PROCEDURE
  117. \*---------------------------------------------------------------------------*/
  118. BOOL DrawCommandProc(HWND hWnd, WPARAM wParam, LONG lParam)
  119. {
  120.     hWnd   = hWnd;
  121.     wParam = wParam;
  122.     lParam = lParam;
  123.  
  124.     return(TRUE);
  125. }
  126.  
  127.  
  128. /*---------------------------------------------------------------------------*\
  129. | DRAW PAINT PROCEDURE
  130. \*---------------------------------------------------------------------------*/
  131. VOID DrawPaintProc(HWND hWnd)
  132. {
  133.     HDC         hDC;
  134.     PAINTSTRUCT ps;
  135.  
  136.  
  137.     if(hDC = BeginPaint(hWnd,&ps))
  138.         EndPaint(hWnd,&ps);
  139.  
  140.     return;
  141. }
  142.  
  143.  
  144. /*---------------------------------------------------------------------------*\
  145. | DRAW DESTROY PROCEDURE
  146. \*---------------------------------------------------------------------------*/
  147. VOID DrawDestroyProc(HWND hWnd)
  148. {
  149.     KillTimer(hWnd,1);
  150.     FreeWindowInfo(hWnd);
  151.     return;
  152. }
  153.  
  154.  
  155. VOID DrawObject(HWND hWnd)
  156. {
  157.     PDRAWDATA pdd;
  158.     RECT      rect;
  159.     HDC       hDC;
  160.     int       x1,y1,x2,y2,x3,y3,x4,y4,r,g,b,nObject;
  161.     HBRUSH    hBrush;
  162.     char      szDebug[80];
  163.  
  164.  
  165.     if(pdd = (PDRAWDATA)LockWindowInfo(hWnd))
  166.     {
  167.         if(hDC = GetDC(hWnd))
  168.         {
  169.             GetClientRect(hWnd,&rect);
  170.  
  171.             // avoid divide by zero errors when the window is small.
  172.             if ( rect.right== 0) rect.right++;
  173.             if ( rect.bottom== 0) rect.bottom++;
  174.  
  175.             r = lRandom() % 255;
  176.             g = lRandom() % 255;
  177.             b = lRandom() % 255;
  178.  
  179.             if(hBrush = SelectObject(hDC,CreateSolidBrush(RGB(r,g,b))))
  180.             {
  181.                 x1 = lRandom() % rect.right;
  182.                 y1 = lRandom() % rect.bottom;
  183.                 x2 = lRandom() % rect.right;
  184.                 y2 = lRandom() % rect.bottom;
  185.                 x3 = lRandom() % rect.right;
  186.                 y3 = lRandom() % rect.bottom;
  187.                 x4 = lRandom() % rect.right;
  188.                 y4 = lRandom() % rect.bottom;
  189.  
  190.  
  191.                 nObject = lRandom() % 5;
  192.  
  193.                 switch(nObject)
  194.                 {
  195.                     default:
  196.                     case OBJ_RECTANGLE:
  197.                         wsprintf(szDebug,"Rectangle(%d,%d,%d,%d)\n",x1,y1,x2,y2);
  198.                         DEBUGOUT(szDebug);
  199.                         Rectangle(hDC,x1,y1,x2,y2);
  200.                         break;
  201.  
  202.                     case OBJ_ELLIPSE:
  203.                         wsprintf(szDebug,"Ellipse(%d,%d,%d,%d)\n",x1,y1,x2,y2);
  204.                         DEBUGOUT(szDebug);
  205.                         Ellipse(hDC,x1,y1,x2,y2);
  206.                         break;
  207.  
  208.  
  209.  
  210.                     case OBJ_ROUNDRECT:
  211.                         wsprintf(szDebug,"RoundRect(%d,%d,%d,%d,%d,%d)\n",x1,y1,x2,y2,x3,y3);
  212.                         DEBUGOUT(szDebug);
  213.                         RoundRect(hDC,x1,y1,x2,y2,x3,y3);
  214.                         break;
  215.  
  216.                     case OBJ_CHORD:
  217.                         wsprintf(szDebug,"Chord(%d,%d,%d,%d,%d,%d,%d,%d)\n",x1,y1,x2,y2,x3,y3,x4,y4);
  218.                         DEBUGOUT(szDebug);
  219.                         Chord(hDC,x1,y1,x2,y2,x3,y3,x4,y4);
  220.                         break;
  221.  
  222.                     case OBJ_PIE:
  223.                         wsprintf(szDebug,"Pie(%d,%d,%d,%d,%d,%d,%d,%d)\n",x1,y1,x2,y2,x3,y3,x4,y4);
  224.                         DEBUGOUT(szDebug);
  225.                         Pie(hDC,x1,y1,x2,y2,x3,y3,x4,y4);
  226.                         break;
  227.  
  228.                 }
  229.  
  230.                 DeleteObject(SelectObject(hDC,hBrush));
  231.             }
  232.             ReleaseDC(hWnd,hDC);
  233.         }
  234.         UnlockWindowInfo(hWnd);
  235.     }
  236.     return;
  237. }
  238.