Introduction
There are a lot of articles about ToolTip controls. However, all of them do not support displaying a title and an icon. So I am sharing my ToolTip control named ScToolTip
which supports displaying of a title and an icon.
Using the Control
To use the control, you just drag the control onto the form from the Visual Studio .NET toolbox. Then each control on the form will have three new properties that you can set for the particular object. Visual Studio will add the additional code automatically to support displaying tooltip for controls.
Points of Interest
The ScToolTip
class is declared as follows:
[
ProvideProperty("ToolTip",typeof(Control)),
ProvideProperty("TipTitle",typeof(Control)),
ProvideProperty("IconType", typeof(Control)),
]
public class ScToolTip : Component,IExtenderProvider
To display different titles and icons, you must fill the TOOLINFO
structure like this:
TOOLINFO toolinfo = new TOOLINFO();
toolinfo.hwnd = handle;
toolinfo.uFlags = TTF_IDISHWND;
toolinfo.uId = control.Handle;
toolinfo.lpszText = LPSTR_TEXTCALLBACKW;
When the owner window accepts the TTN_NEEDTEXT
message, we do this:
if (m.Msg == NativeMethods.WM_NOTIFY)
{
NativeMethods.NMHDR hdr =
(NativeMethods.NMHDR)
Marshal.PtrToStructure(m.LParam,
typeof(NativeMethods.NMHDR));
if (hdr.code == NativeMethods.TTN_NEEDTEXT)
{
Control control =
Control.FromHandle(new IntPtr(hdr.idFrom));
if (control != null)
{
string text = GetToolTip(control);
string title = GetTipTitle(control);
int icon = (int)GetIconType(control);
NativeMethods.SendMessage(new HandleRef(this,
this.Handle),NativeMethods.TTM_SETTITLE,icon,title);
NativeMethods.NMTTDISPINFO info =
(NativeMethods.NMTTDISPINFO)
Marshal.PtrToStructure(m.LParam,
typeof(NativeMethods.NMTTDISPINFO));
info.lpszText = text;
Marshal.StructureToPtr(info,m.LParam,true);
}
}
}
References
This article was inspired by wilsone8's article "Building a Ballontooltip provider in C#".