如何增加工具提示矩形大小 [英] How to increase ToolTip Rectangle size

查看:15
本文介绍了如何增加工具提示矩形大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的问题是矩形的高度.

剪辑:

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

代码:

使用系统;使用 System.Collections.Generic;使用 System.ComponentModel;使用 System.Drawing;使用 System.Data;使用 System.Drawing.Drawing2D;使用 System.Linq;使用 System.Text;使用 System.Threading.Tasks;使用 System.Windows.Forms;命名空间 Discounting.Module{公共部分类优点:UserControl{公共福利(){初始化组件();}private void ToolTip1_Draw(对象发送者,DrawToolTipEventArgs e){var newEventArgs = new DrawToolTipEventArgs(e.图形,e.关联窗口,e.AssociatedControl,e.Bounds, e.ToolTipText,this.BackColor,this.ForeColor,字体);DrawToolTip(e);}private void DrawToolTip(DrawToolTipEventArgs e){使用 (var sf = new StringFormat()){sf.LineAlignment = StringAlignment.Center;sf.Alignment = StringAlignment.Center;使用(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));//阴影层graphics.DrawString(e.ToolTipText, new Font("Aerial",12.0f, FontStyle.Bold), Brushes.Black,new PointF(linearGradientBrush.Rectangle.X + 5, linearGradientBrush.Rectangle.Y + 5));//顶层linearGradientBrush.Dispose();}}}private void ToolTip2_Draw(对象发送者,DrawToolTipEventArgs e){DrawToolTip(e);}private void ToolTip3_Draw(对象发送者,DrawToolTipEventArgs e){DrawToolTip(e);}private void ToolTip4_Draw(对象发送者,DrawToolTipEventArgs e){DrawToolTip(e);}}}

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

解决方案

好吧,因为在混合 TextRenderer 时可能会有一些怪癖图形对象,下面是一个例子:

TextFormatFlags toolTipFlags = TextFormatFlags.VerticalCenter |TextFormatFlags.LeftAndRightPadding |TextFormatFlags.Horizo​​ntalCenter |TextFormatFlags.NoClipping;字体 toolTipFont = new Font("Arial", 12.0f, FontStyle.Bold);private void toolTip1_Popup(object sender, PopupEventArgs e){string toolTipText = (发送者为工具提示).GetToolTip(e.AssociatedControl);使用 (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){使用 (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);}

I am currently implementing a tooltip which has at least two sentences worth inside of it, so I need to somehow create a large rectangle which would hold it.

My issue is the height of the rectangle.

Snip:

As you can see the green rectangle does not have the required size.

Code:

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);
        }
    }
}

If you require further details I would be happy to provide them.

解决方案

Well, since there might be some quirks when mixing TextRenderer and the Graphics object, here's an example:

The ToolTip.PopUp event provides means to set the Size of the ToolTip rectangle. You just need to measure the Text and set its PopupEventArgs.ToolTipSize property to the measured Size.
This allows to use multi-line strings as well, using Environment.NewLine to separate the lines.

The PopupEventArgs object doesn't provide a Graphics object that can be use to measure the Text. We can use TextRenderer.MeasureText instead.

TextRenderer.MeasureText is very precise: it will give back the exact measure of the Text. Since you are using Graphics.DrawString to draw the Text, we better be generous and add some more space to the measured Width, to avoid text wrapping and also because the Text looks better if the container rectangle is not too tight.
In the Popup event, after measuring the Text, I'm adding 5 pixels to both the Width and Height (Size.Add([Measured Size], new Size(5, 5))). Modify as required

Note:
Here, the Font family and Size are hard-coded. Of course you may want to use a more dynamic Font object, possibly linked to a property of your UserControl. The Font can be changed at any time: the PopUp event will use it to measure the test bounds.

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);
}

这篇关于如何增加工具提示矩形大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