Tooltip Text going off screen (WinForms)

Viewed 288

I've drawn on a Tool Tip control - The code is below.

private void toolTip1_Popup(object sender, PopupEventArgs e)
        {
            ToolTip tt = (sender as ToolTip);
            string toolTipText = tt.GetToolTip(e.AssociatedControl);
            e.ToolTipSize = TextRenderer.MeasureText(toolTipText, new Font("Arial", 16.0f));
        }
private void toolTip1_Draw(object sender, DrawToolTipEventArgs e) => DrawToolTip(sender, e);

        [System.Runtime.InteropServices.DllImport("User32.dll")]
        static extern bool MoveWindow(IntPtr h, int x, int y, int width, int height, bool redraw);
        private void DrawToolTip(object sender, DrawToolTipEventArgs e)
        {
            ToolTip tt = (sender as ToolTip);
            string toolTipText = tt.GetToolTip(e.AssociatedControl);
            Font f = new Font("Arial", 16.0f);
            e.DrawBackground();
            e.DrawBorder();
            toolTipText = e.ToolTipText;
            e.Graphics.DrawString(e.ToolTipText, f, Brushes.Black, new PointF(2, 2));
        }

And then to set the ToolTip text:

toolTip1.SetToolTip(btnLogin, "Some text....."); 

Additionally, here is an image of what is happening.

It looks alright, but it has come to my attention that if the text is a certain length, the tooltip will go off-screen. Is there anyway to prevent that? I would rather not have to add Environment.NewLine(); or \n etc, since I would need to do that for MANY strings.

1 Answers

If I understand your question correctly, you are trying to combine the two solutions posted here and here to mainly resize the ToolTip window with the screen width, and move it to Point(2, 2), screen coordinates.

If that is what you need, you need to modify the source codes a bit to set the right e.ToolTipSize in the Popup event, and as the gentlemen commented above, draw the string in a rectangle, the e.Bounds property in the Draw event.

using System;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public partial class Form1 : Form
{
    TextFormatFlags flags = TextFormatFlags.VerticalCenter
    | TextFormatFlags.Left
    | TextFormatFlags.LeftAndRightPadding
    | TextFormatFlags.NoClipping
    | TextFormatFlags.WordBreak;

    public Form1()
    {
        InitializeComponent();
    }

    private void toolTip1_Popup(object sender, PopupEventArgs e)
    {
        var tt = sender as ToolTip;
        var toolTipText = tt.GetToolTip(e.AssociatedControl);
        var screen = Screen.FromControl(e.AssociatedControl).WorkingArea;

        using (var g = e.AssociatedControl.CreateGraphics())
        using (var font = new Font("Arial", 16))
        {
            var sz = TextRenderer.MeasureText(
                g, toolTipText, font, screen.Size, flags);

            e.ToolTipSize = new Size(screen.Width - 2, sz.Height + 10);
        }
    }

    private void toolTip1_Draw(object sender, DrawToolTipEventArgs e)
    {
        var t = sender as ToolTip;
        var h = (IntPtr)t.GetType().GetProperty("Handle",
            BindingFlags.NonPublic | BindingFlags.Instance).GetValue(t);

        MoveWindow(h, 2, 2, e.Bounds.Width - 2, e.Bounds.Height, false);

        e.DrawBackground();
        e.DrawBorder();

        using (var font = new Font("Arial", 16))
            TextRenderer.DrawText(e.Graphics, e.ToolTipText, font, 
                e.Bounds, Color.Black, flags);
    }

    [DllImport("User32.dll")]
    static extern bool MoveWindow(IntPtr h, int x, int y, 
        int width, int height, bool redraw);

}

SOQ65526197

Related