home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / code / design / mstips / dutip.bas next >
BASIC Source File  |  1995-02-27  |  5KB  |  211 lines

  1. Option Explicit
  2.  
  3. 'This module provides the generic tooltip routines.
  4.  
  5.  
  6. Dim frmCurrForm As Form
  7. Dim nCurr_hWnd As Integer
  8.  
  9. Const nTIP_TEXT_OFFSET_IN_PIXELS_X = 2
  10. Const nTIP_TEXT_OFFSET_IN_PIXELS_Y = 18
  11. Const nTIP_TIMER_LARGE_INTERNAL = 1000
  12. Const nTIP_TIMER_SMALL_INTERNAL = 50
  13. Const nTIP_MAX_WIDTH_IN_TWIPS = 2880  '2 inches
  14. Const nlTIP_BACKCOLOR = &H80FFFF  'light yellow
  15.  
  16.  
  17. Type typTipPointAPI
  18.    nX As Integer
  19.    nY As Integer
  20. End Type
  21.  
  22. Const SW_SHOWNOACTIVATE = 4
  23. Const GW_CHILD = 5
  24.  
  25. Declare Sub dutip_GetCursorPos Lib "User" Alias "GetCursorPos" (lpPoint As typTipPointAPI)
  26. Declare Function dutip_nGetActiveWindow Lib "User" Alias "GetActiveWindow" () As Integer
  27. Declare Function dutip_nWindowFromPoint Lib "User" Alias "WindowFromPoint" (ByVal lpPointY As Integer, ByVal lpPointX As Integer) As Integer
  28. Declare Function dutip_nShowWindow Lib "User" Alias "ShowWindow" (ByVal hWnd As Integer, ByVal nCmdShow As Integer) As Integer
  29. Declare Function dutip_nGetWindow Lib "User" Alias "GetWindow" (ByVal hWnd As Integer, ByVal wCmd As Integer) As Integer
  30.  
  31. Sub dutip_DisableTips ()
  32.  
  33. 'Disable the tooltip timer, and unloads
  34. 'the tooltip form.
  35.  
  36.  
  37. frmTip.tmrTip.Enabled = False
  38.  
  39. frmTip.Hide
  40. Unload frmTip
  41.  
  42. Set frmTip = Nothing
  43.  
  44. End Sub
  45.  
  46. Sub dutip_EnableTips ()
  47.  
  48. 'Load the tooltip form, customize colors.
  49.  
  50.  
  51. Load frmTip
  52.  
  53. frmTip.Caption = ""
  54. frmTip.BackColor = nlTIP_BACKCOLOR
  55.  
  56. frmTip.lblTip.BackColor = nlTIP_BACKCOLOR
  57. frmTip.lblTip.AutoSize = True
  58. frmTip.lblTip.Move Screen.TwipsPerPixelX * 2, Screen.TwipsPerPixelY * 1
  59.  
  60. frmTip.tmrTip.Enabled = False
  61.  
  62. End Sub
  63.  
  64. Sub dutip_PollTips ()
  65.  
  66. 'Called from the _Timer event in DUTIP.FRM
  67. 'to periodically check the position of the mouse.
  68.  
  69.  
  70. Dim nSub As Integer, tfControlHit As Integer
  71. Dim nTemp_hWnd As Integer
  72.  
  73. Dim recPoint As typTipPointAPI
  74.  
  75.  
  76. If dutip_nGetActiveWindow() = frmCurrForm.hWnd Then
  77.    Call dutip_GetCursorPos(recPoint)
  78.  
  79.    nTemp_hWnd = dutip_nWindowFromPoint(recPoint.nY, recPoint.nX)
  80.  
  81.    If nTemp_hWnd <> nCurr_hWnd Then
  82.       nCurr_hWnd = nTemp_hWnd
  83.  
  84.  
  85.       tfControlHit = False
  86.  
  87.       On Error Resume Next
  88.  
  89.       For nSub = 0 To frmCurrForm.Controls.Count - 1
  90.      nTemp_hWnd = frmCurrForm.Controls(nSub).hWnd
  91.  
  92.      If Err = 0 Then
  93.         If nCurr_hWnd = nTemp_hWnd Then  'hit on primary window
  94.            tfControlHit = True
  95.         Else                             'check child windows
  96.            If TypeOf frmCurrForm.Controls(nSub) Is ComboBox Then
  97.           If nCurr_hWnd = dutip_nGetWindow(frmCurrForm.Controls(nSub).hWnd, GW_CHILD) Then
  98.              tfControlHit = True
  99.           End If
  100.            Else
  101.           'add checks here for other multi-window controls
  102.            End If
  103.         End If
  104.  
  105.         If tfControlHit Then  'popup the tooltip
  106.            Call dutip_ShowTip(frmCurrForm.Controls(nSub), recPoint)
  107.  
  108.            Exit For
  109.         End If
  110.      Else
  111.         Err = 0
  112.      End If
  113.       Next
  114.  
  115.       On Error GoTo 0
  116.  
  117.       If tfControlHit Then
  118.      frmTip.tmrTip.Interval = nTIP_TIMER_SMALL_INTERNAL
  119.       Else
  120.      If nCurr_hWnd <> frmTip.hWnd Then
  121.         frmTip.tmrTip.Interval = nTIP_TIMER_LARGE_INTERNAL
  122.      End If
  123.  
  124.      frmTip.Hide
  125.       End If
  126.    End If
  127. End If
  128.  
  129. End Sub
  130.  
  131. Sub dutip_SetTipForm (frmForm As Form)
  132.  
  133. 'Identifies the form to monitor, and enables
  134. 'the timer.
  135.  
  136.  
  137. Set frmCurrForm = frmForm
  138. nCurr_hWnd = 0
  139.  
  140. frmTip.tmrTip.Enabled = False
  141. frmTip.tmrTip.Interval = nTIP_TIMER_LARGE_INTERNAL
  142. frmTip.tmrTip.Enabled = True
  143.  
  144. End Sub
  145.  
  146. Sub dutip_ShowTip (ctlControl As Control, recPoint As typTipPointAPI)
  147.  
  148. 'Sizes and positions the tooltip, according to the
  149. 'length of the text and the location of the mouse.
  150.  
  151.  
  152. Dim nRC As Integer
  153. Dim fLeft As Single, fTop As Single
  154. Dim fWidth As Single, fHeight As Single
  155. Dim sTipText As String
  156.  
  157.  
  158. sTipText = ToolTips_sGetTipText(ctlControl, recPoint.nX, recPoint.nY)
  159.       'This callback function lives
  160.       'in *your* application code,
  161.       'not in this module.
  162.  
  163. frmTip.Hide
  164.  
  165. If Len(sTipText) > 0 Then
  166.    frmTip.lblTip.WordWrap = False
  167.    frmTip.lblTip.Caption = sTipText
  168.  
  169.    'Set the tooltip width
  170.  
  171.    If frmTip.lblTip.Width > nTIP_MAX_WIDTH_IN_TWIPS Then
  172.       frmTip.lblTip.WordWrap = True
  173.       frmTip.lblTip.Width = nTIP_MAX_WIDTH_IN_TWIPS
  174.    Else
  175.       If frmTip.lblTip.Width > (nTIP_MAX_WIDTH_IN_TWIPS \ 2) Then
  176.      frmTip.lblTip.WordWrap = True
  177.      frmTip.lblTip.Width = (nTIP_MAX_WIDTH_IN_TWIPS \ 2)
  178.       End If
  179.    End If
  180.  
  181.    'Calculate where to popup the tooltip,
  182.    'relative to the mouse
  183.  
  184.    fLeft = (recPoint.nX - nTIP_TEXT_OFFSET_IN_PIXELS_X) * Screen.TwipsPerPixelX
  185.    fTop = (recPoint.nY + nTIP_TEXT_OFFSET_IN_PIXELS_Y) * Screen.TwipsPerPixelY
  186.    fWidth = (Screen.TwipsPerPixelX * 6) + frmTip.lblTip.Width
  187.    fHeight = (Screen.TwipsPerPixelY * 4) + frmTip.lblTip.Height
  188.    
  189.    'Make adjustments so the tooltip isn't
  190.    'clipped by the edge of the screen
  191.  
  192.    If (fLeft + fWidth) > Screen.Width Then
  193.       fLeft = fLeft - fWidth - (nTIP_TEXT_OFFSET_IN_PIXELS_X * Screen.TwipsPerPixelX)
  194.    End If
  195.    If (fTop + fHeight) > Screen.Height Then
  196.       fTop = fTop - fHeight - (nTIP_TEXT_OFFSET_IN_PIXELS_Y * Screen.TwipsPerPixelY)
  197.    End If
  198.  
  199.    'Position the tooltip
  200.  
  201.    frmTip.Move fLeft, fTop, fWidth, fHeight
  202.    frmTip.ZOrder
  203.  
  204.    'Pop it up, without focus
  205.  
  206.    nRC = dutip_nShowWindow(frmTip.hWnd, SW_SHOWNOACTIVATE)
  207. End If
  208.  
  209. End Sub
  210.  
  211.