Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a bar gauge ActiveX control
Keywordsbar gauge, ActiveX control
CategoriesGraphics, ActiveX Controls, ActiveX, Controls
 
In the control's MouseDown event handler, start dragging. Calculate the new value and call DisplayValue to draw the value indicator. Raise the ValueScroll event so the main program can take action if appropriate.

In the MouseMove event handler, calculate the new value, call DisplayValue to display it, and raise the ValueScroll event.

In the MouseUp event handler, stop dragging and raise the ValueChanged event. If the program only wants to take action after the user finishes dragging the value, it should watch for this event and not the ValueScroll event.

 
' Start dragging.
Private Sub UserControl_MouseDown(Button As Integer, Shift _
    As Integer, X As Single, Y As Single)
    m_Dragging = True

    ' Calculate and display the new value.
    m_Value = m_Minimum + (m_Maximum - m_Minimum) * X / _
        ScaleWidth
    DisplayValue
    RaiseEvent ValueScroll
End Sub

' Continue dragging.
Private Sub UserControl_MouseMove(Button As Integer, Shift _
    As Integer, X As Single, Y As Single)
    If Not m_Dragging Then Exit Sub

    ' Calculate and display the new value.
    If X < 0 Then X = 0
    If X > ScaleWidth Then X = ScaleWidth
    m_Value = m_Minimum + (m_Maximum - m_Minimum) * X / _
        ScaleWidth
    DisplayValue
    RaiseEvent ValueScroll
End Sub

' Finish the drag.
Private Sub UserControl_MouseUp(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    If Not m_Dragging Then Exit Sub
    m_Dragging = False

    RaiseEvent ValueChanged
End Sub
 
Subroutine DrawBackground gives the control a shaded background. It then sets Picture = Image so the background is permanent. Later it can use Cls to restore this image.

Subroutine DisplayValue erases any previous line and draws a new one showing the current value.

 
' Draw a background.
Private Sub DrawBackground()
Dim i As Integer
Dim clr As Integer
Dim dclr As Single
Dim hgt As Single

    ' Give the slider an interesting background.
    UserControl.ScaleMode = vbPixels
    dclr = 256 / UserControl.ScaleWidth
    hgt = UserControl.ScaleHeight
    For i = 0 To UserControl.ScaleWidth
        clr = i * dclr
        UserControl.Line (i, 0)-(i, hgt), RGB(0, 0, clr)
    Next i

    ' Make the image permanent.
    UserControl.Picture = UserControl.Image

    ' Prepare.
    UserControl.DrawWidth = 2
End Sub

' Display the current value.
Private Sub DisplayValue()
Dim X As Single

    ' Erase any previous line.
    Cls

    ' Draw the new line.
    On Error Resume Next
    X = ScaleWidth * (m_Value - m_Minimum) / (m_Maximum - _
        m_Minimum)
    Line (X, 0)-Step(0, ScaleHeight)
End Sub
 
A couple tips:
  • The UserControl's AutoRedraw property is set to True at control design time so it keeps its background image.
  • The ForeColor property let procedure calls DisplayValue to redisplay the value because this property changes the display's appearance.
  • The control redraws its background and value when it is resized.
  • The control redraws its value if Value, Minimum, or Maximum change.
  • The Minimum, Maximum, and Value property procedures ensure that Minimum <= Value <= Maximum at all times.
  • The control's ToolboxBitmap property is set at control design time. This is a 16x15 pixel bitmap.

For more information on creating ActiveX controls, see my book Custom Controls Library.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated