Author : Bryan King
Date Submitted : 3/15/2002
Category : Forms
Compatibility : VB 6,VB 5
This code has been accessed 7176 times.
Task : This code is a great way to create a toolbar that hides
just above the desktop until the cursor is positioned near
the top of the screen which makes the toolbar drop down.
Moving the cursor off the form makes the form hide itself again.
Declarations
Code
'you will need the following:
'Form1, Timer1, Timer2, Label1, Command1
Private Sub Command1_Click()
Me.Top = 0 - Me.Height 'hide the form just out of sight above the desktop
'out of sight until needed.
MsgBox "Hi" 'replace with your code
'This can also be used to shell external programs
End Sub
Private Sub Timer1_Timer()
'declare the variable.
Dim ptAPI As POINTAPI
'pass the cursor position to the new variable.
GetCursorPos ptAPI
'if the cursor is within 5 twips of the top of the screen.
If ptAPI.Y < 5 Then
'bring the form down so that the top of
'the form aligns with the top of the screen.
Me.Top = ScreenTop
'if the cursor leaves the form, then hide the form
'above the desktop again.
'The Val(ptAPI.Y * 15) code was necessary because the variable ptAPI.Y
'is given the cursor position in pixels and I was using twips when
'calculating the Height and Width of my forms.
'So Val(ptAPI.Y * 15) simply converts the cursor position from
'pixels to twips since the sizes are different in pixels than
'they are in twips. The entire program needs to work by the
'same set of rules.
ElseIf Val(ptAPI.Y * 15) > Me.Height Then 'check to see if the cursor goes below the form
Me.Top = 0 - Me.Height 'hide the form until needed.
ElseIf Val(ptAPI.X * 15) < Me.Left Then 'check to see if the cursor goes to the left of the form
Me.Top = 0 - Me.Height 'hide the form until needed.
ElseIf Val(ptAPI.X * 15) > Me.Left + Me.Width Then 'check to see if the cursor goes to the right of the form
Me.Top = 0 - Me.Height 'hide the form until needed.
End If
End Sub
Private Sub Form_Load()
'begin code to read the screen size
Dim SX As Long
Dim SY As Long
SX = Screen.Width / Screen.TwipsPerPixelX
SY = Screen.Height / Screen.TwipsPerPixelY
'end code to read the screen size
Me.Left = (Screen.Width - Me.Width) / 2 'center the form horizontally on the screen
Me.Top = 0 - Me.Height 'hide the form just out of sight above the desktop
'out of sight until needed.
'put the current time into Label1
Label1.Caption = Format(Now, "h:mm:ss am/pm")
End Sub
Private Sub Timer2_Timer()
'update the current time into Label1
Label1.Caption = Format(Now, "h:mm:ss am/pm")
End Sub