Per Erik Strandberg /cv /kurser /blog

After messing a lot with tool tips and reading some blogs I think I've made a minimal example that illustrates how to customize the tool tip by just hooking into the ToolTip.Popup and ToolTip.Draw events.

    this.m_toolTip.Popup += new PopupEventHandler(toolTip1_Popup);
    this.m_toolTip.Draw += new DrawToolTipEventHandler(toolTip1_Draw);

While running it looks a little something like this:
http://www.pererikstrandberg.se/blog/code/customized-tooltip.png

One really important property is OwnerDraw that according to [1] the property Gets or sets a value indicating whether the ToolTip is drawn by the operating system or by code that you provide. So set it to true.

    this.m_toolTip.OwnerDraw = true;

The event handlers I wrote (copied from the web) resize the tool tip and draws something different than the tooltip, so the OwnerDraw property of the tool tip must be true.

        void toolTip1_Popup(object sender, PopupEventArgs e)
        {
            SizeF size = e.AssociatedControl.CreateGraphics().MeasureString(
                m_toolTip.GetToolTip(e.AssociatedControl),
                m_font);
            e.ToolTipSize = new Size(
                Convert.ToInt32(size.Width), 
                Convert.ToInt32(size.Height));
        }

        void toolTip1_Draw(object sender, DrawToolTipEventArgs e)
        {
            DrawToolTipEventArgs newArgs = new DrawToolTipEventArgs(e.Graphics,
                e.AssociatedWindow, e.AssociatedControl, e.Bounds, e.ToolTipText,
                m_toolTip.BackColor, m_toolTip.ForeColor, m_font);

            newArgs.DrawBackground();
            newArgs.DrawBorder();
            newArgs.DrawText(TextFormatFlags.TextBoxControl);
        }

Download code here [2] and executable here: [3]


See also Csharp Tool Tip Test, Csharp Override Tool Tip Test and Csharp Data Grid View And Tool Tip Test.


Tillhör Kategori Programmering