The idea of this thread is a tutorial on how to create your own custom tooltips since the standard tooltip is pretty limited...it doesn't even allow for multiline text.
Each project added to this thread will have more functionality than the previous project.
This will build up to a full blown graphical custom tool tip demo with features such as multiline text, icons, shading and graphical styles.
Woka
Last edited by Wokawidget; Jul 12th, 2004 at 10:17 AM.
Instead of letting windows create and draw the tooltip, we subclass the tooltip window and trap for the WM_PAINT message. When this arrives we create our own drawing area, and manually draw text and graphics to make up the tooltip.
New features:
ToolTip can now display multiline strings.
Woka
Last edited by Wokawidget; Jul 12th, 2004 at 10:20 AM.
BallonColor and Text color properties have now been added to the ToolTip class. When we manually draw the tooltip we can specify the colors to be used.
this is really cool.. how is it regarding performance?
1 suggestion.. since you create a collection of tooltips.. perhaps you could create a global settings object... where you could set everything but the text and maybe optional icon of the tooltip and that would be applied to ALL tooltips added to project, this would save the user from having to call their own sub to set the properties for each tool tip, maybe a class called Defaults that is in mobjTips so if they dont set a property it uses the default one they specified? just an idea...
Right, if you have downloaded Stage 2 and Stage 3, and looked at the code, you will notice that the manual drawing of the tooltip is quite "messy"...Lots of constants flying about, not to mention all those evil API commands.
If things stay like this, and we keep adding to the drawing code then sooner or later it will become so much of a mess that it will be very hard to add to it easily.
This can be solved by creating a new class, clsDraw. This wraps up all this horrible API code and just has some simple methods exposed so that the drawing is made way easier.
No new features have been implemented at this stage.
As suggested by kleinma I have added the ability to create styles.
The project now as 3 new classes:
ToolTipStyle
TextStyle
BalloonStyle
The class ToolTipStyle holds references to the TextStyle and BalloonStyle classes.
So, for example, to get the TextColor we now do:
VB Code:
Dim objStyle As ToolTipStyle
Set objStyle = New ToolTipStyle
MsgBox objStyle.TextStyle.Color
Set objStyle = Nothing
The reason I have written it like this is because over the next few days we are going to add many many properties to the project that allows you to change the appearance of the ToolTip, some related to text, some related to the balloon.
Since these properties are for different things I thought I'd create a class to hold them. This makes things slightly easier in the long run. I admitt it's a little overkill for the color properties we have now, but you will soon see the benefits of this structure.
New features:
Ability to create and set styles to individual tooltips
I have added a few properties for both the balloon style and text style.
The balloon can now have rounded corners. The level of rounding can be set by the property CurveIndex in the BalloonStyle class. If CurveIndex is 0 then a square balloon is drawn. By default this is set to 7.
Margin properties have been added to the text style class. This allows you to specify the gap between the edge of the balloon and the text. All 4 margins, Top, Bottom, Left and Right, are available to change by the user. By default ALL margins are set to 5.
New Feautures:
Add rounded corners to balloon
Specify margin gaps to position the text in the balloon
A new class has been added, Shadow. This holds properties related to a shadow, ie X and Y offsets, color and whether it's visible or not.
The shadow class is used by the text style class and the balloon style class.
You'll notice that while drawing the tooltip, there is a little bit more work to be done to work out the position of grafix that are drawn to the screen. This is the only really messy thing that we have to do now, but it's unavoidable.
At this stage I have added font attributes to the TextStyle class.
These are used when drawing text onto our tooltip.
New Features:
Font Type
Font Size
Bold
Italic
Underline
We now have a fully functional method of creating custom tooltips.
We can create nice tips, like the one I like in this example (not sure about the bold though), or we can create evil tips that look crap, like the 2 buttons in the middle.
It's all about what properties you set.
if you don't create a style then the default style is used. This is demonstrated by the 1st button of the form.
We could leave it at this...but an icon would be nice don't you think?
The next stage will have the ability to add an icon to the tooltip.
The ballon class now has a new property, Image.
An image can be set by using:
VB Code:
objTip.Style.BalloonStyle.Image = Image1.Picture
If a background image is specified then this overrides the background color.
I have also changed the demo app.
This now incorporates a listview and allows you to set tooltips for individual listitems, and for the listview itself if no item is selected.
I am not sure if this would be a widely used feature, but I added it in because I could, and because I have just fixed a bug in the drawing code
The bug was in the clsDraw class. I was creating the HDC, setting it's background to Transparent, and then moving it to the desired position. This caused a few pixels to be out of place in the top right hand corner of the tool tip.
The clsDraw class has been changed so that it moves the window 1st, then starts the painting on the hDC.
This bug is in all previous versions posted here. To rectify this you can download the zip attached to this post and add the clsDarw class to previous versions. Then in the drawtooltip sub, in modDrawToolTip, the old code was:
This is really good stuff, Wokawidget. Very nice work.
One other feature I could use is being able to set the location of the ToolTip balloon. I found where the cursor position is controlled by the two constants CURSOR_OFFSET_X and CURSOR_OFFSET_Y in the modDrawToolTip module, but that is about as far I have been able to get.
What can we do to make these two constants into variables and make them accessable?
Sorry for the late reply ... I had forgotten about this post ...
No, nothing like a CTD (crash to desktop), the control works fine on XP, but on 2000 machines, nothing appears at all. It is very strange. While I have discovered this on my own program, I have not yet tested it on a 2000 machine using your samples. I will do so and let you know...
I have done some further testing based upon Divot's comments.
Before I proceed, I would also like to thank you for developing the multiline code.
I have tested the code downloaded from this thread under windows 2000 and no tooltips appear at all during normal running.
I have also tested another application (that has tooltip code integrated) under windows 2000 - with the same result of no tooltip showing.
When the code from both applications is tested under XP it works without a problem.
Further information:
I had noticed when testing under windows 2000, if the screensaver activates and the mouse is moved to recover the screen, the tooltip does appear briefly as the application window is redrawn. But then vanishes, not to be seen until returning from a mouse move after the screensaver loads again. (During normal operation of the applications under windows 2000 there are no multiline tooltips displayed.)
(I have made sure that in each instance of testing, that the mouse is over the multiline active segment of the application.)
Cheers. I have always always intended to write .NET code for all my apps in my sig. But with my new job and moving house etc I haven't had time over the last 6 months.
I know it's lazy of me, I should realy get it done.
ths is because it uses the control name for a key, and the control name is the same for all controls in an array. U need to modify the code to change the key it's saving in the collection when adding a tooltip to a control.
I notice when I'm testing my project (in evironment mode) and the program initiates one of the tool tip text options, whenever I End the project from the standard toolbar that my project disappears and I have to re-load the project - any ideas?.
ths is because it uses the control name for a key, and the control name is the same for all controls in an array. U need to modify the code to change the key it's saving in the collection when adding a tooltip to a control.
Woka
Hi Woka ,
Thanks your work of this, Can you guide me how to save the control array to the collection ?
Thanks woka :-)
Where there is no hope, there can be no endeavor.
There are two ways of rising in the world, either by your own industry or by the folly of others.
Wokawidget,you've done a fantastic job by posting the codes for creating custom tooltip.You effort is worthy of the highest praise.It's because of such greatness that developer's forums have become all the more popular.
I hate downloading codings sans thanking the author,wherever it's possible.
I came across this thread in a search for multi-line tooltips. Firstly I'd like to say what a useful feature this is to have, but I have one small problem. I wanted to use it to added tooltips to image controls to replace the tooltip feature of the image. It seems the image control doesn't have and hWnd property associated with it.