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

  1. /*
  2.  
  3. Description:
  4.  
  5. This program first invokes the function GetRelAbs to find and display
  6. the current mode.  The mode is set to another value using the SetRelAbs
  7. function. GetRelAbs is called again to verify that the mode has indeed
  8. changed.  Below are descriptions for the demonstrated functions.
  9.  
  10. SetRelAbs sets the relative or absolute mode. This function specifies
  11. whether the coordinates in GDI functions are relative to the origin
  12. of the given device (ABSOLUTE), or relative to the current position
  13. (RELATIVE).  The mode affects the output created by the LineTo and
  14. Polyline functions.
  15.  
  16. GetRelAbs retrieves the relative-absolute flag. The return value
  17. specifies the current mode.  It can be ABSOLUTE or RELATIVE.
  18.  
  19. ****************************************************************************/
  20.  
  21. #include <windows.h>
  22. #include <string.h>
  23.  
  24. long FAR PASCAL WndProc (HWND, unsigned, WORD, LONG) ;
  25.  
  26. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  27.      HANDLE      hInstance, hPrevInstance ;
  28.      LPSTR       lpszCmdLine ;
  29.      int         nCmdShow ;
  30.      {
  31.      static char szAppName [] = "GetRelAbs" ;
  32.      HWND        hWnd ;
  33.      WNDCLASS    wndclass ;
  34.      MSG msg;
  35.  
  36.      if (!hPrevInstance) 
  37.           {
  38.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  39.           wndclass.lpfnWndProc   = WndProc ;
  40.           wndclass.cbClsExtra    = 0 ;
  41.           wndclass.cbWndExtra    = 0 ;
  42.           wndclass.hInstance     = hInstance ;
  43.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  44.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  45.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  46.           wndclass.lpszMenuName  = NULL ;
  47.           wndclass.lpszClassName = szAppName ;
  48.  
  49.           if (!RegisterClass (&wndclass))
  50.                return FALSE ;
  51.           }
  52.  
  53.      hWnd = CreateWindow (szAppName,            /* window class name       */
  54.                     "GetRelAbs",  /* window caption          */
  55.                     WS_OVERLAPPEDWINDOW,        /* window style            */
  56.                     CW_USEDEFAULT,              /* initial x position      */
  57.                     0,                          /* initial y position      */
  58.                     CW_USEDEFAULT,              /* initial x size          */
  59.                     0,                          /* initial y size          */
  60.                     NULL,                       /* parent window handle    */
  61.                     NULL,                       /* window menu handle      */
  62.                     hInstance,                  /* program instance handle */
  63.                     NULL) ;                     /* create parameters       */
  64.  
  65.      ShowWindow (hWnd, nCmdShow) ;
  66.  
  67.      UpdateWindow (hWnd) ;
  68.  
  69.      while (GetMessage(&msg, NULL, 0, 0))
  70.      {
  71.       TranslateMessage(&msg);
  72.       DispatchMessage(&msg);
  73.      } 
  74.      return (msg.wParam) ;     
  75.      }
  76.  
  77. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  78. HWND     hWnd ;
  79. unsigned iMessage ;
  80. WORD     wParam ;
  81. LONG     lParam ;
  82. {
  83.  PAINTSTRUCT     ps;
  84.  short           fRelAbsFlag;
  85.  HDC             hDC;
  86.  TEXTMETRIC      tm;
  87.  static short    xChar,
  88.                  yChar;
  89.  
  90.  switch(iMessage)
  91.  {
  92.   case WM_CREATE:
  93.   {
  94.    hDC = GetDC (hWnd);
  95.    GetTextMetrics (hDC, &tm);
  96.    xChar = tm.tmAveCharWidth;
  97.    yChar = tm.tmHeight + tm.tmExternalLeading;
  98.    ReleaseDC (hWnd, hDC);
  99.    break;
  100.   }
  101.   case WM_PAINT:
  102.   {
  103.    hDC = BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  104.  
  105.    fRelAbsFlag = GetRelAbs (hDC);                        /* Check the mode */
  106.  
  107.    if (fRelAbsFlag == RELATIVE)
  108.       {
  109.       TextOut (hDC,
  110.                xChar,
  111.                yChar,
  112.                (LPSTR)"1. Function GetRelAbs returns RELATIVE, so the mode is currently RELATIVE.",
  113.                strlen("1. Function GetRelAbs returns RELATIVE, so the mode is currently RELATIVE.")
  114.               );
  115.  
  116.       SetRelAbs (hDC, ABSOLUTE);
  117.       }
  118.    else /* fRelAbsFlag ==  ABSOLUTE */         /* Set the mode to ABSOLUTE */
  119.       {
  120.       TextOut (hDC,
  121.                xChar,
  122.                yChar,
  123.                (LPSTR)"1. Function GetRelAbs returns ABSOLUTE, so the mode is currently ABSOLUTE.",
  124.                strlen("1. Function GetRelAbs returns ABSOLUTE, so the mode is currently ABSOLUTE.")
  125.               );
  126.  
  127.       SetRelAbs (hDC, RELATIVE);               /* Set the mode to RELATIVE */
  128.       }
  129.  
  130.    fRelAbsFlag = GetRelAbs (hDC);                        /* Check the mode */
  131.  
  132.    if (fRelAbsFlag == RELATIVE)
  133.       {
  134.       TextOut (hDC,
  135.                xChar,
  136.                yChar * 2,
  137.                (LPSTR)"2. Function SetRelAbs sets mode to RELATIVE.  Function GetRelAbs returns RELATIVE.",
  138.                strlen("2. Function SetRelAbs sets mode to RELATIVE.  Function GetRelAbs returns RELATIVE.")
  139.               );
  140.       }
  141.    else /* fRelAbsFlag ==  ABSOLUTE */
  142.       {
  143.       TextOut (hDC,
  144.                xChar,
  145.                yChar * 2,
  146.                (LPSTR)"2. Function SetRelAbs sets mode to ABSOLUTE.  Function GetRelAbs returns ABSOLUTE.",
  147.                strlen("2. Function SetRelAbs sets mode to ABSOLUTE.  Function GetRelAbs returns ABSOLUTE.")
  148.               );
  149.       }
  150.  
  151.  
  152.    EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  153.    break;
  154.   }
  155.   case WM_DESTROY:
  156.   {
  157.    PostQuitMessage(0);
  158.    break;
  159.   }
  160.   default:
  161.   {
  162.    return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  163.   }
  164.  }
  165.  return (0L); 
  166. }
  167.