VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 如何建立多行的球状工具栏
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:10183, 日期:2001-05-10
How to create multi-line balloon style tool tips in Visual Basic.
by Sheppe Pharis

June 2000


--------------------------------------------------------------------------------

The code in this article has only been tested in Visual Basic 6 SP3, though it should work without a problem in all versions of Visual Basic that support API Declarations.

**Note: This code has not been tested on a platform with a version of the Windows Common Controls that is below 5.81. The Balloon, NoAnimate, and NoFade styles will only work if you have version 5.80 and up of the Windows Common Controls (Comctl32.DLL).


--------------------------------------------------------------------------------

An oft asked question about Visual Basic is, "How do I make multi-line tool tips?". This article is going to to explain the process necessary to do this in as clear and readable a format as possible.

Firstly, you should know that creating multi-line tool tips in VB requires the use of the API (application programming interface). A quick description of the steps taken to create a multi-line tool tip in Visual Basic follows:

Make the necessary API declarations.
Declare the variables to contain and pass the information to and from the API.
Set the necessary values of the declared variables.
Create a new window using the CreateWindowEx API call. This window is the window the tool tip will be contained in.
Determine the region in which the tool tip will be activated.
Set the information necessary to creating a tool tip in a TOOLINFO structure.
Tell the tool tip window to apply itself to the window you want it to be displayed for.
Set the maximum width of the tool tip.
Set the fore and back colours of the tool tip window.
Below is a well commented code example. Understanding this example should not be a problem, and it will give you a good glimpse into how to begin creating all of your own tool tips through the API.


--------------------------------------------------------------------------------

Code Example:

API Declarations. Place all of the code in the table below at the beginning of your form, or change all of the Private statements to Public statements and paste it into a module:

Option Explicit
注释: Beware of word-wrap when copying this code!
注释: API calls necessary for creating and destroying tool tip windows.
Private Declare Function CreateWindowEx Lib "user32"  Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal  lpClassName As String, ByVal lpWindowName As String,  ByVal dwStyle As Long, ByVal X As Long, ByVal Y As Long,  ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long

Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long

注释: A RECT user defined type. This is used for setting the bounds of the tool tip window.
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

注释: A TOOLINFO user defined type. This is used for setting 注释:all of the necessary
注释: flags when creating a tool tip window.
Private Type TOOLINFO
cbSize As Long
uFlags As Long
hwnd As Long
uid As Long
RECT As RECT
hinst As Long
lpszText As String
lParam As Long
End Type

注释: A constant used in conjunction with the CreateWindowEx 注释:API. It indicates to use the default value.
Private Const CW_USEDEFAULT = &H80000000

注释: Constants used with the SetWindowPosition API.
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOACTIVATE = &H10
Private Const SWP_NOSIZE = &H1
Private Const HWND_NOTOPMOST = -2
Private Const HWND_TOP = 0
Private Const HWND_TOPMOST = -1
Private Const HWND_BOTTOM = 1

注释: Constants for setting the style of the tool tip window.
Private Const WS_POPUP = &H80000000
Private Const WS_EX_TOPMOST = &H8&

注释: A constant used with the SendMessage API to define 注释:private messages.
Private Const WM_USER = &H400

注释: Messages used for setting the duration time of tool 注释:tips.
注释: Not used here.
Private Const TTDT_AUTOMATIC = 0
Private Const TTDT_AUTOPOP = 2
Private Const TTDT_INITIAL = 3
Private Const TTDT_RESHOW = 1

注释: All of the flags for tool tip windows.
Private Const TTF_ABSOLUTE = &H80
Private Const TTF_CENTERTIP = &H2
Private Const TTF_DI_SETITEM = &H8000
Private Const TTF_IDISHWND = &H1
Private Const TTF_RTLREADING = &H4
Private Const TTF_SUBCLASS = &H10
Private Const TTF_TRACK = &H20
Private Const TTF_TRANSPARENT = &H100

