|
|
Title | Make ActiveX button controls that display different pictures when up or down |
Description | This example shows how to make ActiveX button controls that display different pictures when up or down in Visual Basic 6. The controls track the MouseDown, MouseMove, and MouseUp events to decide which picture to display. |
Keywords | ActiveX control, button, picture |
Categories | Controls, ActiveX Controls, ActiveX |
|
|
The PictureButton control has UpPicture and DownPicture properties. Its MouseDown, MouseMove, and MouseUp event handlers track the mouse position and display the correct picture depending on whether the mouse is over the control.
When the control receives a Click event, the control raises its Click event.
|
|
Private Sub UserControl_MouseDown(Button As Integer, Shift _
As Integer, X As Single, Y As Single)
m_Clicking = True
UserControl.Picture = m_DownPicture
End Sub
Private Sub UserControl_MouseMove(Button As Integer, Shift _
As Integer, X As Single, Y As Single)
Dim should_be_pressed As Boolean
Dim is_pressed As Boolean
If Not m_Clicking Then Exit Sub
should_be_pressed = _
(X >= 0 And X < m_WidPix) And _
(Y >= 0 And Y < m_HgtPix)
is_pressed = UserControl.Picture Is m_DownPicture
If is_pressed <> should_be_pressed Then
If should_be_pressed Then
UserControl.Picture = m_DownPicture
Else
UserControl.Picture = m_UpPicture
End If
End If
End Sub
Private Sub UserControl_MouseUp(Button As Integer, Shift As _
Integer, X As Single, Y As Single)
Dim should_be_pressed As Boolean
If Not m_Clicking Then Exit Sub
m_Clicking = False
should_be_pressed = _
(X >= 0 And X < m_WidPix) And _
(Y >= 0 And Y < m_HgtPix)
If should_be_pressed Then RaiseEvent Click
UserControl.Picture = m_UpPicture
End Sub
Private Sub UserControl_Click()
RaiseEvent Click
End Sub
|
|
The DragButton control displays a button that lets the user drag the control's form. Its MouseDown event handler displays the control's "down" picture and saves the mouse's initial position.
The MouseMove event handler moves the control's form by the distance the mouse has moved.
The MouseUp event handler stops dragging the form and displays the "up" picture.
|
|
Private Sub UserControl_MouseDown(Button As Integer, Shift _
As Integer, X As Single, Y As Single)
m_Dragging = True
UserControl.Picture = m_DownPicture
m_X0 = X
m_Y0 = Y
End Sub
' Move the form.
Private Sub UserControl_MouseMove(Button As Integer, Shift _
As Integer, X As Single, Y As Single)
If Not m_Dragging Then Exit Sub
' See how far the mouse has moved since
' last time in twips.
Dim dx As Single
Dim dy As Single
dx = ScaleX(X - m_X0, vbPixels, vbTwips)
dy = ScaleY(Y - m_Y0, vbPixels, vbTwips)
' Move the form.
Dim frm As Form
Set frm = UserControl.Parent
frm.Move frm.Left + dx, frm.Top + dy
End Sub
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
UserControl.Picture = m_UpPicture
End Sub
|
|
|
|
|
|