home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / w3_syst / setup.arj / GASGAUGE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-25  |  12.2 KB  |  310 lines

  1. /**********************************************************************
  2. *                                                                     *
  3. * WINDOWS Gas-Gauge Control                                           *
  4. *                                                                     *
  5. * LANGUAGE : Microsoft C 6.0                                          *
  6. * TOOLKIT  : Windows 3.00 SDK                                         *
  7. * MODEL    : Small or Medium                                          *
  8. *                                                                     *
  9. * 03/20/88 - Kevin       - initial creation of SPECTRUM Model         *
  10. *                             from which this was made                *
  11. *                                                                     *
  12. * 08/09/89 - Dave       - Used base code to make this gauge             *
  13. *                                                                      *
  14. * 12/01/89 - Tim       - Added text percentage to gauge                *
  15. *                                                                     *
  16. **********************************************************************/
  17.  
  18. #include <windows.h>
  19. #include <stdio.h>
  20. #include "gasgauge.h"          // Messages
  21.  
  22. /* general GasGauge definitions */
  23.  
  24. #define   ID                GetWindowWord( hWnd, GWW_ID )
  25. #define   PARENT            GetWindowWord( hWnd, GWW_HWNDPARENT )
  26. #define   INSTANCE          GetWindowWord( hWnd, GWW_HINSTANCE )
  27.  
  28. /* GasGauge specific definitions */
  29.  
  30. #define   GASGAUGE_EXTRA         16
  31.  
  32. #define   RANGE             GetWindowWord( hWnd, 0 )
  33. #define   VALUE             GetWindowWord( hWnd, 2 )
  34. #define   WIDTH             GetWindowWord( hWnd, 4 )
  35. #define   HEIGHT            GetWindowWord( hWnd, 6 )
  36. #define   BACKGROUND        GetWindowLong( hWnd, 8 )
  37. #define   FOREGROUND        GetWindowLong( hWnd, 12 )
  38.  
  39. #define   SET_RANGE(x)      SetWindowWord( hWnd, 0, x )
  40. #define   SET_VALUE(x)      SetWindowWord( hWnd, 2, x )
  41. #define   SET_WIDTH(x)      SetWindowWord( hWnd, 4, x )
  42. #define   SET_HEIGHT(x)     SetWindowWord( hWnd, 6, x )
  43. #define   SET_BACKGROUND(x) SetWindowLong( hWnd, 8, x )
  44. #define   SET_FOREGROUND(x) SetWindowLong( hWnd, 12, x )
  45.  
  46. /****************************************************************************
  47. *                                                                           *
  48. * RegisterGasGauge( hAppInstance ) : BOOL                                   *
  49. *                                                                           *
  50. *      hAppInstance      application instance handle         *
  51. *                                                                           *
  52. * This function is responsible for the definition and registration          *
  53. * of the GasGauge window class.  Note that this function should             *
  54. * only be called ONCE (typically during the initialization phase            *
  55. * of the host application) within a program.                                *
  56. *                                                                           *
  57. * A value of TRUE is returned if the registration operation was             *
  58. * performed sucessfully.                                                    *
  59. *                                                                           *
  60. ****************************************************************************/
  61.  
  62. BOOL FAR PASCAL RegisterGasGauge( hAppInstance )
  63. HANDLE      hAppInstance;
  64. {
  65.    /* local variables */
  66.    WNDCLASS   WndClass;   /* window class data structure */
  67.  
  68.    /* Define GasGauge window class.  Note that no check is made to
  69.     * see if it has been previously registed!  It is up to the host
  70.     * application to prevent multiple registrations.
  71.     */
  72.     
  73.    WndClass.lpszClassName = (LPSTR)"GasGauge";
  74.    WndClass.hCursor       = LoadCursor( NULL, IDC_ARROW );
  75.    WndClass.lpszMenuName  = (LPSTR)NULL;
  76.    WndClass.style         = CS_HREDRAW|CS_VREDRAW;
  77.    WndClass.lpfnWndProc   = GasGaugeWndFn;
  78.    WndClass.hInstance     = hAppInstance;
  79.    WndClass.hIcon         = NULL;
  80.    WndClass.cbWndExtra    = GASGAUGE_EXTRA;
  81.    WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1 );
  82.    
  83.    /* register GasGauge window class & return */
  84.    return( RegisterClass( (LPWNDCLASS)&WndClass ) );
  85.  
  86. }
  87.  
  88.  
  89. /*******************************************************************
  90. *                                                                  *
  91. * GasGaugeWndFn( hWnd, wMsg, wParam, lParam ) : LONG               *
  92. *                                                                  *
  93. *   hWnd      handle to GasGauge window                            *
  94. *   wMsg      message number                                       *
  95. *   wParam      single word parameter                              *
  96. *   lParam      double word parameter                              *
  97. *                                                                  *
  98. * This function is responsible for processing all the messages     *
  99. * which relate to the GasGauge control window.  Note how the       *
  100. * code is written to avoid potential problems when re-entrancy     *
  101. * happens - this involves the use of extra bytes associated        *
  102. * with the window data structure.                                  *
  103. *                                                                  *
  104. * The LONG value returned by this function is the conventional     *
  105. * result of the default window procedure or the internal           *
  106. * handling of a message.                                           *
  107. *                                                                  *
  108. *******************************************************************/
  109.  
  110. LONG FAR PASCAL GasGaugeWndFn( hWnd, wMsg, wParam, lParam )
  111. HWND   hWnd;
  112. WORD   wMsg;
  113. WORD   wParam;
  114. LONG   lParam;
  115. {
  116.    /* local variables */
  117.    LONG       lResult;  /* temporary result variable */
  118.    
  119.    /* initialization */
  120.    lResult = TRUE;
  121.    
  122.    /* process message */
  123.    switch( wMsg )
  124.      {
  125.      case WM_GETDLGCODE : /* capture clicks on scroll bar arrows */
  126.  
  127.          lParam = DLGC_WANTARROWS;      
  128.          break;
  129.  
  130.      case WM_CREATE : 
  131.  
  132.          SET_RANGE( 100 );
  133.          SET_VALUE( 0   );
  134.          SET_WIDTH( ((LPCREATESTRUCT)lParam)->cx  );
  135.          SET_HEIGHT( ((LPCREATESTRUCT)lParam)->cy );
  136.          SET_BACKGROUND( RGB (255, 255, 255 ));    // white
  137.          SET_FOREGROUND( RGB (255, 0, 0 ));      // red
  138.          break;
  139.  
  140.      case WM_SIZE : /* window being resized */
  141.    
  142.       /* redefine width & height instance variables */
  143.  
  144.          SET_WIDTH  ( LOWORD(lParam) );
  145.          SET_HEIGHT ( HIWORD(lParam) );
  146.  
  147.          break;
  148.  
  149.      case WM_COMMAND:
  150.  
  151.          switch ( wParam )
  152.             {
  153.             case GG_SETFOREGROUND:
  154.  
  155.                 SET_FOREGROUND ( lParam );
  156.                 InvalidateRect ( hWnd, NULL, FALSE );
  157.                 break;
  158.  
  159.             case GG_SETBACKGROUND:
  160.  
  161.                 SET_BACKGROUND ( lParam );
  162.                 InvalidateRect ( hWnd, NULL, FALSE );
  163.                 break;
  164.  
  165.             case GG_SETRANGE:
  166.  
  167.                 SET_RANGE      ( (WORD)lParam );
  168.                 if ( VALUE > (WORD)lParam )
  169.                    SET_VALUE ( (WORD)lParam );
  170.                 InvalidateRect ( hWnd, NULL, FALSE );
  171.                 break;
  172.  
  173.             case GG_SETVALUE:
  174.  
  175.                 if ((WORD)lParam <= RANGE )
  176.                    SET_VALUE ( (WORD)lParam );
  177.                 else
  178.                    SET_VALUE ( RANGE );
  179.                 InvalidateRect ( hWnd, NULL, FALSE );
  180.                 break;                
  181.             
  182.             default:
  183.  
  184.                 break;
  185.             }
  186.          break;
  187.  
  188.  
  189.      /* Paint rectangles and % done within control window.
  190.      ** Done indicated by red rectangle, not done is white.
  191.      **
  192.      ** Percent complete is written to bitmap, and then Bitblt'd onto
  193.      ** control. For TextOut to bitmap's DC, use black background
  194.      ** and NOT red/done text so that later Bitblt can XOR
  195.      ** source and dest to get white text on red, or red on white.
  196.      */
  197.      case WM_PAINT:
  198.  
  199.         {
  200.         /* Many of the variable declarations, assignments, and
  201.         ** code below don't need to be done for each paint message
  202.         ** and doing so slows the gauge down; they are here to
  203.         ** make this example easier to follow.
  204.         */
  205.         HDC         hDC,                           // handle to display
  206.                     hdcSrc;                        // handle to % bitmap's DC
  207.         HBITMAP     hbmSrc;                        // handle to % bitmap
  208.         PAINTSTRUCT Ps;                            // paint structure
  209.         HBRUSH      hOldBrush, hForeGround, 
  210.                     hBackGround;
  211.         HPEN        hOldPen, hPen;
  212.         RECT        rc;                            // red or white rect
  213.         WORD        x1, x2, x3, y1, y2,            // logical coordinates
  214.                     xText, xTextWidth, yText, 
  215.                     yTextHeight;                   // % text
  216.         char        chBuffer[6];                   // for % string
  217.         DWORD       dwTextExtent,                  // string text width
  218.                     dwOldTextColor, dwOldBkColor;
  219.  
  220.         x1 = 0;          // 0=y1+----------+-------------+
  221.         x3 = WIDTH;      //     | On = Red | Off = White |
  222.         y1 = 0;          //cy=y2+----------+-------------+
  223.         y2 = HEIGHT;     //     x1=0        x2    cx=x3
  224.  
  225.         x2 = (int) ((float)(((float)VALUE / (float)RANGE)) * x3);
  226.  
  227.         if ( x2 > x3 )
  228.            x2 = x3;
  229.                
  230.         hDC = BeginPaint( hWnd, (LPPAINTSTRUCT)&Ps );
  231.  
  232.         hForeGround = CreateSolidBrush ( FOREGROUND );
  233.         hBackGround = CreateSolidBrush ( BACKGROUND );
  234.  
  235.         hOldBrush = SelectObject ( hDC, hForeGround );
  236.  
  237.         // red "amount done" rectangle
  238.         hPen = CreatePen( 0, 1, RGB( 255, 0, 0 ) );
  239.         hOldPen = SelectObject( hDC, hPen );
  240.         Rectangle ( hDC, x1, y1, x2, y2 );
  241.         SelectObject( hDC, hOldPen );
  242.         DeleteObject( hPen );
  243.  
  244.         // white "not done" rectangle
  245.         if ( x2 < x3 )
  246.           {
  247.           SelectObject ( hDC, hBackGround );
  248.           hPen = CreatePen( 0, 1, RGB( 255, 255, 255 ) );
  249.           hOldPen = SelectObject( hDC, hPen );
  250.           Rectangle ( hDC, x2, y1, x3, y2 );
  251.           SelectObject( hDC, hOldPen );
  252.           DeleteObject( hPen );
  253.           }
  254.  
  255.         /* Now write percentage Red/done in middle of gauge */
  256.  
  257.         // Determine percentage Red & center text's location in gauge
  258.         if ( x1 >= x3 )
  259.           sprintf( chBuffer, "100%%" );
  260.         else
  261.           sprintf( chBuffer, "%d%%",
  262.                    (int)( 100 * (x2 - x1) / (x3 - x1) ) );
  263.         dwTextExtent = GetTextExtent( hDC, (LPSTR)chBuffer,
  264.                        lstrlen((LPSTR)chBuffer) );
  265.         xTextWidth  = LOWORD( dwTextExtent );
  266.         yTextHeight = HIWORD( dwTextExtent );
  267.         xText = (x3 - x1 - xTextWidth) >> 1;
  268.         yText = y1 + ( ((y2 - y1) - yTextHeight) >> 1 );
  269.   
  270.         // Create DC & bitmap, write text, and blt to display
  271.         hdcSrc = CreateCompatibleDC( hDC );
  272.         hbmSrc = CreateCompatibleBitmap( hDC, xTextWidth, yTextHeight);
  273.         SelectObject( hdcSrc, hbmSrc );
  274.   
  275.         // text and background color all important for ROP code
  276.         dwOldBkColor = SetBkColor( hdcSrc, RGB( 0, 0, 0 ) );     // black
  277.         dwOldTextColor = SetTextColor( hdcSrc, RGB(0,255,255) ); // ~red
  278.         TextOut( hdcSrc, 0, 0, (LPSTR)chBuffer,
  279.                  lstrlen( (LPSTR)chBuffer ) );
  280.         BitBlt( hDC, xText, yText, xTextWidth, yTextHeight,
  281.                 hdcSrc, 0, 0, SRCINVERT ); //
  282.   
  283.         // clean up
  284.         SetBkColor( hdcSrc, dwOldBkColor );
  285.         SetTextColor( hdcSrc, dwOldTextColor );
  286.         DeleteObject( hdcSrc );
  287.         DeleteObject( hbmSrc );    // fixes RAID bug 119
  288.         ReleaseDC( hWnd, hdcSrc );
  289.   
  290.         SelectObject ( hDC, hOldBrush );
  291.         DeleteObject ( hForeGround );
  292.         DeleteObject ( hBackGround );
  293.   
  294.         EndPaint( hWnd, (LPPAINTSTRUCT)&Ps );
  295.         }
  296.       break;
  297.  
  298.    default : /* default window message processing */
  299.  
  300.       lResult = DefWindowProc( hWnd, wMsg, wParam, lParam );
  301.       break;
  302.    }
  303.    
  304.    /* return final result */
  305.    return( lResult );
  306.  
  307. }
  308.  
  309. 
  310.