注释: All of the available messages for tool tip windows.
Private Const TTM_ACTIVATE = (WM_USER + 1)
Private Const TTM_ADDTOOLA = (WM_USER + 4)
Private Const TTM_ADDTOOLW = (WM_USER + 50)
Private Const TTM_ADJUSTRECT = (WM_USER + 31)
Private Const TTM_DELTOOLA = (WM_USER + 5)
Private Const TTM_DELTOOLW = (WM_USER + 51)
Private Const TTM_ENUMTOOLSA = (WM_USER + 14)
Private Const TTM_ENUMTOOLSW = (WM_USER + 58)
Private Const TTM_GETBUBBLESIZE = (WM_USER + 30)
Private Const TTM_GETCURRENTTOOLA = (WM_USER + 15)
Private Const TTM_GETCURRENTTOOLW = (WM_USER + 59)
Private Const TTM_GETDELAYTIME = (WM_USER + 21)
Private Const TTM_GETMARGIN = (WM_USER + 27)
Private Const TTM_GETMAXTIPWIDTH = (WM_USER + 25)
Private Const TTM_GETTEXTA = (WM_USER + 11)
Private Const TTM_GETTEXTW = (WM_USER + 56)
Private Const TTM_GETTIPBKCOLOR = (WM_USER + 22)
Private Const TTM_GETTIPTEXTCOLOR = (WM_USER + 23)
Private Const TTM_GETTOOLCOUNT = (WM_USER + 13)
Private Const TTM_GETTOOLINFOA = (WM_USER + 8)
Private Const TTM_GETTOOLINFOW = (WM_USER + 53)
Private Const TTM_HITTESTA = (WM_USER + 10)
Private Const TTM_HITTESTW = (WM_USER + 55)
Private Const TTM_NEWTOOLRECTA = (WM_USER + 6)
Private Const TTM_NEWTOOLRECTW = (WM_USER + 52)
Private Const TTM_POP = (WM_USER + 28)
Private Const TTM_RELAYEVENT = (WM_USER + 7)
Private Const TTM_SETDELAYTIME = (WM_USER + 3)
Private Const TTM_SETMARGIN = (WM_USER + 26)
Private Const TTM_SETMAXTIPWIDTH = (WM_USER + 24)
Private Const TTM_SETTIPBKCOLOR = (WM_USER + 19)
Private Const TTM_SETTIPTEXTCOLOR = (WM_USER + 20)
Private Const TTM_SETTITLEA = (WM_USER + 32)
Private Const TTM_SETTITLEW = (WM_USER + 33)
Private Const TTM_SETTOOLINFOA = (WM_USER + 9)
Private Const TTM_SETTOOLINFOW = (WM_USER + 54)
Private Const TTM_TRACKACTIVATE = (WM_USER + 17)
Private Const TTM_TRACKPOSITION = (WM_USER + 18)
Private Const TTM_UPDATE = (WM_USER + 29)
Private Const TTM_UPDATETIPTEXTA = (WM_USER + 12)
Private Const TTM_UPDATETIPTEXTW = (WM_USER + 57)
Private Const TTM_WINDOWFROMPOINT = (WM_USER + 16)

注释: Constants for setting the style of the tool tip window.
注释:
注释: Always tip, even if the parent window is not active.
Private Const TTS_ALWAYSTIP = &H1
注释:
注释: Use the balloon style tool tip. <used here>
Private Const TTS_BALLOON = &H40
注释:
注释: Win98 and up - do not use sliding tool tips.
Private Const TTS_NOANIMATE = &H10
注释:
注释: Win2K and up - do not fade in tool tips.
Private Const TTS_NOFADE = &H20
注释:
注释: Prevents windows from removing any ampersand characters 注释:in the tool tip
注释: string. Without this flag, Windows will automatically 注释:remove ampersand
注释: characters from the string. This is done to allow the 注释:same string to be
注释: used as the tool tip text, and as the caption of a 注释:control.
Private Const TTS_NOPREFIX = &H2


注释: The two different tool tip classes.
Private Const TOOLTIPS_CLASS = "tooltips_class"
Private Const TOOLTIPS_CLASSA = "tooltips_class32"

注释: A long for containing the hwnd (window handle) of the 注释:tool tip window created in
注释: this demo. This would have to be an array of longs if 注释:we were to create
注释: tool tip windows for multiple windows/controls.
Dim hwndTT As Long




Event Code. To use the code below ensure that you have two command buttons on your form named cmdToolTip1, and cmdToolTip2:

