Doing Tooltips with Mouse events

The next example shows how to add Tooltips to your forms using the new Mouse events. First, an overview of the new mouse events in Microsoft Access 2.0.

The following mouse-related events were added to Microsoft Access 2.0. They are based on the same events in Microsoft Visual Basic.

Click

Fires when user clicks the left mouse button.

DblClick

Fires when the user double-clicks the left mouse button

MouseDown

Fires when the user first clicks any mouse button

MouseUp

Fires when the user releases any mouse button

MouseMove

Fires when the mouse cursor is moved on the screen


One of the keys to working with the mouse events is understanding the coordinate system they use. The following describes some key points about this system:

So when you move the mouse inside a control, the mouse coordinates are relative to the control. To convert these to section coordinates, you need to add the control's Top and Left properties.


    SectionX = ControlX + Forms!MyForm!MyCntl.Left
    SectionY = ControlY + Forms!MyForm!MyCntl.Top

To convert the section coordinates to form coordinates, you need to correct for any possible scroll of the section within the form. You do this with the CurrentSectionLeft and CurrentSectionTop properties.


    FormX = SectionX + Forms!MyForm.CurrentSectionLeft
    FormY = SectionY + Forms!MyForm.CurrentSectionTop

Finally, to convert form coordinates to screen coordinates, you need to compensate for the form caption and borders. This information is available from Microsoft Windows® APIs. (Note: You also need to convert from twips to pixels at this step.)


GetWindowRect Forms!MyForm.hWnd, rcWnd
TwipsPerPixelX = 1440 / GetDeviceCaps(hdc, LOGPIXELSX)
TwipsPerPixelY = 1440 / GetDeviceCaps(hdc, LOGPIXELSY)
ScreenX = FormX / TwipsPerPixelX +  GetSystemMetrics(CM_SXFRAME) + rcWnd.Left
ScreenY = FormY / TwipsPerPixelY + (GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME)) + rcWnd.Top

Now we're ready to look at the Tooltips sample in the AC301 database. This sample uses the Customer form and the Tooltips global module. Open the Customer form and point at a label for one of the text box controls with the mouse. After a few moments, a Tooltip book will appear, similar to the one you see on the toolbars in all Microsoft Office family programs.

The code to implement this is mostly in the Tooltips global module. It's invoked from event procedures in the Customer form module. The events used at the form level are:

The two key events in this sample are the control level mouse move and the timer events. The following functions in the Tooltips global module implement this functionality:


Sub TTProcessControlMove (x As Single, y As Single, Cntl As Control)
Dim i As Integer
Dim Label As Control
Dim ptMouse As POINTAPI

'First map to Tips array using the Tag property in Cntl
'as the index. Get a pointer to the label for the control
i = Cntl.Tag
Set Label = Forms(th.TipForm)(TipList(i).Label)

'Set up mouse point in section coords
ptMouse.x = x + Cntl.Left
ptMouse.y = y + Cntl.Top

'See if the mouse is over the label
If (Not TTInLabel(Label, ptMouse)) Then
    'Not over the label. We only show tips when pointing to
    'a label. Dismiss the tip if showing and kill timer
    TTDismissTip
Else
    'See if showing the tip already
    If (th.TipShowing) Then
        'if we are but have changed labels since this tip
        'went up then dismiss it
        If (i <> th.CurrTip) Then TTDismissTip
    Else
        'Not showing the tip. Start timer if we need to
        If (Forms(th.TipForm).TimerInterval = 0) Then
            Forms(th.TipForm).TimerInterval = th.DelayTime
        End If

        'Record current mouse position and index of label
        th.CurrPos = ptMouse
        th.CurrTip = i
    End If
End If
End Sub

Sub TTProcessTimer ()
Dim CursorPos As POINTAPI
Dim pt As POINTAPI
Dim TipCtl As Control
Dim x As Integer

'Get the current cursor position and map our saved position from the last mouse move message to screen coords
GetCursorPos CursorPos
TTSectionToScreen th.CurrPos, pt

'If the 2 points are the same, then user has waited long enough.
'Time to show the tip
If (CursorPos.x = pt.x) Then
    If (CursorPos.y = pt.y) Then
        'Point to the ToolTip control
        Set TipCtl = Forms(th.TipForm)(th.Tip)

        'First set the text and width of the tip control
        TipCtl.Value = TipList(th.CurrTip).TipText
        TipCtl.Width = TipList(th.CurrTip).TipWidth

        'Now set it in position
        x = th.CurrPos.x - TipCtl.Width / 2
        If (x < 0) Then x = 0
        TipCtl.Left = x
        TipCtl.Top = th.CurrPos.y + GetSystemMetrics(SM_CYCURSOR) * th.TwipsPerPixelY * 2 / 3

        'Finally set the flag and show the control
        th.TipShowing = True
        TipCtl.Visible = True
  End If
End If
End Sub

Please see the sample database for more information on this example.