home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / graphics / rect.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  14.1 KB  |  448 lines

  1. /*
  2.  
  3. Function(s) demonstrated in this program: SetRectEmpty, UnionRect, OffsetRect
  4.  
  5. Description:  The SetRectEmpty sets all the fields of a rectangle structure
  6.     to 0.  The UnionRect function takes two rectangle structures, and 
  7.     places in a third rectangle structure the coordinates of the smallest
  8.     rectangle which contains both of the other rectangles.  The OffsetRect
  9.     function adjusts the coordinates of the rectangle structrure by the 
  10.     offsets supplied.
  11.  
  12. */
  13.  
  14. #define NOMINMAX
  15. #include <windows.h>
  16. #include <stdlib.h>
  17. #include <math.h>
  18. #include <stdio.h>
  19. #include "Rect.h"
  20.  
  21. #define PI 3.14159265
  22.  
  23.  
  24. char    szRadius  [15];
  25. HANDLE  hInstMain;
  26. FARPROC lpProcGetRadius;
  27. char     szOutputBuffer1 [70];
  28. char     szOutputBuffer2 [500];
  29. HWND     hWndMain, hX, hY, hOK2;
  30. FARPROC  lpProcEnterPoint;
  31. FARPROC  lpProcNewEnterPoint;
  32. FARPROC  lpProcOldX;
  33. FARPROC  lpProcOldY;
  34. char     szXValue [40];
  35. char     szYValue [40];
  36.  
  37.  
  38. /****************************************************************************/
  39. /************************    Message Structure      *************************/
  40. /****************************************************************************/
  41.  
  42. struct { char *szMessage; }
  43.        Messages [] = {
  44. "About\0",
  45. "     This is a sample application to demonstrate the\n\
  46. use of the SetRectEmpty, UnionRect, and OffsetRect\n\
  47. Windows functions.",
  48.  
  49. "Help Message",
  50. "     This program uses the Windows functions\n\
  51. SetRectEmpty, UnionRect, and OffsetRect.  There are\n\
  52. three rectangles displayed in the window.  Two of\n\
  53. these windows are adjustable, and the third is\n\
  54. a union of the first two.  Use the menu to\n\
  55. adjust the size and position of the two rectangles\n\
  56. in pixels.",
  57.  
  58. };    
  59.  
  60. /****************************************************************************/
  61.  
  62. void ProcessMessage (HWND, int); 
  63.  
  64. void ProcessMessage (hWnd, MessageNumber) 
  65.      HWND     hWnd;
  66.      int      MessageNumber;
  67. {
  68.      sprintf (szOutputBuffer1, "%s", Messages [MessageNumber]);
  69.      sprintf (szOutputBuffer2, "%s", Messages [MessageNumber + 1]);
  70.      MessageBox (hWnd, szOutputBuffer2, szOutputBuffer1, MB_OK);
  71. }       
  72.  
  73. /****************************************************************************/
  74.  
  75. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  76.     HANDLE    hInstance, hPrevInstance;
  77.     LPSTR    lpszCmdLine;
  78.     int     nCmdShow;
  79.     {
  80.     static char szAppName[] = "Rect";
  81.     HWND    hWnd;
  82.     MSG     msg;
  83.     WNDCLASS    wndclass;
  84.  
  85.     if (!hPrevInstance)    {
  86.         wndclass.style          = CS_HREDRAW | CS_VREDRAW;
  87.         wndclass.lpfnWndProc    = WndProc;
  88.         wndclass.cbClsExtra     = 0;
  89.         wndclass.cbWndExtra     = 0;
  90.         wndclass.hInstance      = hInstance;
  91.         wndclass.hIcon          = NULL; /*LoadIcon (NULL, IDI_ASTERISK);*/
  92.         wndclass.hCursor        = LoadCursor (NULL, IDC_CROSS);
  93.         wndclass.hbrBackground  = GetStockObject (WHITE_BRUSH);
  94.         wndclass.lpszMenuName   = szAppName;
  95.         wndclass.lpszClassName  = szAppName;
  96.  
  97.         if (!RegisterClass (&wndclass) )
  98.             return FALSE;
  99.     }
  100.  
  101.  
  102.     hWnd = CreateWindow (szAppName, "Rect",
  103.             WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0,
  104.             CW_USEDEFAULT, 0, NULL, NULL,
  105.             hInstance, NULL);
  106.  
  107.     hInstMain = hInstance;
  108.     hWndMain  = hWnd;
  109.  
  110.     ShowWindow (hWnd, nCmdShow);
  111.     UpdateWindow (hWnd);
  112.  
  113.     lpProcEnterPoint    = MakeProcInstance (EnterPointDlgProc, hInstance);
  114.     lpProcNewEnterPoint = MakeProcInstance (NewEnterPointDlgProc,hInstance);
  115.  
  116.     while (GetMessage (&msg, NULL, 0, 0))    {
  117.        TranslateMessage (&msg);
  118.        DispatchMessage (&msg);
  119.     }
  120.     return msg.wParam;
  121. }
  122.  
  123. /****************************************************************************/
  124.  
  125. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  126.     HWND        hWnd;
  127.     unsigned    iMessage;
  128.     WORD        wParam;
  129.     LONG        lParam;
  130.     {
  131.    HMENU         hMenu;
  132. static   RECT    Rect1, Rect2, Rect3;
  133.     static short  xClient, yClient;
  134.     HDC            hDC;
  135.     PAINTSTRUCT   ps;
  136.    int           xOffset, yOffset;
  137.  
  138.     switch (iMessage) {
  139.        case WM_CREATE:
  140.             hMenu = GetSystemMenu (hWnd, FALSE);
  141.      
  142.             ChangeMenu (hMenu, NULL, "&About", IDM_ABOUT, 
  143.                         MF_APPEND | MF_STRING);
  144.             SetRectEmpty (&Rect1);
  145.             SetRectEmpty (&Rect2);
  146.             SetRectEmpty (&Rect3);
  147.  
  148.             Rect1.top = 50;
  149.             Rect1.left = 150;
  150.             Rect1.right = 250;
  151.             Rect1.bottom = 110;
  152.  
  153.             Rect2.top = 160;
  154.             Rect2.left = 400;
  155.             Rect2.right = 500;
  156.             Rect2.bottom = 220;
  157.             UnionRect (&Rect3, &Rect2, &Rect1);
  158.             break;
  159.      
  160.        case WM_SYSCOMMAND:
  161.             switch (wParam) {
  162.                case IDM_ABOUT:
  163.                     ProcessMessage (hWnd, 0);
  164.                     break;
  165.                default:
  166.                     return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  167.             }
  168.             break;
  169.  
  170.         case WM_SIZE:
  171.             xClient=LOWORD (lParam);
  172.             yClient=HIWORD (lParam);
  173.             break;
  174.  
  175.  
  176.        case WM_COMMAND:
  177.             switch (wParam) {
  178.                case IDM_CHANGERECT1:
  179.                     SetRectEmpty (&Rect1);
  180.                     MessageBox (hWnd, "Rectangle 1 has been emptied", 
  181.                                 "SetRectEmpty", MB_OK);
  182.                     sprintf (szYValue, "Enter Coordinates");
  183.                     sprintf (szXValue, "rect1.top,  rect1.left");
  184.                     DialogBox (hInstMain, (LPSTR)"EnterPointDlg", hWnd, 
  185.                            lpProcEnterPoint);
  186.                     Rect1.top  = atoi (szXValue);
  187.                     Rect1.left = atoi (szYValue);                      
  188.  
  189.                     sprintf (szYValue, "Enter Coordinates");
  190.                     sprintf (szXValue, "rect1.bottom, rect1.right");
  191.                     DialogBox (hInstMain, (LPSTR)"EnterPointDlg", hWnd, 
  192.                            lpProcEnterPoint);
  193.                     Rect1.bottom = atoi (szXValue);
  194.                     Rect1.right  = atoi (szYValue);                      
  195.  
  196.                     UnionRect (&Rect3, &Rect2, &Rect1);
  197.  
  198.                     InvalidateRect (hWnd, NULL, TRUE);
  199.                     UpdateWindow (hWnd);
  200.                     break;
  201.  
  202.                case IDM_CHANGERECT2:
  203.                     SetRectEmpty (&Rect2);
  204.                     MessageBox (hWnd, "Rectangle 2 has been emptied", 
  205.                                 "SetRectEmpty", MB_OK);
  206.                     sprintf (szYValue, "Enter Coordinates");
  207.                     sprintf (szXValue, "rect2.top,  rect2.left");
  208.                     DialogBox (hInstMain, (LPSTR)"EnterPointDlg", hWnd, 
  209.                            lpProcEnterPoint);
  210.                     Rect2.top  = atoi (szXValue);
  211.                     Rect2.left = atoi (szYValue);                      
  212.  
  213.                     sprintf (szYValue, "Enter Coordinates");
  214.                     sprintf (szXValue, "rect2.bottom, rect2.right");
  215.                     DialogBox (hInstMain, (LPSTR)"EnterPointDlg", hWnd, 
  216.                            lpProcEnterPoint);
  217.                     Rect2.bottom = atoi (szXValue);
  218.                     Rect2.right  = atoi (szYValue);                      
  219.  
  220.                     UnionRect (&Rect3, &Rect2, &Rect1);
  221.  
  222.                     InvalidateRect (hWnd, NULL, TRUE);
  223.                     UpdateWindow (hWnd);
  224.                     break;
  225.  
  226.                case IDM_OFFSETRECT1:
  227.                     sprintf (szYValue, "Enter Offsets");
  228.                     sprintf (szXValue, "X Offset, Y Offset");
  229.                     DialogBox (hInstMain, (LPSTR)"EnterPointDlg", hWnd, 
  230.                            lpProcEnterPoint);
  231.                     xOffset = atoi (szXValue);
  232.                     yOffset = atoi (szYValue);                      
  233.  
  234.                     OffsetRect (&Rect1, xOffset, yOffset);
  235.                     UnionRect (&Rect3, &Rect2, &Rect1);
  236.  
  237.                     InvalidateRect (hWnd, NULL, TRUE);
  238.                     UpdateWindow (hWnd);
  239.                     break;
  240.  
  241.                case IDM_OFFSETRECT2:
  242.                     sprintf (szYValue, "Enter Offsets");
  243.                     sprintf (szXValue, "X Offset, Y Offset");
  244.                     DialogBox (hInstMain, (LPSTR)"EnterPointDlg", hWnd, 
  245.                            lpProcEnterPoint);
  246.                     xOffset = atoi (szXValue);
  247.                     yOffset = atoi (szYValue);                      
  248.  
  249.                     OffsetRect (&Rect2, xOffset, yOffset);
  250.                     UnionRect (&Rect3, &Rect2, &Rect1);
  251.  
  252.                     InvalidateRect (hWnd, NULL, TRUE);
  253.                     UpdateWindow (hWnd);
  254.                     break;
  255.  
  256.                case IDM_HELP:
  257.                     ProcessMessage (hWnd, 2);
  258.                     break;
  259.             }
  260.             break;
  261.  
  262.         case WM_PAINT:
  263.             hDC=BeginPaint(hWnd, &ps);
  264.             DrawSquare (hDC, Rect1);
  265.             DrawSquare (hDC, Rect2);
  266.             DrawSquare (hDC, Rect3);
  267.  
  268.             EndPaint (hWnd, &ps);
  269.             break;
  270.  
  271.        case WM_DESTROY:
  272.             PostQuitMessage (0);
  273.             break;
  274.  
  275.         default:
  276.             return DefWindowProc( hWnd, iMessage, wParam, lParam );
  277.        }
  278.     return 0L;
  279. }
  280.  
  281. /***************************************************************************/
  282.  
  283. BOOL FAR PASCAL GetRadiusDlgProc (hDlg, iMessage, wParam, lParam)
  284. HWND hDlg;
  285. unsigned iMessage;
  286. WORD wParam;
  287. LONG lParam;
  288. {
  289.      int Index;
  290.      char szChange [10];
  291.      long lReturn;
  292.  
  293.      switch (iMessage) {
  294.         case WM_INITDIALOG:
  295.              SendDlgItemMessage (hDlg, IDD_RADIUS, EM_LIMITTEXT,
  296.                                  (WORD)80, 0L);
  297.              SetDlgItemText (hDlg, IDD_RADIUS, "1.0");
  298.              return TRUE;
  299.              break;
  300.  
  301.         case WM_COMMAND:
  302.              switch (wParam)
  303.              {
  304.                case IDD_RADIUS:
  305.                     if (HIWORD (lParam) == EN_CHANGE)
  306.                         EnableWindow (GetDlgItem(hDlg,IDOK),
  307.                                      (BOOL)SendMessage(LOWORD (lParam),
  308.                                       WM_GETTEXTLENGTH, 0, 0L)) ;
  309.                     break;
  310.             
  311.                case IDOK:                 
  312.                     GetDlgItemText (hDlg, IDD_RADIUS, szRadius, 80) ;
  313.                     OemToAnsi (szRadius, szRadius);
  314.                     EndDialog (hDlg, TRUE);
  315.                     break;
  316.    
  317.                case IDCANCEL:
  318.                   EndDialog (hDlg, FALSE) ;
  319.                     break;
  320.          
  321.                default:
  322.                     return FALSE;
  323.              }
  324.         default:
  325.              return FALSE;
  326.      }
  327.      return TRUE;
  328. }
  329.  
  330. /****************************************************************************/
  331.  
  332. BOOL FAR PASCAL EnterPointDlgProc (hDlg, iMessage, wParam, lParam)
  333. HWND hDlg;
  334. unsigned iMessage;
  335. WORD wParam;
  336. LONG lParam;
  337. {
  338.      int Index;
  339.      char szChange [10];
  340.      long lReturn;
  341.  
  342.      switch (iMessage) {
  343.      case WM_INITDIALOG:
  344.        SendDlgItemMessage (hDlg, IDD_X, EM_LIMITTEXT,
  345.                            (WORD)40, 0L);
  346.        SendDlgItemMessage (hDlg, IDD_Y, EM_LIMITTEXT,
  347.                            (WORD)40, 0L);
  348.        SetDlgItemText (hDlg, IDD_TEXT1, szXValue);
  349.        SetDlgItemText (hDlg, IDD_TEXT2, szYValue);
  350.  
  351.        hX = GetDlgItem (hDlg, IDD_X);
  352.          lpProcOldX = (FARPROC) GetWindowLong (hX, GWL_WNDPROC);
  353.          SetWindowLong (hX, GWL_WNDPROC, (LONG) lpProcNewEnterPoint);
  354.          SendMessage (hX, EM_SETSEL, 0, MAKELONG (0,32767));
  355.        hY = GetDlgItem (hDlg, IDD_Y);
  356.          lpProcOldY = (FARPROC) GetWindowLong (hY, GWL_WNDPROC);
  357.          SetWindowLong (hY, GWL_WNDPROC, (LONG) lpProcNewEnterPoint);
  358.          SendMessage (hY, EM_SETSEL, 0, MAKELONG (0,32767));
  359.        hOK2 = GetDlgItem (hDlg, IDOK);
  360.      
  361.        return TRUE;
  362.        break;
  363.  
  364.      case WM_COMMAND:
  365.        switch (wParam) {
  366.          case IDD_X:
  367.               break;
  368.  
  369.          case IDD_Y:
  370.               break;
  371.          
  372.          case IDOK:            
  373.               GetDlgItemText (hDlg, IDD_X, szXValue, 10) ;
  374.               GetDlgItemText (hDlg, IDD_Y, szYValue, 10) ;
  375.               EndDialog (hDlg, TRUE);
  376.               break;
  377.  
  378.          default:
  379.               return FALSE;
  380.       }
  381.     default:
  382.       return FALSE;
  383.   }
  384.   return TRUE;
  385. }
  386.  
  387. /****************************************************************************/
  388.  
  389. BOOL FAR PASCAL NewEnterPointDlgProc  (hWnd, iMessage, wParam, lParam) 
  390.      HWND     hWnd;
  391.      unsigned iMessage; 
  392.      WORD     wParam;
  393.      LONG     lParam;
  394. {
  395.      switch (iMessage) {
  396.        case WM_GETDLGCODE:
  397.             return (DLGC_WANTALLKEYS);
  398.  
  399.        case WM_CHAR:
  400.             if ((wParam == VK_TAB) || (wParam == VK_RETURN)) {
  401.                 SendMessage (hWndMain, WM_USER, 0, 0L);
  402.                 SetFocus (FindNextWindow (hWnd));
  403.                 return TRUE;
  404.             }
  405.             else {
  406.               if (hWnd == hX) 
  407.                 return ((BOOL)CallWindowProc (lpProcOldX, hWnd, 
  408.                         iMessage, wParam, lParam));
  409.               if (hWnd == hY) 
  410.                 return ((BOOL)CallWindowProc (lpProcOldY, hWnd, 
  411.                         iMessage, wParam, lParam));
  412.             }
  413.             break;
  414.  
  415.        default:
  416.          if (hWnd == hX)
  417.             return ((BOOL)CallWindowProc (lpProcOldX, hWnd,
  418.                     iMessage, wParam, lParam));
  419.          if (hWnd == hY)
  420.             return ((BOOL)CallWindowProc (lpProcOldY, hWnd, 
  421.                     iMessage, wParam, lParam));
  422.      }
  423. }
  424.  
  425. /****************************************************************************/
  426.  
  427. HWND FindNextWindow (hWnd)
  428.      HWND   hWnd;
  429. {
  430.      if (hWnd == hX)      return hY;
  431.      if (hWnd == hY)      return hOK2;
  432.      return NULL;
  433. }
  434.  
  435. /****************************************************************************/
  436.  
  437. void DrawSquare (hDC, rect)
  438.      HDC       hDC;
  439.      RECT      rect;
  440. {
  441.      MoveTo (hDC, rect.left,  rect.top);
  442.      LineTo (hDC, rect.right, rect.top);
  443.      LineTo (hDC, rect.right, rect.bottom);
  444.      LineTo (hDC, rect.left,  rect.bottom);
  445.      LineTo (hDC, rect.left,  rect.top);
  446. }
  447.  
  448.