注释: Beware of word-wrap when copying this code!
Private Sub Form_Load()
注释: Declare a toolinfo UDT variable.
Dim ti As TOOLINFO

注释: A rect UDT variable used to indicate the bounds of the 注释:tool tip window注释:s
注释: region.
Dim RECT As RECT

注释: A long used to pass to the toolinfo UDT as an ID for 注释:the tool tip window.
注释: Does not really do anything in this demo, but is used 注释:for clarity.
Dim uid As Long
uid = 0

注释: A string that contains the text of the tool tip.
Dim strPntr As String
strPntr = "This is what the API created tool tip looks like. " & vbCrLf _
& "And, as you can see, it does support line breaking and the balloon style."

注释: A long to contain the return values of the API calls.
Dim RetVal As Long


注释: Create a tool tip window, and capture it注释:s hwnd in the 注释:form-wide hwndTT long.
hwndTT = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASSA, vbNullString, WS_POPUP Or TTS_NOPREFIX Or TTS_BALLOON, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, Me.hwnd, 0, App.hInstance, 0)

注释: Use the setwindowpos API to set the window position of 注释:the tool tip window.
SetWindowPos hwndTT, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_NOACTIVATE

注释: Determine the rectangle of the control we are adding 注释:the tool tip to. This
注释: will be the region in which the tool tip window will be 注释:activated.
GetClientRect cmdToolTip1.hwnd, RECT

注释: Set all of the necessary info in the toolinfo UDT.
注释:
注释: The size of the toolinfo UDT in bytes. Must be set!
ti.cbSize = Len(ti)
注释:
注释: The flags that we want to pass to the tool tip. 注释:TTF_CENTERTIP is not
注释: necessary, but centers the tool tip to the window it is 注释:being applied to
注释: (when possible). TTF_SUBCLASS tells the tool tip window 注释:to subclass the
注释: window it is being applied to. This is the best route 注释:to take in VB, so
注释: subclassing by the developer is not necessary.
ti.uFlags = TTF_CENTERTIP Or TTF_SUBCLASS
注释:
注释: The hwnd of the control having the tool tip applied.
ti.hwnd = cmdToolTip1.hwnd
注释:
注释: The instance of the app the tool tip applies to.
ti.hinst = App.hInstance
注释:
注释: The ID (hwnd) of the tool tip window. Not necessary 注释:unless the window is
注释: created using the TTF_IDISHWND flag.
ti.uid = uid
注释:
注释: A pointer to the tool tip text.
ti.lpszText = strPntr
注释:
注释: The coordinates that specify the tool tip window注释:s 注释:region of activation.
ti.RECT.Left = RECT.Left
ti.RECT.Top = RECT.Top
ti.RECT.Right = RECT.Right
ti.RECT.Bottom = RECT.Bottom


注释: Send a message to the tool tip window telling it to 注释:apply itself to the
注释: control it is being added to.
RetVal = SendMessage(hwndTT, TTM_ADDTOOLA, 0, ti)

注释: Send a message to the tool tip window telling it to set 注释:the maximum tip
注释: width, to allow line breaking.
RetVal = SendMessage(hwndTT, TTM_SETMAXTIPWIDTH, 0, 80)

注释: Send messages to the tool tip window telling it what 注释:it注释:s fore and back
注释: colours are.
RetVal = SendMessage(hwndTT, TTM_SETTIPBKCOLOR, RGB(255, 255, 255), 0)
RetVal = SendMessage(hwndTT, TTM_SETTIPTEXTCOLOR, RGB(0, 0, 150), 0)

注释: Send a message to the tool tip window telling it to 注释:update itself
注释: (to reflect the new fore and back colours).
RetVal = SendMessage(hwndTT, TTM_UPDATETIPTEXTA, 0, ti)

注释: Set the text of the second command button on the form 注释:to display what
注释: a standard VB tool tip looks like.
cmdToolTip2.ToolTipText = "This is what a standard VB tool tip looks like. " _
& vbCrLf & "And, as you can see by the CRLF characters to the left, " _
& "line breaking is not supported by the standard VB tool tips."

End Sub

Private Sub Form_Unload(Cancel As Integer)

注释: When the form unloads, be sure and destroy any tool tip windows created.
DestroyWindow hwndTT

End Sub