Minggu, 26 Oktober 2008

Implementing mouse-over effects on VB6 forms

If you spend much time browsing the Web, you'll notice that some Web pages feature mouse-over effects. These effects change the appearance of a screen element when a user passes the mouse cursor over the element without clicking on it. When the user moves the mouse cursor, the element goes back to its original appearance.

Mouse-over effects can be an effective way to improve the visual appearance of your programs. In order to create mouse-over effects in VB6, you'll need to use the MouseMove event, which fires whenever the mouse cursor moves over a screen element. Here's what to do:

In the MouseMove event procedure for the control for which you're creating the mouse-over effect, put code that changes the font, color, or other aspects of the control's appearance.
In the MouseMove event procedure for the control's container (which is usually the form), place code that changes the control back to its normal appearance.
You may also want to save the control's default appearance when the form loads. This allows you to change its default appearance in the VB form editor without worrying about changing your code.

Here's an example that implements a mouse-over effect for a command button. In the form, at the module level, declare two variables to hold the control's default color and font size:
Private OrigColorAs Long
Private OrigSize As Integer
In the form's Load event procedure, load these variables with the control properties:

Private Sub Form_Load()
OrigColor = Command1.BackColor
OrigSize = Command1.Font.Size
End Sub
In the command button's MouseMove event procedure, change its appearance to the "over" state:

Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
Command1.BackColor = vbRed
Command1.Font.Size = 14
End Sub
Finally, in the form's MouseMove event procedure, change the variables back to the default values:

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, _
Y As Single)
Command1.Font.Size = OrigSize
Command1.BackColor = OrigColor
End Sub

Tidak ada komentar: