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

  1. /*
  2.  
  3. Function(s) demonstrated in this program: GetViewportExt, GetWindowExt, 
  4.     OffsetViewportOrg, OffsetWindowOrg, SetViewportExt, SetWindowExt, 
  5.      ScaleViewportExt, ScaleWindowExt, SetViewportOrg, SetWindowOrg
  6.  
  7. Windows version:  2.03
  8.  
  9. Windows SDK version:  2.00
  10.  
  11. Compiler version:  C 5.10
  12.  
  13. Description:  These functions modify the interface between screen pixels and
  14.     the screen viewport.
  15.  
  16. Additional Comments:  The Scale functions (ScaleViewportExt, ScaleWindowExt),
  17.     both take 5 parameters the first is a handle to a display context and 
  18.     the next 4 are the numerators and denominators of the fractions that 
  19.     will be used to scale the x and y extents.
  20.  
  21. */
  22.  
  23. #define NOMINMAX
  24. #include <windows.h>
  25. #include <stdlib.h>
  26. #include <math.h>
  27. #include <stdio.h>
  28. #include "ViewPort.h"
  29.  
  30. #define PI 3.14159265
  31.  
  32.  
  33. char    szRadius  [15];
  34. HANDLE  hInstMain;
  35. FARPROC lpProcGetRadius;
  36. char     szOutputBuffer1 [70];
  37. char     szOutputBuffer2 [500];
  38. HWND     hWndMain, hX, hY, hOK2;
  39. FARPROC  lpProcEnterPoint;
  40. FARPROC  lpProcNewEnterPoint;
  41. FARPROC  lpProcOldX;
  42. FARPROC  lpProcOldY;
  43. char     szXValue [40];
  44. char     szYValue [40];
  45.  
  46.  
  47. /****************************************************************************/
  48. /************************    Message Structure      *************************/
  49. /****************************************************************************/
  50.  
  51. struct { char *szMessage; }
  52.        Messages [] = {
  53. "About\0",
  54. "     This is a sample application to demonstrate the\n\
  55. use of ViewPort Window Extent functions.",
  56.  
  57. "Help Message",
  58. "     This program uses the Windows functions\n\
  59. GetViewportExt, GetWindowExt, OffsetViewportOrg,\n\
  60. OffsetWindowOrg, SetViewportExt, SetWindowExt,\n\
  61. ScaleViewportExt, SetViewportOrg, SetWindowOrg\n\
  62. and ScaleWindowExt to modify the relationship\n\
  63. between the viewport and the screen pixels.  Use\n\
  64. the menu to request the function, then set the\n\
  65. variables accordingly.",
  66.  
  67. };    
  68.  
  69. /****************************************************************************/
  70.  
  71. void ProcessMessage (HWND, int); 
  72.  
  73. void ProcessMessage (hWnd, MessageNumber) 
  74.      HWND     hWnd;
  75.      int      MessageNumber;
  76. {
  77.      sprintf (szOutputBuffer1, "%s", Messages [MessageNumber]);
  78.      sprintf (szOutputBuffer2, "%s", Messages [MessageNumber + 1]);
  79.      MessageBox (hWnd, szOutputBuffer2, szOutputBuffer1, MB_OK);
  80. }       
  81.  
  82. /****************************************************************************/
  83.  
  84. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  85.     HANDLE    hInstance, hPrevInstance;
  86.     LPSTR    lpszCmdLine;
  87.     int     nCmdShow;
  88.     {
  89.     static char szAppName[] = "ViewPort";
  90.     HWND    hWnd;
  91.     MSG     msg;
  92.     WNDCLASS    wndclass;
  93.  
  94.     if (!hPrevInstance)    {
  95.         wndclass.style          = CS_HREDRAW | CS_VREDRAW;
  96.         wndclass.lpfnWndProc    = WndProc;
  97.         wndclass.cbClsExtra     = 0;
  98.         wndclass.cbWndExtra     = 0;
  99.         wndclass.hInstance      = hInstance;
  100.         wndclass.hIcon          = NULL; /*LoadIcon (NULL, IDI_ASTERISK);*/
  101.         wndclass.hCursor        = LoadCursor (NULL, IDC_CROSS);
  102.         wndclass.hbrBackground  = GetStockObject (WHITE_BRUSH);
  103.         wndclass.lpszMenuName   = szAppName;
  104.         wndclass.lpszClassName  = szAppName;
  105.  
  106.         if (!RegisterClass (&wndclass) )
  107.             return FALSE;
  108.     }
  109.  
  110.  
  111.     hWnd = CreateWindow (szAppName, "ViewPort",
  112.             WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0,
  113.         CW_USEDEFAULT, 0, NULL, NULL,
  114.             hInstance, NULL);
  115.  
  116.     hInstMain = hInstance;
  117.     hWndMain  = hWnd;
  118.  
  119.     ShowWindow (hWnd, nCmdShow);
  120.     UpdateWindow (hWnd);
  121.  
  122.     lpProcEnterPoint    = MakeProcInstance (EnterPointDlgProc, hInstance);
  123.     lpProcNewEnterPoint = MakeProcInstance (NewEnterPointDlgProc,hInstance);
  124.  
  125.     while (GetMessage (&msg, NULL, 0, 0))    {
  126.        TranslateMessage (&msg);
  127.        DispatchMessage (&msg);
  128.     }
  129.     return msg.wParam;
  130. }
  131.  
  132. /****************************************************************************/
  133.  
  134. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  135.     HWND        hWnd;
  136.     unsigned    iMessage;
  137.     WORD        wParam;
  138.     LONG        lParam;
  139.     {
  140.    HMENU         hMenu;
  141.     static HRGN   hRgnClip;
  142.     static double xXcoord, yYcoord, fRadiusInc=(float)1, fRadius=(float)0, 
  143.                  fAngle=(float)0, fAngleShift=(float)1, fMaxRadius, 
  144.                  fProportion;
  145.     static short  xClient, yClient;
  146.     HDC            hDC;
  147.     HRGN            hRGN;
  148.     PAINTSTRUCT   ps;
  149.    static WORD   OffViewX, OffViewY, OffWindowX, OffWindowY, SetViewX, 
  150.                  SetViewY, SetWindowX, SetWindowY, ScaleViewXnum,
  151.                  ScaleViewYnum, ScaleViewXdenom, ScaleViewYdenom,
  152.                  ScaleWindowXnum, ScaleWindowYnum, ScaleWindowXdenom,
  153.                  ScaleWindowYdenom, SetWindowOrgX, SetWindowOrgY,
  154.                  SetViewportOrgX, SetViewportOrgY;
  155.    DWORD         Check;
  156.  
  157.     switch (iMessage) {
  158.        case WM_CREATE:
  159.             hMenu = GetSystemMenu (hWnd, FALSE);
  160.      
  161.             ChangeMenu (hMenu, NULL, "&About", IDM_ABOUT, 
  162.                         MF_APPEND | MF_STRING);
  163.             break;
  164.      
  165.        case WM_SYSCOMMAND:
  166.             switch (wParam) {
  167.                case IDM_ABOUT:
  168.                     ProcessMessage (hWnd, 0);
  169.                     break;
  170.                default:
  171.                     return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  172.             }
  173.             break;
  174.  
  175.         case WM_SIZE:
  176.             xClient=LOWORD (lParam);
  177.             yClient=HIWORD (lParam);
  178.             fProportion= ((double) yClient)/ ((double) xClient);
  179.             fMaxRadius = xClient/2.0;
  180.             break;
  181.  
  182.  
  183.        case WM_COMMAND:
  184.             switch (wParam) {
  185.                case IDM_CHANGE:
  186.                     lpProcGetRadius = MakeProcInstance(GetRadiusDlgProc,
  187.                                      hInstMain);
  188.                     DialogBox (hInstMain, (LPSTR)"GetAngle", hWnd, 
  189.                               lpProcGetRadius);
  190.                     FreeProcInstance (lpProcGetRadius);
  191.                     fAngleShift = atof (szRadius);
  192.         
  193.                     lpProcGetRadius = MakeProcInstance(GetRadiusDlgProc,
  194.                                      hInstMain);
  195.                     DialogBox (hInstMain, (LPSTR)"GetRadius", hWnd, 
  196.                               lpProcGetRadius);
  197.                     FreeProcInstance (lpProcGetRadius);
  198.                     fRadiusInc = atof (szRadius);
  199.  
  200.                     InvalidateRect (hWnd, NULL, TRUE);
  201.                     UpdateWindow (hWnd);
  202.                     break;
  203.  
  204.                case IDM_OFFVIEWPORT:
  205.                     bOffsetViewportOrg = TRUE;
  206.                     sprintf (szXValue, "Enter Origin (x,y)");
  207.                     sprintf (szYValue, "OffsetViewportOrg");
  208.                     DialogBox (hInstMain, (LPSTR)"EnterPointDlg", hWnd, 
  209.                            lpProcEnterPoint);
  210.                     OffViewX = atoi (szXValue);
  211.                     OffViewY = atoi (szYValue);                      
  212.  
  213.                     InvalidateRect (hWnd, NULL, TRUE);
  214.                     UpdateWindow (hWnd);
  215.                     break;
  216.  
  217.                case IDM_OFFWINDOW:
  218.                     bOffsetWindowOrg = TRUE;
  219.                     sprintf (szXValue, "Enter Origin (x,y)");
  220.                     sprintf (szYValue, "OffsetWindowOrg");
  221.                     DialogBox (hInstMain, (LPSTR)"EnterPointDlg", hWnd, 
  222.                            lpProcEnterPoint);
  223.                     OffWindowX = atoi (szXValue);
  224.                     OffWindowY = atoi (szYValue);                      
  225.                                       
  226.                     InvalidateRect (hWnd, NULL, TRUE);
  227.                     UpdateWindow (hWnd);
  228.                     break;
  229.  
  230.                case IDM_SETVIEWPORT:
  231.                     bSetViewportExt = TRUE;
  232.                     sprintf (szXValue, "Enter Extents (x,y)");
  233.                     sprintf (szYValue, "SetViewportExt");
  234.                     DialogBox (hInstMain, (LPSTR)"EnterPointDlg", hWnd, 
  235.                            lpProcEnterPoint);
  236.                     SetViewX = atoi (szXValue);
  237.                     SetViewY = atoi (szYValue);                      
  238.                                       
  239.                     InvalidateRect (hWnd, NULL, TRUE);
  240.                     UpdateWindow (hWnd);
  241.                     break;
  242.  
  243.                case IDM_SETEXTENT:
  244.                     bSetWindowExt = TRUE;
  245.                     sprintf (szXValue, "Enter Extents (x,y)");
  246.                     sprintf (szYValue, "SetWindowExt");
  247.                     DialogBox (hInstMain, (LPSTR)"EnterPointDlg", hWnd, 
  248.                            lpProcEnterPoint);
  249.                     SetWindowX = atoi (szXValue);
  250.                     SetWindowY = atoi (szYValue);                      
  251.  
  252.                     InvalidateRect (hWnd, NULL, TRUE);
  253.                     UpdateWindow (hWnd);
  254.                     break;
  255.  
  256.                case IDM_SCALEVIEW:
  257.                     bScaleViewportExt = TRUE;
  258.                     sprintf (szXValue, "Enter Numerators (x,y)");
  259.                     sprintf (szYValue, "ScaleViewportExt");
  260.                     DialogBox (hInstMain, (LPSTR)"EnterPointDlg", hWnd, 
  261.                            lpProcEnterPoint);
  262.                     ScaleViewXnum = atoi (szXValue);
  263.                     ScaleViewYnum = atoi (szYValue);                      
  264.  
  265.                     sprintf (szXValue, "Enter Denominators (x,y)");
  266.                     sprintf (szYValue, "ScaleViewportExt");
  267.                     DialogBox (hInstMain, (LPSTR)"EnterPointDlg", hWnd, 
  268.                            lpProcEnterPoint);
  269.                     ScaleViewXdenom = atoi (szXValue);
  270.                     ScaleViewYdenom = atoi (szYValue);                      
  271.  
  272.                     InvalidateRect (hWnd, NULL, TRUE);
  273.                     UpdateWindow (hWnd);
  274.                     break;
  275.  
  276.                case IDM_SCALEWINDOW:
  277.                     bScaleWindowExt = TRUE;
  278.                     sprintf (szXValue, "Enter Numerators (x,y)");
  279.                     sprintf (szYValue, "ScaleWindowExt");
  280.                     DialogBox (hInstMain, (LPSTR)"EnterPointDlg", hWnd, 
  281.                            lpProcEnterPoint);
  282.                     ScaleWindowXnum = atoi (szXValue);
  283.                     ScaleWindowYnum = atoi (szYValue);                      
  284.  
  285.                     sprintf (szXValue, "Enter Denominators (x,y)");
  286.                     sprintf (szYValue, "ScaleWindowExt");
  287.                     DialogBox (hInstMain, (LPSTR)"EnterPointDlg", hWnd, 
  288.                            lpProcEnterPoint);
  289.                     ScaleWindowXdenom = atoi (szXValue);
  290.                     ScaleWindowYdenom = atoi (szYValue);                      
  291.  
  292.                     InvalidateRect (hWnd, NULL, TRUE);
  293.                     UpdateWindow (hWnd);
  294.                     break;
  295.  
  296.                case IDM_GETVIEWPORTEXT:
  297.                     hDC = GetDC (hWnd);
  298.                     SetMapMode (hDC, MM_ANISOTROPIC);
  299.                     if (bSetViewportExt)
  300.                         SetViewportExt    (hDC, SetViewX, SetViewY);
  301.                     Check = GetViewportExt (hDC);
  302.                     sprintf (szOutputBuffer1, "Viewport Extension = (%i,%i)",
  303.                             LOWORD (Check), HIWORD (Check));
  304.                     MessageBox (hWnd, szOutputBuffer1, "GetViewportExt", MB_OK);
  305.                     ReleaseDC (hWnd, hDC);
  306.                     break;
  307.  
  308.                case IDM_GETWINDOWEXT:
  309.                     hDC = GetDC (hWnd);
  310.                     SetMapMode (hDC, MM_ANISOTROPIC);
  311.                     if (bSetWindowExt)       
  312.                         SetWindowExt      (hDC, SetWindowX, SetWindowY); 
  313.                     Check = GetWindowExt (hDC);
  314.                     sprintf (szOutputBuffer1, "Window Extention = (%i,%i)",
  315.                             LOWORD (Check), HIWORD (Check));
  316.                     MessageBox (hWnd, szOutputBuffer1, "GetWindowExt", MB_OK);
  317.                     ReleaseDC (hWnd, hDC);
  318.                     break;
  319.  
  320.                case IDM_SETVIEWPORTORG:
  321.                     bSetViewportOrg = TRUE;
  322.                     sprintf (szXValue, "Enter Origin (x,y)");
  323.                     sprintf (szYValue, "SetViewportOrg");
  324.                     DialogBox (hInstMain, (LPSTR)"EnterPointDlg", hWnd, 
  325.                            lpProcEnterPoint);
  326.                     SetViewportOrgX = atoi (szXValue);
  327.                     SetViewportOrgY = atoi (szYValue);                      
  328.  
  329.                     InvalidateRect (hWnd, NULL, TRUE);
  330.                     UpdateWindow (hWnd);
  331.                     break;
  332.  
  333.                case IDM_SETWINDOWORG:
  334.                     bSetWindowOrg = TRUE;
  335.                     sprintf (szXValue, "Enter Origin (x,y)");
  336.                     sprintf (szYValue, "SetWindowOrg");
  337.                     DialogBox (hInstMain, (LPSTR)"EnterPointDlg", hWnd, 
  338.                            lpProcEnterPoint);
  339.                     SetWindowOrgX = atoi (szXValue);
  340.                     SetWindowOrgY = atoi (szYValue);                      
  341.  
  342.                     InvalidateRect (hWnd, NULL, TRUE);
  343.                     UpdateWindow (hWnd);
  344.                     break;
  345.  
  346.                case IDM_HELP:
  347.                     ProcessMessage (hWnd, 2);
  348.                     break;
  349.             }
  350.             break;
  351.  
  352.         case WM_PAINT:
  353.             hDC=BeginPaint(hWnd, &ps);
  354.             SetMapMode (hDC, MM_ANISOTROPIC);
  355.             SetViewportOrg (hDC, xClient/2, yClient/2);
  356.  
  357.             if (bOffsetViewportOrg)
  358.                   OffsetViewportOrg (hDC, OffViewX, OffViewY);
  359.             if (bOffsetWindowOrg)     
  360.                   OffsetWindowOrg   (hDC, OffWindowX, OffWindowY);
  361.             if (bSetViewportExt)
  362.                   SetViewportExt    (hDC, SetViewX, SetViewY);
  363.             if (bSetWindowExt)       
  364.                   SetWindowExt      (hDC, SetWindowX, SetWindowY); 
  365.             if (bScaleViewportExt)
  366.                   ScaleViewportExt  (hDC, ScaleViewXnum, ScaleViewXdenom,
  367.                                      ScaleViewYnum, ScaleViewYdenom);
  368.             if (bScaleWindowExt)
  369.                   ScaleWindowExt  (hDC, ScaleWindowXnum, ScaleWindowXdenom,
  370.                                      ScaleWindowYnum, ScaleWindowYdenom);
  371.             if (bSetViewportOrg) 
  372.                   SetViewportOrg (hDC, SetViewportOrgX, SetViewportOrgY);
  373.             if (bSetWindowOrg)
  374.                   SetWindowOrg (hDC, SetWindowOrgX, SetWindowOrgY);
  375.  
  376.             MoveTo (hDC, 0, 0); 
  377.             ShowCursor (TRUE);
  378.             for (fRadius=(float)0; fRadius<fMaxRadius; fRadius+=fRadiusInc) {
  379.                  xXcoord = fRadius * cos(fAngle);
  380.                  yYcoord = fRadius * sin(fAngle) * fProportion;
  381.                  LineTo (hDC, (short)xXcoord, (short)yYcoord);
  382.                  fAngle +=fAngleShift;
  383.             }
  384.             EndPaint (hWnd, &ps);
  385.             break;
  386.  
  387.        case WM_DESTROY:
  388.             PostQuitMessage (0);
  389.             break;
  390.  
  391.         default:
  392.             return DefWindowProc( hWnd, iMessage, wParam, lParam );
  393.        }
  394.     return 0L;
  395. }
  396.  
  397. /***************************************************************************/
  398.  
  399. BOOL FAR PASCAL GetRadiusDlgProc (hDlg, iMessage, wParam, lParam)
  400. HWND hDlg;
  401. unsigned iMessage;
  402. WORD wParam;
  403. LONG lParam;
  404. {
  405.      int Index;
  406.      char szChange [10];
  407.      long lReturn;
  408.  
  409.      switch (iMessage) {
  410.         case WM_INITDIALOG:
  411.              SendDlgItemMessage (hDlg, IDD_RADIUS, EM_LIMITTEXT,
  412.                                  (WORD)80, 0L);
  413.              SetDlgItemText (hDlg, IDD_RADIUS, "1.0");
  414.              return TRUE;
  415.              break;
  416.  
  417.         case WM_COMMAND:
  418.              switch (wParam)
  419.              {
  420.                case IDD_RADIUS:
  421.                     if (HIWORD (lParam) == EN_CHANGE)
  422.                         EnableWindow (GetDlgItem(hDlg,IDOK),
  423.                                      (BOOL)SendMessage(LOWORD (lParam),
  424.                                       WM_GETTEXTLENGTH, 0, 0L)) ;
  425.                     break;
  426.             
  427.                case IDOK:                 
  428.                     GetDlgItemText (hDlg, IDD_RADIUS, szRadius, 80) ;
  429.                     OemToAnsi (szRadius, szRadius);
  430.                     EndDialog (hDlg, TRUE);
  431.                     break;
  432.    
  433.                case IDCANCEL:
  434.                   EndDialog (hDlg, FALSE) ;
  435.                     break;
  436.          
  437.                default:
  438.                     return FALSE;
  439.              }
  440.         default:
  441.              return FALSE;
  442.      }
  443.      return TRUE;
  444. }
  445.  
  446. /****************************************************************************/
  447.  
  448. BOOL FAR PASCAL EnterPointDlgProc (hDlg, iMessage, wParam, lParam)
  449. HWND hDlg;
  450. unsigned iMessage;
  451. WORD wParam;
  452. LONG lParam;
  453. {
  454.      int Index;
  455.      char szChange [10];
  456.      long lReturn;
  457.  
  458.      switch (iMessage) {
  459.      case WM_INITDIALOG:
  460.        SendDlgItemMessage (hDlg, IDD_X, EM_LIMITTEXT,
  461.                            (WORD)40, 0L);
  462.        SendDlgItemMessage (hDlg, IDD_Y, EM_LIMITTEXT,
  463.                            (WORD)40, 0L);
  464.        SetDlgItemText (hDlg, IDD_TEXT1, szXValue);
  465.        SetDlgItemText (hDlg, IDD_TEXT2, szYValue);
  466.  
  467.        hX = GetDlgItem (hDlg, IDD_X);
  468.          lpProcOldX = (FARPROC) GetWindowLong (hX, GWL_WNDPROC);
  469.          SetWindowLong (hX, GWL_WNDPROC, (LONG) lpProcNewEnterPoint);
  470.          SendMessage (hX, EM_SETSEL, 0, MAKELONG (0,32767));
  471.        hY = GetDlgItem (hDlg, IDD_Y);
  472.          lpProcOldY = (FARPROC) GetWindowLong (hY, GWL_WNDPROC);
  473.          SetWindowLong (hY, GWL_WNDPROC, (LONG) lpProcNewEnterPoint);
  474.          SendMessage (hY, EM_SETSEL, 0, MAKELONG (0,32767));
  475.        hOK2 = GetDlgItem (hDlg, IDOK);
  476.      
  477.        return TRUE;
  478.        break;
  479.  
  480.      case WM_COMMAND:
  481.        switch (wParam) {
  482.          case IDD_X:
  483.               break;
  484.  
  485.          case IDD_Y:
  486.               break;
  487.          
  488.          case IDOK:            
  489.               GetDlgItemText (hDlg, IDD_X, szXValue, 10) ;
  490.               GetDlgItemText (hDlg, IDD_Y, szYValue, 10) ;
  491.               EndDialog (hDlg, TRUE);
  492.               break;
  493.  
  494.          default:
  495.               return FALSE;
  496.       }
  497.     default:
  498.       return FALSE;
  499.   }
  500.   return TRUE;
  501. }
  502.  
  503. /****************************************************************************/
  504.  
  505. BOOL FAR PASCAL NewEnterPointDlgProc  (hWnd, iMessage, wParam, lParam) 
  506.      HWND     hWnd;
  507.      unsigned iMessage; 
  508.      WORD     wParam;
  509.      LONG     lParam;
  510. {
  511.      switch (iMessage) {
  512.        case WM_GETDLGCODE:
  513.             return (DLGC_WANTALLKEYS);
  514.  
  515.        case WM_CHAR:
  516.             if ((wParam == VK_TAB) || (wParam == VK_RETURN)) {
  517.                 SendMessage (hWndMain, WM_USER, 0, 0L);
  518.                 SetFocus (FindNextWindow (hWnd));
  519.                 return TRUE;
  520.             }
  521.             else {
  522.               if (hWnd == hX) 
  523.                 return ((BOOL)CallWindowProc (lpProcOldX, hWnd, 
  524.                         iMessage, wParam, lParam));
  525.               if (hWnd == hY) 
  526.                 return ((BOOL)CallWindowProc (lpProcOldY, hWnd, 
  527.                         iMessage, wParam, lParam));
  528.             }
  529.             break;
  530.  
  531.        default:
  532.          if (hWnd == hX)
  533.             return ((BOOL)CallWindowProc (lpProcOldX, hWnd,
  534.                     iMessage, wParam, lParam));
  535.          if (hWnd == hY)
  536.             return ((BOOL)CallWindowProc (lpProcOldY, hWnd, 
  537.                     iMessage, wParam, lParam));
  538.      }
  539. }
  540.  
  541. /****************************************************************************/
  542.  
  543. HWND FindNextWindow (hWnd)
  544.      HWND   hWnd;
  545. {
  546.      if (hWnd == hX)      return hY;
  547.      if (hWnd == hY)      return hOK2;
  548.      return NULL;
  549. }
  550.  
  551.