如何增加工具提示矩形大小

Art*_*ias 4 .net c# graphics tooltip winforms

我目前正在实现一个工具提示,其中至少有两个句子值得,所以我需要以某种方式创建一个大矩形来容纳它。

我的问题是矩形的高度。

剪辑:

在此处输入图片说明

如您所见,绿色矩形没有所需的大小。

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Discounting.Module
{
    public partial class Benefits : UserControl
    {
        public Benefits()
        {
            InitializeComponent();
        }

        private void ToolTip1_Draw(object sender, DrawToolTipEventArgs e)
        {
            var newEventArgs = new DrawToolTipEventArgs(
                e.Graphics,
                e.AssociatedWindow,
                e.AssociatedControl,
                e.Bounds, e.ToolTipText,
                this.BackColor,
                this.ForeColor,
                Font);

            DrawToolTip(e);
        }

        private void DrawToolTip(DrawToolTipEventArgs e)
        {
            using (var sf = new StringFormat())
            {
                sf.LineAlignment = StringAlignment.Center;

                sf.Alignment = StringAlignment.Center;


                using (var graphics = e.Graphics)
                {

                    var linearGradientBrush = new LinearGradientBrush(new Rectangle(e.Bounds.X, e.Bounds.Y,
                        8000, 1000), Color.GreenYellow, Color.MintCream, 45f);

                    graphics.FillRectangle(linearGradientBrush, linearGradientBrush.Rectangle);

                    graphics.DrawString(e.ToolTipText, new Font("Aerial",12.0f, FontStyle.Bold), Brushes.Silver,
                        new PointF(linearGradientBrush.Rectangle.X + 6, linearGradientBrush.Rectangle.Y + 6)); // shadow layer
                    graphics.DrawString(e.ToolTipText, new Font("Aerial",12.0f, FontStyle.Bold), Brushes.Black,
                        new PointF(linearGradientBrush.Rectangle.X + 5, linearGradientBrush.Rectangle.Y + 5)); // top layer

                    linearGradientBrush.Dispose();
                }
            }
        }

        private void ToolTip2_Draw(object sender, DrawToolTipEventArgs e)
        {
            DrawToolTip(e);
        }

        private void ToolTip3_Draw(object sender, DrawToolTipEventArgs e)
        {
            DrawToolTip(e);
        }

        private void ToolTip4_Draw(object sender, DrawToolTipEventArgs e)
        {
            DrawToolTip(e);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您需要更多详细信息,我很乐意提供。

Jim*_*imi 6

好吧,因为在混合TextRendererGraphics对象时可能会有一些怪癖,这里有一个例子:

所述ToolTip.PopUp事件提供手段来设定工具提示矩形的大小。您只需要测量 Text 并将其PopupEventArgs.ToolTipSize属性设置为测量的大小。
这也允许使用多行字符串,使用Environment.NewLine分隔行。

PopupEventArgs对象不提供Graphics可用于测量文本的对象。我们可以使用TextRenderer.MeasureText代替。

TextRenderer.MeasureText非常精确:它将返回文本的精确度量。由于您使用的Graphics.DrawString是绘制文本,我们最好大方一些,并为测量的宽度添加更多空间,以避免文本环绕,并且如果容器矩形不太紧,文本看起来会更好。
在 Popup 事件中,在测量 Text 之后,我向 Width 和 Height 都添加了 5 个像素(Size.Add([Measured Size], new Size(5, 5)))。根据需要修改

注意
这里,字体系列和大小是硬编码的。当然,您可能想要使用更动态的 Font 对象,可能链接到您的UserControl. Font 可以随时更改:PopUp事件将使用它来测量测试边界。

工具提示自定义绘图

TextFormatFlags toolTipFlags = TextFormatFlags.VerticalCenter | 
    TextFormatFlags.LeftAndRightPadding | TextFormatFlags.HorizontalCenter | TextFormatFlags.NoClipping;
Font toolTipFont = new Font("Arial", 12.0f, FontStyle.Bold);

private void toolTip1_Popup(object sender, PopupEventArgs e)
{
    string toolTipText = (sender as ToolTip).GetToolTip(e.AssociatedControl);
    using (var g = e.AssociatedControl.CreateGraphics()) {
        var textSize = Size.Add(TextRenderer.MeasureText(
            g, toolTipText, toolTipFont, Size.Empty, flags), new Size(10, 5));
        e.ToolTipSize = textSize;
    }
}

private void toolTip1_Draw(object sender, DrawToolTipEventArgs e) => DrawToolTip(e);

private void DrawToolTip(DrawToolTipEventArgs e)
{
    using (var linearGradientBrush = new LinearGradientBrush(e.Bounds, Color.GreenYellow, Color.MintCream, 45f)) {
        e.Graphics.FillRectangle(linearGradientBrush, e.Bounds);
    }

    var shadowBounds = new Rectangle(new Point(e.Bounds.X + 1, e.Bounds.Y + 1), e.Bounds.Size);

    TextRenderer.DrawText(e.Graphics, e.ToolTipText, toolTipFont, shadowBounds, Color.LightGray, toolTipFlags);
    TextRenderer.DrawText(e.Graphics, e.ToolTipText, toolTipFont, e.Bounds, Color.Black, toolTipFlags);
}
Run Code Online (Sandbox Code Playgroud)