The attached file contains a replacement (or augment) to the ANSI-only, boxy, single-line tooltips provided by VB6. Features of the new class include:
Full Unicode support.
RightToLeft support for tooltips on controls and the form itself.
Multi-line tooltips.
Tooltips can have titles.
Tooltips can be rectangular or balloons with stems.
Some instrinsic controls can have tooltips that cannot be set in traditional VB6 including the form itself, CheckBox, HScrollBar and VScrollBar.
If you are not using VisualStyles, you can set tooltip text and background colors and the margins between the edge of the tooltip and the text.
You can set the maximum width of the toolbar and if your line of text is wider than that, Windows will determine where to break lines.
You can set how long to delay before pop-up, how long to display the tooltip and how long to re-display the tooltip when yo move away and come back.
You can quickly update the text n a tooltip.
You can deactivate/reactivate any tooltip at any time.
You can put a hyperlink in a tooltip and it will show up as such. I haven’t yet figured out to be able to click on it because the tooltip either moves away or disappears when I go after it. I’ll figure it out soon. I suspect I have to fix the tooltip position and keep it displayed a bit.
This system is very simple to use. It requires no special files of any kind.
No hooks, callbacks or sub-classing is used (Windows does some but we re protected from it) so using these tooltip functions within the IDE is perfectly safe.
The module is very small, adding only 16k to your file.
Usage
Include the class module clToolTips in your project. At the top of each form declare it such as this:
Code:
Private myToolTips As clToolTips
Then in your Form_Load procedure set the module and call the NewSetUp procedure:
Code:
Set myToolTips = New clToolTips
myToolTips.NewSetUp Me
At this point, the class module knows all about your form and its controls. It has set default values for each of the variables discussed below. You have several ways of setting tooltips now or at any point in the run. Many controls have a ToolTipText property which you can set at design-time or at run-time. The system used in this class module doesn’t make use of any of those because VB6 uses a different method for showing tooltips. But you have the option of using those old ToolTipText values to set the new tooltips. You can do it for one control using the Sub UseToolTipText or use the sub DoAllToolTips to make new tooltips out of any controls that have a value set for ToolTipText.
The main procedure in the class module is SetToolTip and it allows you to specify the tooltip, whether you want an icon shown and optionally a title.
One of the nifty things about this tooltip system is that you can have multiline tooltips which can occur in two ways. You can specify a maximum width for one or all of the tooltips (discussed below) and if the tooltip text you supply is wider than the maximum tooltip width then Windows automatically looks for spaces to break the lines. Also, quite often you will want more control over where lines break so you can include vbCrLf (or vbNewLine) in your text wherever you want a line break. This gets to be a hassle so you may want to use a shortcut we provide (“``”) which gets converted into a vbCrLf for you. This character is the uppercase of the tilde character just to the left of the “1” key on Western keyboards. You can change this constant in the class module to be whatever you want. I suggest finding some key on the keyboard that you rarely use and put 2 of them in the constant ReplvbCrLf which is defined around line 166.
You can deactivate or reactivate any tooltip at any time with the ActivateOrNot procedure.
You can delete a tooltip by using DeleteToolTip.
Variables
There are some variables that are set to default values but you can change them at any time. Once changed, the variable holds that value until you change it again. For example, if you change the text color of the first tooltip you design you can leave the color value alone and all subsequent tooltips will use the same color.
Beware that some of the variables you can set are ignored if your program uses Visual Styles (themes) because Windows will choose for you. So far, I have found that text color, background color and the 4 internal margin settings are affected by this. BTW, if you are using a control that has the capability to use VisualStyles (like Krool’s VBCCRxx controls), it apparently doesn’t matter if you have VisualStyles turned on or off for a given control because Windows takes over. I think this must be that the tooltip is actually a new window (complete with its own handle) and if VisualStyles are no for the program then the new windows have them turned on as well.
TTForeColor As Long – Sets the text color (ignored if using VisualStyles). Default -1& which tells the system to let Windows choose the text color. You can use any RGB color or VB constants such as vbGreen.
TTBackColor As Long – Background color for the tooltip. Default -1& which means let Windows decide. This value is ignored if using VisualStyles.
TTCentered As Boolean- True => center the text; False => left justify. Default is False.
TTBalloon As Boolean- True => use a balloon with a stem that points to your control. False => Use a rectangle for the tooltip. Default is True.
TTWrapTextLength As Long- This is another method of controlling the horizontal size of the tooltip but this one is based on a width in terms of characters in the tooltip rather than a maximum iwdth in pixels. For example, suppose you set a wide max tooltip of half of the screen width but you had a line of text that you wanted to limit to 40 characters. You could do that by setting this variable to 40. You would not have a wide tooltip; Windows will only make the tooltip wide enough to handle the 40-character width. I don’t use this method much so I hae set the default at 0 which means it is not used. BTW, the code for this was modified from some code Elroy on vbForums gave me.
TTTipWidth As Single- This sets the maximum width of the tooltip expressed in terms of a fraction of the width of the screen with the form and controls. It does not set the width of the tooltip, oonly the maximum. For example, if you set this to 1 you are telling the system that your tooltip can be as wide as the screen but if you set a tooltip with the text “Hello World” the tooltip will be very small. ON the other hand, if you don’t specify line breaks in your tooltip text and the length of the tooltip string is such that it is wider than the maximum toolptip size, Windows will do the line breaks for you and the width of the tooltip will be whatever you have set here as the maximum. If you set this to 0 then there is no text wrapping. The default is 0.3, 30% of your screen’s width.
TTDelayTime As Integer- Sets how long (milliseconds) a pointer must be stationary over the control for the tooltip to pop up. The default is -1 which will use Microsoft’s default time.
TTVisibleTime As Integer- Sets how long the tooltip will display if the pointer is stationary. The default is -1 which means Windows handles it (Windows sets this to be 10 times the delay time above).
TTReshowTime As Integer- Sets the amount of time for subsequent tooltips to appear as the pointer moves from one control to another. The default is -1 which lets Windows handle it (Windows sets it to 1/5th the delay time.
TTMarginsTop As Long- If nt using VisualStyles you can override the Windows defaults and set your own margins for top, left, bottom and right in pixels. If Top = -1 Windows defaults are used for all margins.
TTMarginsLeft As Long- See above discussion. The default is -1.
TTMarginsBottom As Long- See above discussion. The default is -1.
TTMarginsRight As Long- See above discussion. The default is -1.
There are 3 screenshots in my initial post. Two of them came from running the program with a manifest and I thought I had addressed it quite a lot by going over the VisualSyles stuff. It does make quite a difference. I highlighted that it ignores text and background color changes and margins. The other thing it does it to make hyperlinks not appear as hyperlinks.
I do now that the tooltips work great with Krool's VBCCRxx controls. One interesting find was that even if you turn off VisualStyles for a given control, the tooltip still acts as if VisualStyles were active. It must be that since the whole app is themed Windows will apply VisualStyles to all new windows including pop-ups. That doesn't make sense to me but at least it makes the entire app's tooltips look the same.
BTW, I don't know if you have a manifest for VB6 but it makes it easy to check out the effects of VisualStyles on your controls. Here is a link to an old article on how to make and use a manifest file with VB6.EXE to get VisualStyles in the IDE. It's simple and it works.
Thank you for this.
Do you see any reason I wouldn't be able to rejig this to be compatible with VBA (64bit)? I note that the routine for setting the tooltips requires a control with a hWnd (or a hWnd, at least).
Please include and apply a manifest and compile to see the effect of tooltips.
I go back again.
I see double tips (with resource file applied, mouse on the 'Command Button' and waiting for tooltip then go to CheckBox). Please investigate.
Hello, MountainMan!
FYI, I'm using your class in my app, and I added this little piece of code to stop the class from eating up GDI-Objects.
Code:
Private Declare Function DestroyWindow Lib "user32.dll" (ByVal hWnd As Long) As Long
Private Sub Class_Terminate()
Dim n%
For n = 0 To UBound(Controls)
DestroyWindow Controls(n).TThWnd
Next
End Sub
Greetings
Seniorchef
Last edited by Seniorchef; Mar 7th, 2023 at 06:51 AM.
What makes you think those controls actually have a hWnd? You didn't offer any information about what controls you are using but if they do have a hWnd, it is trivial to obtain via different methods. You could use Spy++ to find information about their window class and then use FindWindow(). You could also try WindowFromPoint() to get the hWnd of a window at a certain position.
SendMessageLongW Controls(CtrlIndex).TThWnd, TTM_UPDATETIPTEXTW, 0&, VarPtr(ti)??
show tooltip text by this api?
can you help me ,why my api can't show tooltip?
Code:
Private Declare Function ShowToolTip Lib "user32" Alias "ShowWindow" ( _
ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Dim hToolTip As Long
Private Sub Command1_Click()
hToolTip = CreateToolTip(0, "ToolTip", "tooltip window", 0, 0, 0, 300, 50, Me.hwnd, 0, App.hInstance, ByVal 0&)
SetToolTipText hToolTip, "tooltip text"
ShowToolTip hToolTip, 1 '1????
End Sub