home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk11 / petzold / chap11 / colorscr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-20  |  9.7 KB  |  244 lines

  1. /*--------------------------------------------------------
  2.    COLORSCR.C -- Color Scroll using child window controls
  3.   --------------------------------------------------------*/
  4.  
  5. #define INCL_WIN
  6. #define INCL_GPI
  7. #include <os2.h>
  8. #include <stdlib.h>
  9.  
  10. MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
  11. MRESULT EXPENTRY ScrollProc (HWND, USHORT, MPARAM, MPARAM) ;
  12.  
  13. HWND  hwndScroll[3], hwndFocus ;
  14. PFNWP pfnOldScroll[3] ;
  15.  
  16. int main (void)
  17.      {
  18.      static CHAR  szClientClass[] = "ColorScr" ;
  19.      static ULONG flFrameFlags = FCF_TITLEBAR      | FCF_SYSMENU |
  20.                                  FCF_SIZEBORDER    | FCF_MINMAX  |
  21.                                  FCF_SHELLPOSITION | FCF_TASKLIST ;
  22.      HAB          hab ;
  23.      HMQ          hmq ;
  24.      HWND         hwndFrame, hwndClient ;
  25.      QMSG         qmsg ;
  26.  
  27.      hab = WinInitialize (0) ;
  28.      hmq = WinCreateMsgQueue (hab, 0) ;
  29.  
  30.      WinRegisterClass (hab, szClientClass, ClientWndProc, CS_SIZEREDRAW, 0) ;
  31.  
  32.      hwndFrame = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE,
  33.                                      &flFrameFlags, szClientClass, NULL,
  34.                                      0L, NULL, 0, &hwndClient) ;
  35.  
  36.      WinSetFocus (HWND_DESKTOP, hwndFocus = hwndScroll[0]) ;
  37.  
  38.      WinSendMsg (hwndFrame, WM_SETICON,
  39.                  WinQuerySysPointer (HWND_DESKTOP, SPTR_APPICON, FALSE),
  40.                  NULL) ;
  41.  
  42.      while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  43.           WinDispatchMsg (hab, &qmsg) ;
  44.  
  45.      WinDestroyWindow (hwndFrame) ;
  46.      WinDestroyMsgQueue (hmq) ;
  47.      WinTerminate (hab) ;
  48.      return 0 ;
  49.      }
  50.  
  51. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  52.      {
  53.      static CHAR  *szColorLabel[] = { "Red", "Green", "Blue" } ;
  54.      static HWND  hwndLabel[3], hwndValue[3] ;
  55.      static SHORT cyChar, sColor[3] ;
  56.      static RECTL rclRightHalf ;
  57.      CHAR         szBuffer[10] ;
  58.      FONTMETRICS  fm ;
  59.      HPS          hps ;
  60.      SHORT        s, id, cxClient, cyClient ;
  61.  
  62.      switch (msg)
  63.           {
  64.           case WM_CREATE :
  65.                hps = WinGetPS (hwnd) ;
  66.                GpiQueryFontMetrics (hps, (LONG) sizeof fm, &fm) ;
  67.                cyChar = (SHORT) fm.lMaxBaselineExt ;
  68.                WinReleasePS (hps) ;
  69.  
  70.                for (s = 0 ; s < 3 ; s++)
  71.                     {
  72.                     hwndScroll[s] = WinCreateWindow (
  73.                                         hwnd,               // Parent
  74.                                         WC_SCROLLBAR,       // Class
  75.                                         NULL,               // Text
  76.                                         WS_VISIBLE |        // Style
  77.                                              SBS_VERT,
  78.                                         0, 0,               // Position
  79.                                         0, 0,               // Size
  80.                                         hwnd,               // Owner
  81.                                         HWND_BOTTOM,        // Placement
  82.                                         s,                  // ID
  83.                                         NULL,               // Ctrl Data
  84.                                         NULL) ;             // Pres Params
  85.  
  86.                     hwndLabel[s]  = WinCreateWindow (
  87.                                         hwnd,               // Parent
  88.                                         WC_STATIC,          // Class
  89.                                         szColorLabel[s],    // Text
  90.                                         WS_VISIBLE |        // Style
  91.                                           SS_TEXT | DT_CENTER,
  92.                                         0, 0,               // Position
  93.                                         0, 0,               // Size
  94.                                         hwnd,               // Owner
  95.                                         HWND_BOTTOM,        // Placement
  96.                                         s + 3,              // ID
  97.                                         NULL,               // Ctrl Data
  98.                                         NULL) ;             // Pres Params
  99.  
  100.                     hwndValue[s]  = WinCreateWindow (
  101.                                         hwnd,               // Parent
  102.                                         WC_STATIC,          // Class
  103.                                         "0",                // Text
  104.                                         WS_VISIBLE |        // Style
  105.                                           SS_TEXT | DT_CENTER,
  106.                                         0, 0,               // Position
  107.                                         0, 0,               // Size
  108.                                         hwnd,               // Owner
  109.                                         HWND_BOTTOM,        // Placement
  110.                                         s + 6,              // ID
  111.                                         NULL,               // Ctrl Data
  112.                                         NULL) ;             // Pres Params
  113.  
  114.                     pfnOldScroll[s] =
  115.                               WinSubclassWindow (hwndScroll[s], ScrollProc) ;
  116.  
  117.                     WinSendMsg (hwndScroll[s], SBM_SETSCROLLBAR,
  118.                 MPFROM2SHORT (0, 0), MPFROM2SHORT (0, 255)) ;
  119.                     }
  120.                return 0 ;
  121.  
  122.           case WM_SIZE :
  123.                cxClient = SHORT1FROMMP (mp2) ;
  124.                cyClient = SHORT2FROMMP (mp2) ;
  125.  
  126.                for (s = 0 ; s < 3 ; s++)
  127.                     {
  128.                     WinSetWindowPos (hwndScroll[s], NULL,
  129.                                      (2 * s + 1) * cxClient / 14, 2 * cyChar,
  130.                                      cxClient / 14, cyClient - 4 * cyChar,
  131.                                      SWP_SIZE | SWP_MOVE) ;
  132.  
  133.                     WinSetWindowPos (hwndLabel[s], NULL,
  134.                                      (4 * s + 1) * cxClient / 28,
  135.                                      cyClient - 3 * cyChar / 2,
  136.                                      cxClient / 7, cyChar,
  137.                                      SWP_SIZE | SWP_MOVE) ;
  138.  
  139.                     WinSetWindowPos (hwndValue[s], NULL,
  140.                                      (4 * s + 1) * cxClient / 28, cyChar / 2,
  141.                                      cxClient / 7, cyChar,
  142.                                      SWP_SIZE | SWP_MOVE) ;
  143.                     }
  144.  
  145.                WinQueryWindowRect (hwnd, &rclRightHalf) ;
  146.                rclRightHalf.xLeft = rclRightHalf.xRight / 2 ;
  147.                return 0 ;
  148.  
  149.           case WM_VSCROLL :
  150.                id = SHORT1FROMMP (mp1) ;          // ID of scroll bar
  151.  
  152.                switch (SHORT2FROMMP (mp2))
  153.                     {
  154.                     case SB_LINEDOWN :
  155.                          sColor[id] = min (255, sColor[id] + 1) ;
  156.                          break ;
  157.  
  158.                     case SB_LINEUP :
  159.                          sColor[id] = max (0, sColor[id] - 1) ;
  160.                          break ;
  161.  
  162.                     case SB_PAGEDOWN :
  163.                          sColor[id] = min (255, sColor[id] + 16) ;
  164.                          break ;
  165.  
  166.                     case SB_PAGEUP :
  167.                          sColor[id] = max (0, sColor[id] - 16) ;
  168.                          break ;
  169.  
  170.                     case SB_SLIDERTRACK :
  171.                          sColor[id] = SHORT1FROMMP (mp2) ;
  172.                          break ;
  173.  
  174.                     default :
  175.                          return 0 ;
  176.                     }
  177.                WinSendMsg (hwndScroll[id], SBM_SETPOS,
  178.                            MPFROM2SHORT (sColor[id], 0), NULL) ;
  179.  
  180.                WinSetWindowText (hwndValue[id],
  181.                                  itoa (sColor[id], szBuffer, 10)) ;
  182.                WinInvalidateRect (hwnd, &rclRightHalf, FALSE) ;
  183.                return 0 ;
  184.  
  185.           case WM_PAINT:
  186.                hps = WinBeginPaint (hwnd, NULL, NULL) ;
  187.  
  188.                GpiCreateLogColorTable (hps, LCOL_RESET, LCOLF_RGB,
  189.                                             0L, 0L, NULL) ;
  190.  
  191.                WinFillRect (hps, &rclRightHalf, (ULONG) sColor[0] << 16 |
  192.                                                 (ULONG) sColor[1] <<  8 |
  193.                                                 (ULONG) sColor[2]) ;
  194.                WinEndPaint (hps) ;
  195.                return 0 ;
  196.  
  197.           case WM_ERASEBACKGROUND:
  198.                return 1 ;
  199.           }
  200.      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  201.      }
  202.  
  203. MRESULT EXPENTRY ScrollProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  204.      {
  205.      USHORT id ;
  206.  
  207.      id = WinQueryWindowUShort (hwnd, QWS_ID) ;   // ID of scroll bar
  208.  
  209.      switch (msg)
  210.           {
  211.           case WM_CHAR:
  212.                if (!(CHARMSG(&msg)->fs & KC_VIRTUALKEY))
  213.                     break ;
  214.  
  215.                switch (CHARMSG(&msg)->vkey)
  216.                     {
  217.                     case VK_TAB:
  218.                          if (!(CHARMSG(&msg)->fs & KC_KEYUP))
  219.                               {
  220.                               hwndFocus = hwndScroll[(id + 1) % 3] ;
  221.                               WinSetFocus (HWND_DESKTOP, hwndFocus) ;
  222.                               }
  223.                          return 1 ;
  224.  
  225.                     case VK_BACKTAB:
  226.                          if (!(CHARMSG(&msg)->fs & KC_KEYUP))
  227.                               {
  228.                               hwndFocus = hwndScroll[(id + 2) % 3] ;
  229.                               WinSetFocus (HWND_DESKTOP, hwndFocus) ;
  230.                               }
  231.                          return 1 ;
  232.  
  233.                     default:
  234.                          break ;
  235.                     }
  236.                break ;
  237.  
  238.           case WM_BUTTON1DOWN:
  239.                WinSetFocus (HWND_DESKTOP, hwndFocus = hwnd) ;
  240.                break ;
  241.           }
  242.      return pfnOldScroll[id] (hwnd, msg, mp1, mp2) ;
  243.      }
  244.