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


The picture gauge’s Paint subroutine begins by calculating the amount of the gauge that should be filled as usual. Then, much like the tic gauge, it begins filling small regions in the control until it has filled the desired area. The process is somewhat more complicated here because the PaintPicture routine used to draw the picture is not as simple as the Line statement used to draw tic marks.

For example, suppose the control has the right-to-left style. The control’s value will probably make the control display only part of the leftmost picture, just as the last horizontal gauge in Figure 15.5 displays roughly half of its leftmost picture. The Paint subroutine must not only calculate the fraction of the picture that must be drawn, but it must draw the rightmost portion of the picture. The PaintPicture statement is designed to draw portions of pictures from the left, so Paint must do a bit of work to place the correct piece of the picture in the proper position.

The following code shows the parts of the Paint subroutine that are unique to the picture gauge:

Private Sub Paint()
    :
    ‘ If there is no picture, do nothing.
     If FillPicture Is Nothing Then Exit Sub
    :
    ‘ Calculate v, the amount to fill, as usual.
    :
    ‘ See how big the picture is.
    pic_wid = CInt(ScaleX(FillPicture.Width, vbHimetric, vbPixels))
    pic_hgt = CInt(ScaleY(FillPicture.Height, vbHimetric, vbPixels))
   
    ‘ Draw the gauge with the correct style.
     Select Case the_style
     Case gsLeftToRight
         x1 = 0
         y1 = 0
         Do While x1 < v
          If x1 + pic_wid > v Then pic_wid = v - x1
          If pic_wid > 0 Then
              the_canvas.PaintPicture FillPicture, _
                  x1, y1, pic_wid, pic_hgt, _
                  , , pic_wid, pic_hgt
              x1 = x1 + pic_wid
          Else
              x1 = v + 1
          End If
         Loop
        
     Case gsRightToLeft
         x1 = pic_wid
         y1 = 0
         Do While x1 - pic_wid < v
          If x1 <= v Then
              the_canvas.PaintPicture FillPicture, _
                  wid - x1, y1, pic_wid, pic_hgt, _
                  , , pic_wid, pic_hgt
              x1 = x1 + pic_wid
          Else
              x2 = x1 - v
              x1 = v
              pic_wid = pic_wid - x2
              If pic_wid > 0 Then
               the_canvas.PaintPicture FillPicture, _
                    wid - x1, y1, pic_wid, pic_hgt, _
                    x2, , pic_wid, pic_hgt
              End If
              Exit Do
          End If
       Loop
   
     Case gsBottomToTop
         x1 = 0
         y1 = pic_hgt
         Do While y1 - pic_hgt < v
             If y1 <= v Then
                 the_canvas.PaintPicture FillPicture, _
                     x1, hgt - y1, pic_wid, pic_hgt, _
                     , , pic_wid, pic_hgt
                  y1 = y1 + pic_wid
          Else
              y2 = y1 - v
              y1 = v
              pic_hgt = pic_hgt - y2
              If pic_hgt > 0 Then
                  the_canvas.PaintPicture FillPicture, _
                      x1, hgt - y1, pic_wid, pic_hgt, _
                      , y2, pic_wid, pic_hgt
              End If
              Exit Do
          End If
     Loop
     
 Case gsTopToBottom
     x1 = 0
     y1 = 0
     Do While y1 < v
          If y1 + pic_hgt > v Then pic_hgt = v - y1
          If pic_hgt > 0 Then
              the_canvas.PaintPicture FillPicture, _
                   x1, y1, pic_wid, pic_hgt, _
                   , , pic_wid, pic_hgt
              y1 = y1 + pic_wid
          Else
              y1 = v + 1
          End If
         Loop
   
    End Select
    :
    ‘ Draw the focus outline as usual (if necessary).
     :
End Sub

Dial

At first glance, the dial gauges shown at the bottom of Figure 15.5 seem to be completely unrelated to the standard bar gauge. Actually, the two types of controls are very similar. The main differences are in the way the controls draw themselves and in the way they calculate new values based on mouse events.

The dial gauge supports an extra FillColor property that determines the color that fills the face of the dial. It also provides a simple numeric property TicSpacing that determines the distance between tic marks on the dial. TicSpacing is measured in the same units as the control’s Min, Max, and Value properties. For example, suppose the control’s Min, Max, and TicSpacing values are 0, 100, and 10, respectively. Then the control will display a tic mark every 10 units along the face of the dial.

This control’s Paint subroutine is simplified slightly by the fact that it supports only the horizontal left-to-right and right-to-left styles. After clearing the control, Paint draws the dial’s semicircular face, filling it with the value specified by the FillColor property. It then draws the tic marks around the inside edge of the dial. It draws the value needle and finishes by drawing the focus rectangle if the control has the input focus.

Private Sub Paint()
Dim r1 As Single ‘ Outer radius.
Dim r2 As Single ‘ Inner (tic) radius.
Dim x0 As Single ‘ The origin of the circle.
Dim y0 As Single
Dim x1 As Single
Dim y1 As Single
Dim x2 As Single
Dim y2 As Single
Dim wid As Single
Dim hgt As Single
Dim theta As Single
Dim dtheta As Single
Dim stheta As Single
Dim ctheta As Single
     ‘ Do not redraw while loading.
     If loading Then Exit Sub
   
     ‘ Start from scratch.
     the_canvas.Cls
               
     ‘ Don’t bother if Min >= Max.
     If the_min >= the_max Then Exit Sub
               
    ‘Draw the circle.
    wid = the_canvas.ScaleWidth
    hgt = the_canvas.ScaleHeight
    r1 = hgt - 2
    r2 = wid / 2 - 1
    If r2 < r1 Then r1 = r2
    r2 = 0.9 * r1
    x0 = wid / 2
    y0 = hgt - 1
    the_canvas.FillStyle = vbSolid
    the_canvas.Circle (x0, y0), r1, , -0.0000001, -PI
    the_canvas.FillStyle = vbFSTransparent
   
     ‘ Draw the tic marks.
     If tic_spacing > 0 Then
        dtheta = PI * tic_spacing / (the_max - the_min)
        For theta = dtheta To PI Step dtheta
            If Style = gsLeftToRight Then
               ctheta = Cos(PI - theta)
            Else
               ctheta = Cos(theta)
            End If
            stheta = Sin(theta)
            x1 = x0 + r1 * ctheta
            x2 = x0 + r2 * ctheta
            y1 = y0 - r1 * stheta
            y2 = y0 - r2 * stheta
        
            the_canvas.Line (x1, y1)-(x2, y2)
        Next theta
    End If
   
    ‘ See where the needle belongs.
    If the_style = gsLeftToRight Then
        theta = PI * (1 - (the_value - the_min) / (the_max - the_min))
    Else
        theta = PI * (the_value - the_min) / (the_max - the_min)
    End If
   
    ‘ Draw the needle.
    the_canvas.Line (x0, y0)- _
        (x0 + r1 * Cos(theta), y0 - r1 * Sin(theta))
    ‘ If the gauge has the focus, outline it.
    If has_focus Then
        the_canvas.DrawStyle = vbDot
        the_canvas.DrawMode = vbInvert
        the_canvas.Line (0, 0)- _
             (wid - 1, hgt - 1), _
             , B
        the_canvas.DrawStyle = vbSolid
        the_canvas.DrawMode = vbCopyPen
    End If
End Sub


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.