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


Appearance

Some controls include properties that are available only at design time, that are available only at run time, or that are read-only at run time. Using some of these properties can present unique problems for ActiveX controls.

The Appearance property determines whether a control should appear three-dimensional or whether it should have a flat, two-dimensional appearance. Figure 15.4 shows the same bar gauges shown in Figure 15.1. The upper horizontal gauge and the vertical gauge on the left have Appearance set to 0 - appears2D.

Appearance is supported by the UserControl object, so one might think it would be easy to implement an Appearance property by delegating it. Unfortunately, the Appearance property is read-only at run time. Whenever an ActiveX control is created, modified, or destroyed, the control’s code executes. For instance, when an application designer creates a new instance of an ActiveX control, the control’s InitProperties event handler is invoked. InitProperties will usually call a series of property procedures to initialize the control’s properties.


FIGURE 15.4  Two- and three-dimensional bar gauges.

While the control code executes, the control acts in run-time mode. Even though the application designer is working in design time and the application being built is in design mode, the ActiveX control is working in run time. That means it cannot set the UserControl’s Appearance property because that property is read-only at run time. That means the control cannot delegate the Appearance property to the UserControl object.

There are two solutions to this problem. First, the control can manage its own Appearance property. It would store the property’s value in a private variable and draw itself in a three-dimensional way when necessary. Drawing a three-dimensional Appearance property and making it match that of other controls can be difficult.

A simpler solution is to create two sets of controls within the ActiveX control, one with Appearance set to 1 - 3D and the other with Appearance 0 - Flat. The ActiveX control’s Appearance property can be managed locally rather than by delegation, and the controls with the appropriate Appearance can be used to display the ActiveX control.

The bar gauge control contains two picture boxes named Picture2D and Picture3D. The first has Appearance set to 0 - Flat; the other has Appearance set to 1 - 3D. Both have BorderStyle set to 1 - Fixed Single.

The following code shows the UserControl’s Resize event handler. It resizes both of the picture boxes so they fill the UserControl’s drawing area.

Private Sub UserControl_Resize()
    Picture2D.Move 0, 0, ScaleWidth, ScaleHeight
    Picture3D.Move 0, 0, ScaleWidth, ScaleHeight
End Sub

The Appearance property is managed locally using the private variable the_appearance. When Appearance is modified, its property let procedure updates the value of the_apparance. It also makes the appropriate one of the two picture boxes visible and saves a reference to that picture box in the variable the_canvas. This variable is used later when drawing the bar gauge.

Public Enum AppearanceValue
    appears2D
    appears3D
End Enum
Private the_appearance As AppearanceValue
    :
Dim the_canvas As PictureBox
    :
Public Property Let Appearance(val As AppearanceValue)
    the_appearance = val
   
    ‘ Make the correct picture box visible.
    If the_appearance = appears2D Then
        Picture2D.Visible = True
        Picture3D.Visible = False
        Set the_canvas = Picture2D
    Else
        Picture2D.Visible = False
        Picture3D.Visible = True
        Set the_canvas = Picture3D
    End If
   
    PropertyChanged "Appearance"    ‘ Tell VB.    
    Paint   ‘ Redisplay the gauge.
End Property

When the control needs to display itself, it draws on the currently visible picture box, as described in the next section.

Drawing Bar Gauge

Whenever one of the bar gauge’s properties changes, the corresponding property procedure invokes the Paint subroutine to redraw the control using the new property values. Similarly, whenever either picture box Picture2D or Picture3D receives a Paint event, it invokes the Paint subroutine.

Paint starts by clearing the picture box that is currently visible. It then calculates the fraction of the picture box that should be filled to represent the control’s current value. Next, it fills in the correct part of the picture box depending on the control’s Style. For example, if Style is gsLeftToRight, the routine fills the left portion of the picture box.

Finally, if the control has the input focus, Paint draws a dotted box around the inside of the picture box. The line is drawn in drawing mode vbInvert. This drawing mode inverts the color values for the pixels that are drawn. By inverting, the control increases the chances that the dotted box will be visible no matter what ForeColor and BackColor properties the control has.

Private Sub Paint()
Dim v As Single
Dim wid As Single
Dim hgt As Single

 ‘ Do nothing if we’re loading.
    If loading Then Exit Sub
 ‘ Start from scratch.
    the_canvas.Cls
      
 ‘ See how much of the gauge should be filled.
    If the_min >= the_max Then
     ‘ Fill in nothing.
        v = 0#
    Else
        v = (the_value - the_min) / (the_max - the_min)
    End If
        
    wid = the_canvas.ScaleWidth
    hgt = the_canvas.ScaleHeight
      
 ‘ Convert the value to pixels.
    If the_style = gsLeftToRight Or _
       the_style = gsRightToLeft _
    Then
        v = v * wid
    Else
        v = v * hgt
    End If
      
 ‘ Draw the gauge with the correct style.
    Select Case the_style
        Case gsLeftToRight
            the_canvas.Line (0, 0)-(v, hgt), , BF
        Case gsRightToLeft
            the_canvas.Line (wid, 0)-(wid - v, hgt), , BF
        Case gsBottomToTop
            the_canvas.Line (0, hgt)-(wid, hgt - v), , BF
        Case gsTopToBottom
            the_canvas.Line (0, 0)-(wid, v), , BF
    End Select
   
 ‘ 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

Managing the Mouse

When the user clicks and drags on the bar gauge, the control must adjust its value. The control handles mouse movements in MouseDown, MouseMove, and MouseUp event handlers. Because either picture box Picture2D or Picture3D might be visible at any given time, the control must handle all three mouse events for both controls. Rather than duplicating three complicated event handlers for both picture boxes, the event handlers simply invoke other routines to process the mouse events. For example, the Picture2D_MouseDown and Picture3D_MouseDown event handlers both invoke the private subroutine MouseDown.

Private Sub Picture3d_MouseMove(Button As Integer, _
    Shift As Integer, X As Single, Y As Single)

    MouseMove X, Y
End Sub

The other mouse event handlers are very similar.


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.