DRAW.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:7k
源码类别:

Windows编程

开发平台:

Visual C++

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