home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Advanced Visual Basic Techniques
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471188816
Publication Date: 06/01/97

Search this book:
 
Previous Table of Contents Next


Modifying Bar Gauge

The bar gauge control shows how an ActiveX control can handle mouse and keyboard events. By modifying the control slightly, creating other gauges with very different appearances is easy. Figure 15.5 shows several copies of four different kinds of gauge controls.


FIGURE 15.5  Modified bar gauges.

All of the controls shown in Figure 15.5 are based on the bar gauge control. They provide the same two events, Change and Scroll, and they support almost the same properties. The main difference between them is in their Paint subroutines. A few also support additional properties used to specify their particular styles. All of the controls support two- and three-dimensional appearances. All except the Dial controls shown at the bottom of Figure 15.5 also support the left-to-right, right-to-left, bottom-to-top, and top-to-bottom styles. The Dial control supports only the two horizontal styles, left-to-right and right-to-left.

The following sections briefly explain how these controls differ from the bar gauge. You can find the complete code for each of them, and for the test application shown in Figure 15.5, in the Ch15\Others directory on the compact disk.

TicGauge

The tic gauge control, shown in the upper left of Figure 15.5, displays the selected value as a series of tic marks. The tic gauge has one property that the standard bar gauge does not: TicWidth. This property determines how wide each of the tic marks is in pixels. The gap between tics is the same width as the tic marks. TicWidth is a simple numeric value with straightforward property procedures.

The only other difference between the tic gauge and the bar gauge is in the way it is drawn. The following code fragment shows the interesting parts of tic gauge’s Paint subroutine.

As in the previous version, Paint starts by clearing the visible picture box and by calculating the fraction of the control that should be filled to represent the current value. It then repeatedly draws tic marks, starting at the end that is appropriate for the control’s Style, and continuing until it reaches the calculated value.

Finally, if the control has the input focus, Paint draws a dotted line around the outside edge of the control.

Private Sub Paint()
 :
    ‘ Calculate how much of the gauge should be filled, etc.
     :
    ‘ Draw the gauge with the correct style.
     dx = TicWidth
    x1 = 0
    Do While x1 < v
        x2 = x1 + dx
        If x2 > v Then x2 = v
        
        Select Case the_style
            Case gsLeftToRight
                the_canvas.Line (x1, 0)-(x2, hgt), , BF
            Case gsRightToLeft
                the_canvas.Line (wid - x1, 0)-(wid - x2, hgt), , BF
            Case gsBottomToTop
                the_canvas.Line (0, hgt - x1)-(wid, hgt - x2), , BF
            Case gsTopToBottom
                the_canvas.Line (0, x1)-(wid, x2), , BF
        End Select
        
        x1 = x2 + dx + 2
       Loop
       ‘ If the gauge has the focus, outline it.
        :
End Sub

WidGauge

The wide gauge control uses bars of two different thicknesses and colors to display values. The controls third and fourth from the top on the left in Figure 15.5 are wide gauge control’s.

The control’s ForeColor property determines the color of the larger bar as it does for the bar gauge. The BackColor property determines the color shown behind the narrower bar.

The new property NarrowColor determines the color of the narrower bar. The numeric property NarrowPercent determines how thick the narrow bar is. For example, when NarrowPercent is 50, the narrow bar is 50 percent as wide as the thick bar.

The wide gauge’s Paint subroutine is very similar to the one used by the original bar gauge control. After clearing the control, however, this version draws the narrower bar. It then draws the thick bar on top of the narrow one.

The following code fragment shows how the Paint subroutine draws the narrower bar. The rest of the routine is the same as the original version.

Private Sub Paint()
    :
    ‘ Draw the narrow part.
    wid = the_canvas.ScaleWidth
    hgt = the_canvas.ScaleHeight
    Select Case the_style
        Case gsLeftToRight
            v1 = (hgt * NarrowPercent) / 200#
            v2 = hgt - v1
            the_canvas.Line (0, v1)-(wid, v2), NarrowColor, BF
        Case gsRightToLeft
            v1 = (hgt * NarrowPercent) / 200#
            v2 = hgt - v1
            the_canvas.Line (0, v1)-(wid, v2), NarrowColor, BF
        Case gsBottomToTop
            v1 = (wid * NarrowPercent) / 200#
            v2 = wid - v1
            the_canvas.Line (v1, 0)-(v2, hgt), NarrowColor, BF
        Case gsTopToBottom
            v1 = (wid * NarrowPercent) / 200#
            v2 = wid - v1
            the_canvas.Line (v1, 0)-(v2, hgt), NarrowColor, BF
       End Select
    ‘ Draw the thick bar as before.
    :
End Sub

PicGauge

The picture gauge control fills a bar area with copies of a picture. The bottom two horizontal gauges in Figure 15.5 are picture gauges.

The picture gauge displays a value by tiling an area with a picture rather than by filling it with a solid color. This means it does not need a ForeColor property so this control does not support ForeColor. Instead, it has a FillPicture property that specifies the picture that should be used to tile the area.

FillPicture is declared as type StdPicture. This allows the Visual Basic Properties window to display the picture in a standard way. If the application programmer clicks on the value’s drop-down arrow, Visual Basic presents a standard picture selection dialog.

Because a variable of type StdPicture is an object, an application using the FillPicture property should initialize variables using the Set statement—for example:

Set PicGauge1.FillPicture = LoadPicture("MY_PICT.BMP")

This means FillPicture must have a property set procedure rather than a property let procedure.

Public Property Set FillPicture(pic As StdPicture)
    Set fill_picture = pic

    PropertyChanged "FillPicture" ‘ Tell VB.
    Paint   ‘ Redisplay the gauge.
End Property


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.