home *** CD-ROM | disk | FTP | other *** search
- /**********************************************************************
- * *
- * WINDOWS Gas-Gauge Control *
- * *
- * LANGUAGE : Microsoft C 6.0 *
- * TOOLKIT : Windows 3.00 SDK *
- * MODEL : Small or Medium *
- * *
- * 03/20/88 - Kevin - initial creation of SPECTRUM Model *
- * from which this was made *
- * *
- * 08/09/89 - Dave - Used base code to make this gauge *
- * *
- * 12/01/89 - Tim - Added text percentage to gauge *
- * *
- **********************************************************************/
-
- #include <windows.h>
- #include <stdio.h>
- #include "gasgauge.h" // Messages
-
- /* general GasGauge definitions */
-
- #define ID GetWindowWord( hWnd, GWW_ID )
- #define PARENT GetWindowWord( hWnd, GWW_HWNDPARENT )
- #define INSTANCE GetWindowWord( hWnd, GWW_HINSTANCE )
-
- /* GasGauge specific definitions */
-
- #define GASGAUGE_EXTRA 16
-
- #define RANGE GetWindowWord( hWnd, 0 )
- #define VALUE GetWindowWord( hWnd, 2 )
- #define WIDTH GetWindowWord( hWnd, 4 )
- #define HEIGHT GetWindowWord( hWnd, 6 )
- #define BACKGROUND GetWindowLong( hWnd, 8 )
- #define FOREGROUND GetWindowLong( hWnd, 12 )
-
- #define SET_RANGE(x) SetWindowWord( hWnd, 0, x )
- #define SET_VALUE(x) SetWindowWord( hWnd, 2, x )
- #define SET_WIDTH(x) SetWindowWord( hWnd, 4, x )
- #define SET_HEIGHT(x) SetWindowWord( hWnd, 6, x )
- #define SET_BACKGROUND(x) SetWindowLong( hWnd, 8, x )
- #define SET_FOREGROUND(x) SetWindowLong( hWnd, 12, x )
-
- /****************************************************************************
- * *
- * RegisterGasGauge( hAppInstance ) : BOOL *
- * *
- * hAppInstance application instance handle *
- * *
- * This function is responsible for the definition and registration *
- * of the GasGauge window class. Note that this function should *
- * only be called ONCE (typically during the initialization phase *
- * of the host application) within a program. *
- * *
- * A value of TRUE is returned if the registration operation was *
- * performed sucessfully. *
- * *
- ****************************************************************************/
-
- BOOL FAR PASCAL RegisterGasGauge( hAppInstance )
- HANDLE hAppInstance;
- {
- /* local variables */
- WNDCLASS WndClass; /* window class data structure */
-
- /* Define GasGauge window class. Note that no check is made to
- * see if it has been previously registed! It is up to the host
- * application to prevent multiple registrations.
- */
-
- WndClass.lpszClassName = (LPSTR)"GasGauge";
- WndClass.hCursor = LoadCursor( NULL, IDC_ARROW );
- WndClass.lpszMenuName = (LPSTR)NULL;
- WndClass.style = CS_HREDRAW|CS_VREDRAW;
- WndClass.lpfnWndProc = GasGaugeWndFn;
- WndClass.hInstance = hAppInstance;
- WndClass.hIcon = NULL;
- WndClass.cbWndExtra = GASGAUGE_EXTRA;
- WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1 );
-
- /* register GasGauge window class & return */
- return( RegisterClass( (LPWNDCLASS)&WndClass ) );
-
- }
-
-
- /*******************************************************************
- * *
- * GasGaugeWndFn( hWnd, wMsg, wParam, lParam ) : LONG *
- * *
- * hWnd handle to GasGauge window *
- * wMsg message number *
- * wParam single word parameter *
- * lParam double word parameter *
- * *
- * This function is responsible for processing all the messages *
- * which relate to the GasGauge control window. Note how the *
- * code is written to avoid potential problems when re-entrancy *
- * happens - this involves the use of extra bytes associated *
- * with the window data structure. *
- * *
- * The LONG value returned by this function is the conventional *
- * result of the default window procedure or the internal *
- * handling of a message. *
- * *
- *******************************************************************/
-
- LONG FAR PASCAL GasGaugeWndFn( hWnd, wMsg, wParam, lParam )
- HWND hWnd;
- WORD wMsg;
- WORD wParam;
- LONG lParam;
- {
- /* local variables */
- LONG lResult; /* temporary result variable */
-
- /* initialization */
- lResult = TRUE;
-
- /* process message */
- switch( wMsg )
- {
- case WM_GETDLGCODE : /* capture clicks on scroll bar arrows */
-
- lParam = DLGC_WANTARROWS;
- break;
-
- case WM_CREATE :
-
- SET_RANGE( 100 );
- SET_VALUE( 0 );
- SET_WIDTH( ((LPCREATESTRUCT)lParam)->cx );
- SET_HEIGHT( ((LPCREATESTRUCT)lParam)->cy );
- SET_BACKGROUND( RGB (255, 255, 255 )); // white
- SET_FOREGROUND( RGB (255, 0, 0 )); // red
- break;
-
- case WM_SIZE : /* window being resized */
-
- /* redefine width & height instance variables */
-
- SET_WIDTH ( LOWORD(lParam) );
- SET_HEIGHT ( HIWORD(lParam) );
-
- break;
-
- case WM_COMMAND:
-
- switch ( wParam )
- {
- case GG_SETFOREGROUND:
-
- SET_FOREGROUND ( lParam );
- InvalidateRect ( hWnd, NULL, FALSE );
- break;
-
- case GG_SETBACKGROUND:
-
- SET_BACKGROUND ( lParam );
- InvalidateRect ( hWnd, NULL, FALSE );
- break;
-
- case GG_SETRANGE:
-
- SET_RANGE ( (WORD)lParam );
- if ( VALUE > (WORD)lParam )
- SET_VALUE ( (WORD)lParam );
- InvalidateRect ( hWnd, NULL, FALSE );
- break;
-
- case GG_SETVALUE:
-
- if ((WORD)lParam <= RANGE )
- SET_VALUE ( (WORD)lParam );
- else
- SET_VALUE ( RANGE );
- InvalidateRect ( hWnd, NULL, FALSE );
- break;
-
- default:
-
- break;
- }
- break;
-
-
- /* Paint rectangles and % done within control window.
- ** Done indicated by red rectangle, not done is white.
- **
- ** Percent complete is written to bitmap, and then Bitblt'd onto
- ** control. For TextOut to bitmap's DC, use black background
- ** and NOT red/done text so that later Bitblt can XOR
- ** source and dest to get white text on red, or red on white.
- */
- case WM_PAINT:
-
- {
- /* Many of the variable declarations, assignments, and
- ** code below don't need to be done for each paint message
- ** and doing so slows the gauge down; they are here to
- ** make this example easier to follow.
- */
- HDC hDC, // handle to display
- hdcSrc; // handle to % bitmap's DC
- HBITMAP hbmSrc; // handle to % bitmap
- PAINTSTRUCT Ps; // paint structure
- HBRUSH hOldBrush, hForeGround,
- hBackGround;
- HPEN hOldPen, hPen;
- RECT rc; // red or white rect
- WORD x1, x2, x3, y1, y2, // logical coordinates
- xText, xTextWidth, yText,
- yTextHeight; // % text
- char chBuffer[6]; // for % string
- DWORD dwTextExtent, // string text width
- dwOldTextColor, dwOldBkColor;
-
- x1 = 0; // 0=y1+----------+-------------+
- x3 = WIDTH; // | On = Red | Off = White |
- y1 = 0; //cy=y2+----------+-------------+
- y2 = HEIGHT; // x1=0 x2 cx=x3
-
- x2 = (int) ((float)(((float)VALUE / (float)RANGE)) * x3);
-
- if ( x2 > x3 )
- x2 = x3;
-
- hDC = BeginPaint( hWnd, (LPPAINTSTRUCT)&Ps );
-
- hForeGround = CreateSolidBrush ( FOREGROUND );
- hBackGround = CreateSolidBrush ( BACKGROUND );
-
- hOldBrush = SelectObject ( hDC, hForeGround );
-
- // red "amount done" rectangle
- hPen = CreatePen( 0, 1, RGB( 255, 0, 0 ) );
- hOldPen = SelectObject( hDC, hPen );
- Rectangle ( hDC, x1, y1, x2, y2 );
- SelectObject( hDC, hOldPen );
- DeleteObject( hPen );
-
- // white "not done" rectangle
- if ( x2 < x3 )
- {
- SelectObject ( hDC, hBackGround );
- hPen = CreatePen( 0, 1, RGB( 255, 255, 255 ) );
- hOldPen = SelectObject( hDC, hPen );
- Rectangle ( hDC, x2, y1, x3, y2 );
- SelectObject( hDC, hOldPen );
- DeleteObject( hPen );
- }
-
- /* Now write percentage Red/done in middle of gauge */
-
- // Determine percentage Red & center text's location in gauge
- if ( x1 >= x3 )
- sprintf( chBuffer, "100%%" );
- else
- sprintf( chBuffer, "%d%%",
- (int)( 100 * (x2 - x1) / (x3 - x1) ) );
- dwTextExtent = GetTextExtent( hDC, (LPSTR)chBuffer,
- lstrlen((LPSTR)chBuffer) );
- xTextWidth = LOWORD( dwTextExtent );
- yTextHeight = HIWORD( dwTextExtent );
- xText = (x3 - x1 - xTextWidth) >> 1;
- yText = y1 + ( ((y2 - y1) - yTextHeight) >> 1 );
-
- // Create DC & bitmap, write text, and blt to display
- hdcSrc = CreateCompatibleDC( hDC );
- hbmSrc = CreateCompatibleBitmap( hDC, xTextWidth, yTextHeight);
- SelectObject( hdcSrc, hbmSrc );
-
- // text and background color all important for ROP code
- dwOldBkColor = SetBkColor( hdcSrc, RGB( 0, 0, 0 ) ); // black
- dwOldTextColor = SetTextColor( hdcSrc, RGB(0,255,255) ); // ~red
- TextOut( hdcSrc, 0, 0, (LPSTR)chBuffer,
- lstrlen( (LPSTR)chBuffer ) );
- BitBlt( hDC, xText, yText, xTextWidth, yTextHeight,
- hdcSrc, 0, 0, SRCINVERT ); //
-
- // clean up
- SetBkColor( hdcSrc, dwOldBkColor );
- SetTextColor( hdcSrc, dwOldTextColor );
- DeleteObject( hdcSrc );
- DeleteObject( hbmSrc ); // fixes RAID bug 119
- ReleaseDC( hWnd, hdcSrc );
-
- SelectObject ( hDC, hOldBrush );
- DeleteObject ( hForeGround );
- DeleteObject ( hBackGround );
-
- EndPaint( hWnd, (LPPAINTSTRUCT)&Ps );
- }
- break;
-
- default : /* default window message processing */
-
- lResult = DefWindowProc( hWnd, wMsg, wParam, lParam );
- break;
- }
-
- /* return final result */
- return( lResult );
-
- }
-