简体   繁体   English

工具提示文本离开屏幕 (WinForms)

[英]Tooltip Text going off screen (WinForms)

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:然后设置 ToolTip 文本:

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.看起来不错,但我注意到如果文本有一定长度,工具提示将 go 离屏。 Is there anyway to prevent that?有没有办法阻止这种情况? I would rather not have to add Environment.NewLine();我宁愿不必添加 Environment.NewLine(); or \n etc, since I would need to do that for MANY strings.或 \n 等,因为我需要为许多字符串执行此操作。

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.如果我正确理解您的问题,您正在尝试结合此处此处发布的两种解决方案,主要调整ToolTip window 的屏幕宽度,并将其移动到Point(2, 2) ,屏幕坐标。

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.如果这是你需要的,你需要稍微修改一下源代码,在Popup事件中设置正确的e.ToolTipSize ,并如上面先生评论的那样,将字符串绘制在一个矩形中,即Draw中的e.Bounds属性事件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM