I am trying to create a custom tooltip for a TreeView control. The tooltip has a draw event and a popup event. During the popup event, the tooltip container is resized to handle the text which is a larger font. The tooltip works fine as long as the mouse is NOT over TreeNode text. However, as soon as the mouse moves over a node, the popup event is not called, and the tooltip is not sized correctly, truncating the text.
Some of the code:
Initializing the tooltip
// Creating the Custom Tooltip
m_toolTip = new ToolTip();
m_toolTip.InitialDelay = 1000; // duration of mouse hover before tooltip is shown
m_toolTip.ReshowDelay = 1000; // duration of mouse hover over 2nd control before tooltip is shown
m_toolTip.AutoPopDelay = 20000; // duration tool tip is shown
m_toolTip.OwnerDraw = true;
m_toolTip.ShowAlways = true;
m_toolTip.Draw += new DrawToolTipEventHandler(toolTip_Draw);
m_toolTip.Popup += new PopupEventHandler(toolTip_Popup);
// TreeView tooltips should not display
myTreeView1.ShowNodeToolTips = false;
Drawing the custom tooltip
// Handles drawing the ToolTip.
private void toolTip_Draw(System.Object sender,
System.Windows.Forms.DrawToolTipEventArgs e)
{
// Draw a custom background and text if the ToolTip is for treeview.
if (e.AssociatedControl == myTreeView1)
{
// Draw the custom background.
e.Graphics.FillRectangle(SystemBrushes.Info, e.Bounds);
// Draw the standard border.
e.DrawBorder();
// Draw the custom text.
// The using block will dispose the StringFormat automatically.
using (StringFormat sf = new StringFormat())
{
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.None;
sf.FormatFlags = StringFormatFlags.NoWrap;
using (Font f = new Font("Microsoft Sans Serif", 12))
{
e.Graphics.DrawString(e.ToolTipText, f,
SystemBrushes.InfoText, e.Bounds, sf);
}
}
}
}
Handling tooltip resize on Popup - This does not get called when mouse is over TreeNode, and text is therefore truncated.
// Determines the correct size for the treeview ToolTip.
private void toolTip_Popup(object sender, PopupEventArgs e)
{
if (e.AssociatedControl == myTreeView1)
{
using (Font f = new Font("Microsoft Sans Serif", 12))
{
// Get tool tip text and add space to make sure the last character does not get trunctated
string tipText = m_toolTip.GetToolTip(e.AssociatedControl) + " ";
e.ToolTipSize = TextRenderer.MeasureText(tipText, f);
}
}
}
MouseMove event that sets tooltip (from Microsoft example)
private void myTreeView1_MouseMove(object sender, MouseEventArgs e)
{
// If the node is null, then the mouse is not over a node
// and the tool tip is set to be empty
if (node == null)
{
m_toolTip.Hide(myTreeView1);
m_toolTip.Active = false;
m_toolTip.SetToolTip(myTreeView1, string.Empty);
}
else
{
// Set the tool tip to the Text of the node so that if a mouse hover
// triggers the tool tip, the node's text is displayed.
string nodeTip = node.Text;
string currentTip = m_toolTip.GetToolTip(myTreeView1);
// Only change the tool tip if the node text is populated
// and it isn't already set in the tool tip
if (nodeTip != null)
{
if ((!m_toolTip.Active) || (!nodeTip.Equals(currentTip)))
{
if (!nodeTip.Equals(currentTip))
{
m_toolTip.Hide(myTreeView1);
}
m_toolTip.Active = false;
m_toolTip.SetToolTip(myTreeView1, nodeTip);
m_toolTip.Active = true;
}
}
}
}
If I set ShowNodeToolTips = true, then it seems to temporarily work correctly, but eventually crashes as described in this support document:
http://support.microsoft.com/default.aspx/kb/953102
If there is another way to accomplish custom draw tooltips in a treeview, I would appreciate any help in how to get this working.