home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / MSJV22.ZIP / COLORSCR.ALL next >
Text File  |  1987-10-30  |  9KB  |  279 lines

  1. Microsoft Systems Journal
  2. Volume 2; Issue 2; May, 1987
  3.  
  4. Code Listings For:
  5.  
  6.     COLORSCR
  7.     pp. 67-75
  8.  
  9. Author(s): Charles Petzold
  10. Title:     A Simple Windows Application for Custom Color Mixing
  11.  
  12.  
  13.  
  14. ==============================================================================
  15. Figure 6: COLORSCR.C
  16.  
  17. ==============================================================================
  18. ==============================================================================
  19.  
  20. /* COLORSCR.C -- Color Scroll (using child window controls) */
  21.  
  22. #include <windows.h>
  23. #include <stdlib.h>
  24.  
  25. long FAR PASCAL WndProc    (HWND, unsigned, WORD, LONG) ;
  26. long FAR PASCAL ScrollProc (HWND, unsigned, WORD, LONG) ;
  27.  
  28. FARPROC lpfnOldScr[3] ;
  29. HWND    hChScrol[3], hChLabel[3], hChValue[3], hChRect ;
  30. short   color[3], nFocus = 0;
  31.  
  32. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  33.      HANDLE   hInstance, hPrevInstance;
  34.      LPSTR    lpszCmdLine;
  35.      int      nCmdShow;
  36.      {
  37.      MSG      msg;
  38.      HWND     hWnd ;
  39.      WNDCLASS wndclass ;
  40.      FARPROC  lpfnScrollProc ;
  41.      short    n ;
  42.      static   char *szColorLabel[] = { "Red", "Green", "Blue" } ;
  43.      static   char szAppName[] = "ColorScr" ;-
  44.  
  45.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  46.      wndclass.lpfnWndProc   = WndProc ;
  47.      wndclass.cbClsExtra    = 0 ;
  48.      wndclass.cbWndExtra    = 0 ;
  49.      wndclass.hInstance     = hInstance ;
  50.      wndclass.hIcon         = NULL ;
  51.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  52.      wndclass.hbrBackground = CreateSolidBrush (0L) ;
  53.      wndclass.lpszMenuName  = NULL ;
  54.      wndclass.lpszClassName = szAppName ;
  55.  
  56.      if (!RegisterClass (&wndclass)) return FALSE;
  57.  
  58.      lpfnScrollProc = MakeProcInstance ((FARPROC) ScrollProc, 
  59.                                        hInstance) ;
  60.  
  61.      hWnd = CreateWindow (szAppName, " Color Scroll ",
  62.                     WS_TILEDWINDOW | WS_CLIPCHILDREN,
  63.                     0, 0, 0, 0, NULL, NULL, hInstance, NULL) ;
  64.  
  65.      hChRect = CreateWindow ("static", NULL,
  66.                     WS_CHILD | WS_VISIBLE | SS_WHITERECT,
  67.                     0, 0, 0, 0, hWnd, 9, hInstance, NULL) ;
  68.  
  69.      for (n = 0 ; n < 3 ; n++) 
  70.           {
  71.           hChScrol[n] = CreateWindow ("scrollbar", NULL,
  72.                         WS_CHILD | WS_VISIBLE | WS_TABSTOP | SBS_VERT,
  73.                         0, 0, 0, 0, hWnd, n, hInstance, NULL) ;
  74.  
  75.           hChLabel[n] = CreateWindow ("static", szColorLabel[n],
  76.                         WS_CHILD | WS_VISIBLE | SS_CENTER,
  77.                         0, 0, 0, 0, hWnd, n + 3, hInstance, NULL) ;
  78.  
  79.           hChValue[n] = CreateWindow ("static", "0",
  80.                         WS_CHILD | WS_VISIBLE | SS_CENTER,
  81.                         0, 0, 0, 0, hWnd, n + 6, hInstance, NULL) ; 
  82.  
  83.           lpfnOldScr[n] = (FARPROC) GetWindowLong (hChScrol[n], 
  84.                           GWL_WNDPROC) ;
  85.           SetWindowLong (hChScrol[n], GWL_WNDPROC, 
  86.                           (LONG) lpfnScrollProc) ;
  87.  
  88.           SetScrollRange (hChScrol[n], SB_CTL, 0, 255, FALSE) ;
  89.           SetScrollPos   (hChScrol[n], SB_CTL, 0, FALSE) ;
  90.           }
  91.  
  92.      ShowWindow (hWnd, nCmdShow) ;
  93.      UpdateWindow (hWnd);
  94.  
  95.      while (GetMessage (&msg, NULL, 0, 0))
  96.           {
  97.           TranslateMessage (&msg) ;
  98.           DispatchMessage  (&msg) ;
  99.           }
  100.      return msg.wParam ;
  101.      }
  102.  
  103. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  104.      HWND       hWnd;
  105.      unsigned   iMessage;
  106.      WORD       wParam;
  107.      LONG       lParam;
  108.      {
  109.      HDC        hDC ;
  110.      TEXTMETRIC tm ;
  111.      char       szbuffer[10] ;
  112.      short      n, xClient, yClient, yChar ;
  113.  
  114.      switch (iMessage)
  115.           {
  116.           case WM_SIZE :
  117.                xClient = LOWORD (lParam) ;
  118.                yClient = HIWORD (lParam) ;
  119.  
  120.                hDC = GetDC (hWnd) ;
  121.                GetTextMetrics (hDC, &tm) ;
  122.                yChar = tm.tmHeight ;
  123.                ReleaseDC (hWnd, hDC) ;
  124.  
  125.                MoveWindow (hChRect, 0, 0, xClient / 2, yClient, TRUE);
  126.  
  127.                for (n = 0 ; n < 3 ; n++)
  128.                     {
  129.                     MoveWindow (hChScrol[n],
  130.                          (2 * n + 1) * xClient / 14, 2 * yChar,
  131.                          xClient / 14, yClient - 4 * yChar, TRUE) ;
  132.  
  133.                     MoveWindow (hChLabel[n],
  134.                          (4 * n + 1) * xClient / 28, yChar / 2,
  135.                          xClient / 7, yChar, TRUE) ;
  136.  
  137.                     MoveWindow (hChValue[n],
  138.                          (4 * n + 1) * xClient / 28, 
  139.                          yClient - 3 * yChar / 2,
  140.                          xClient / 7, yChar, TRUE) ;
  141.                     }
  142.                SetFocus (hWnd) ;
  143.                break ;
  144.  
  145.           case WM_SETFOCUS:
  146.                SetFocus (hChScrol[nFocus]) ;
  147.                break ;
  148.  
  149.           case WM_VSCROLL :
  150.                n = GetWindowWord (HIWORD (lParam), GWW_ID) ;
  151.  
  152.                switch (wParam)
  153.                     {
  154.                     case SB_PAGEDOWN :
  155.                          color[n] += 15 ;         /* fall through */
  156.                     case SB_LINEDOWN :
  157.                          color[n] = min (255, color[n] + 1) ;
  158.                          break ;
  159.                     case SB_PAGEUP :
  160.                          color[n] ╤= 15 ;         /* fall through */
  161.                     case SB_LINEUP :
  162.                          color[n] = max (0, color[n] ╤ 1) ;
  163.                          break ;
  164.                     case SB_TOP:
  165.                          color[n] = 0 ;
  166.                          break ;
  167.                     case SB_BOTTOM :
  168.                          color[n] = 255 ;
  169.                          break ;
  170.                     case SB_THUMBPOSITION :
  171.                     case SB_THUMBTRACK :
  172.                          color[n] = LOWORD (lParam) ;
  173.                          break ;
  174.                     default :
  175.                          break ;
  176.                     }
  177.                SetScrollPos  (hChScrol[n], SB_CTL, color[n], TRUE) ;
  178.                SetWindowText (hChValue[n], 
  179.                              itoa (color[n], szbuffer, 10)) ;
  180.  
  181.                DeleteObject (GetClassWord (hWnd, GCW_HBRBACKGROUND)) ;
  182.                SetClassWord (hWnd, GCW_HBRBACKGROUND, CreateSolidBrush
  183.                             (RGB (color[0], color[1], color[2]))) ;
  184.  
  185.                InvalidateRect (hWnd, NULL, TRUE) ;
  186.                break ;
  187.  
  188.           case WM_DESTROY:
  189.                DeleteObject (GetClassWord (hWnd, GCW_HBRBACKGROUND)) ;
  190.                PostQuitMessage (0) ;
  191.                break ;
  192.  
  193.           default :
  194.                return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  195.           }
  196.      return 0L ;
  197.      }
  198.  
  199. long FAR PASCAL ScrollProc (hWnd, iMessage, wParam, lParam)
  200.      HWND      hWnd ;
  201.      unsigned  iMessage ;
  202.      WORD      wParam ;
  203.      LONG      lParam ;
  204.      {
  205.      short     n = GetWindowWord (hWnd, GWW_ID) ;
  206.  
  207.      switch (iMessage)
  208.           {
  209.           case WM_KEYDOWN:
  210.                if (wParam == VK_TAB)
  211.                     SetFocus (hChScrol[(n +
  212.                          (GetKeyState (VK_SHIFT) < 0 ? 2 : 1)) % 3]) ;
  213.                break ;
  214.  
  215.           case WM_SETFOCUS:
  216.                nFocus = n ;
  217.                break ;
  218.           }
  219.      return CallWindowProc (lpfnOldScr[n], hWnd, iMessage, wParam, 
  220.                            lParam) ;
  221.      }
  222. ==============================================================================
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239. ==============================================================================
  240. ==============================================================================
  241. Figure 7: COLORSCR.DEF
  242.  
  243. ==============================================================================
  244.  
  245. NAME            ColorScr
  246. STUB            'WINSTUB.EXE'
  247. CODE            MOVABLE
  248. DATA            MOVABLE MULTIPLE
  249. HEAPSIZE        1024
  250. STACKSIZA       4096
  251. EXPORTS         WndProc
  252.                 ScrollProc
  253. ==============================================================================
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267. ==============================================================================
  268. ==============================================================================
  269. Figure 8: Make-file for COLORSCR.EXE
  270.  
  271. ==============================================================================
  272.  
  273. colorscr.obj  :  colorscr.c
  274.     cl -c -d -Gsw -Os -W2 -Zpd colorscr.c
  275.  
  276. colorscr.exe  :  colorscr.obj colorscr.def
  277.     link4 colorscr, /align:16, /map /line, slibw, colorscr
  278.     mapsym colorscr
  279. ==============================================================================