在Windows窗体(C#)中,是否有方法设置工具提示';s的高度和宽度是动态的

在Windows窗体(C#)中,是否有方法设置工具提示';s的高度和宽度是动态的,c#,C#,在Windows窗体(C#)中,有一种方法可以动态设置工具提示的高度和宽度(在代码中的含义)。我正在使用DataViewGrid控件,因此我必须使用Show方法。然而,我注意到(当让它自己的设备使用时),工具提示控件并不总是根据提供的内容进行调整 例如: 将工具提示控件添加到表单(称为ttText),然后让它首先显示文本: ttText.Show("I'm hungry\nand waiting!"); 将截断下一个呼叫: ttText.Show("Well, too bad -- so

在Windows窗体(C#)中,有一种方法可以动态设置工具提示的高度和宽度(在代码中的含义)。我正在使用DataViewGrid控件,因此我必须使用Show方法。然而,我注意到(当让它自己的设备使用时),工具提示控件并不总是根据提供的内容进行调整

例如:

将工具提示控件添加到表单(称为ttText),然后让它首先显示文本:

ttText.Show("I'm hungry\nand waiting!");  
将截断下一个呼叫:

ttText.Show("Well, too bad -- so much for your stamina, you should not be here!\nSo the little bear responds!");
有什么想法吗

请记住,DataGridView需要一种显示工具提示帮助的机制,因此需要使用Show方法,我在非DataViewGrids的其他地方看到过这种行为

下面是一个代码示例: 使用System.Collections.Generic; 使用系统图; 使用System.Linq; 使用System.Windows.Forms

namespace TestForm
{
    class Form1 : Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.ttText = new System.Windows.Forms.ToolTip(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGridView1
            // 
            this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Location = new System.Drawing.Point(13, 19);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(453, 321);
            this.dataGridView1.TabIndex = 0;
            this.dataGridView1.CellMouseEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_MouseCellEnter);
            // 
            // ttText
            // 
            this.ttText.AutomaticDelay = 60;
            this.ttText.AutoPopDelay = 600000;
            this.ttText.InitialDelay = 60;
            this.ttText.IsBalloon = true;
            this.ttText.ReshowDelay = 60;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(478, 352);
            this.Controls.Add(this.dataGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);

        }


        private System.Windows.Forms.DataGridView dataGridView1;
        private System.Windows.Forms.ToolTip ttText;

        public Form1()
        {
            InitializeComponent();

            var ds = Sayings().ToList();

            dataGridView1.DataSource = ds;
        }

        public List<dynamic> Sayings()
        {
            return new List<dynamic>
            {
                new 
                { 
                    Human = "I'm hungry\nand waiting!",
                    BabyBear = "Well, too bad -- so much for your stamina, you should not be here!\nSo the little bear responds!"
                }
            };
        }

        private void dataGridView1_MouseCellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != -1 && e.RowIndex != -1)
            {
                var rect = dataGridView1.GetColumnDisplayRectangle(e.ColumnIndex, true);
                var left = rect.Left + (int)(rect.Width * .5f);
                var top = rect.Top;

                Point displayPoint = new Point(left + this.ClientRectangle.Left, top + this.ClientRectangle.Top + 40);

                ttText.Show(dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString(), this, displayPoint);
            }
        }
    }
}
名称空间测试表单
{
类别表格1:表格
{
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.components=new System.ComponentModel.Container();
this.dataGridView1=new System.Windows.Forms.DataGridView();
this.ttText=new System.Windows.Forms.ToolTip(this.components);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
这个.SuspendLayout();
// 
//dataGridView1
// 
this.dataGridView1.Anchor=((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
|系统、窗口、窗体、主播样式(左)
|System.Windows.Forms.AnchorStyles.Right);
this.dataGridView1.columnHeadershightSizeMode=System.Windows.Forms.datagridviewColumnHeadershightSizeMode.AutoSize;
this.dataGridView1.Location=新系统.Drawing.Point(13,19);
this.dataGridView1.Name=“dataGridView1”;
this.dataGridView1.Size=新系统.Drawing.Size(453321);
this.dataGridView1.TabIndex=0;
this.dataGridView1.CellMouseEnter+=新系统.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1\u MouseCelenter);
// 
//ttText
// 
this.ttText.AutomaticDelay=60;
this.ttText.autocopdelay=600000;
this.ttText.InitialDelay=60;
this.ttText.IsBalloon=true;
this.ttText.ReshowDelay=60;
// 
//表格1
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统.Drawing.Size(478352);
this.Controls.Add(this.dataGridView1);
this.Name=“Form1”;
this.Text=“Form1”;
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
此选项为.resume布局(false);
}
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.ToolTip ttText;
公共表格1()
{
初始化组件();
var ds=Sayings().ToList();
dataGridView1.DataSource=ds;
}
公共语录
{
返回新列表
{
新的
{ 
Human=“我饿了,还在等着!”,
BabyBear=“好吧,太糟糕了——你的耐力太差了,你不应该在这里!\n所以小熊回答说!”
}
};
}
私有void dataGridView1_MouseCellEnter(对象发送方,DataGridViewCellEventArgs e)
{
如果(e.ColumnIndex!=-1&&e.RowIndex!=-1)
{
var rect=dataGridView1.GetColumnDisplayRectangle(例如ColumnIndex,true);
var left=矩形左+(int)(矩形宽度*.5f);
var-top=rect.top;
点显示点=新点(左+this.ClientRectangle.left,顶部+this.ClientRectangle.top+40);
ttText.Show(dataGridView1[e.ColumnIndex,e.RowIndex].Value.ToString(),this,displayPoint);
}
}
}
}

如果您将鼠标悬停在第一列,那么第二列,工具提示文本将被截断。

其中提到使用RenderSize属性进行此类操作,但有一些警告。

好的,我发布此问题已经好几个月了。从那时起,我就没有真正考虑过这个问题——只是接受了这个行为。。。直到今晚

由于没有其他人想到答案(或愿意提供另一种窗口形式的想法),我想到了重新审视一般问题。气球工具提示似乎是一种自由形式的东西。。。它采用它遇到的先前形状。这可能是微软在工作中的懒惰。或者更好——这是微软为开发者指明不同方向的方式。毕竟,提供的唯一新方向是wpf

然而,正如我在最初的问题中所说的,我需要的解决方案只是windows窗体,而不是将WPF、Silverlight或其他技术混合到锅中。毕竟,,
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace TestForm
{
    class Form1 : Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.ttText = new System.Windows.Forms.ToolTip(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGridView1
            // 
            this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Location = new System.Drawing.Point(17, 23);
            this.dataGridView1.Margin = new System.Windows.Forms.Padding(4);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(604, 395);
            this.dataGridView1.TabIndex = 0;
            this.dataGridView1.CellMouseEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_MouseCellEnter);
            this.dataGridView1.MouseLeave += new System.EventHandler(this.dataGridView1_MouseLeave);
            this.dataGridView1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.event_MouseMove);
            // 
            // ttText
            // 
            this.ttText.AutomaticDelay = 0;
            this.ttText.AutoPopDelay = 0;
            this.ttText.InitialDelay = 10;
            this.ttText.IsBalloon = true;
            this.ttText.OwnerDraw = true;
            this.ttText.ReshowDelay = 0;
            this.ttText.ShowAlways = true;
            this.ttText.UseAnimation = false;
            this.ttText.UseFading = false;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(637, 433);
            this.Controls.Add(this.dataGridView1);
            this.Margin = new System.Windows.Forms.Padding(4);
            this.Name = "Form1";
            this.Text = "Form1";
            this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.event_MouseMove);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);

        }

        private System.Windows.Forms.DataGridView dataGridView1;
        private System.Windows.Forms.ToolTip ttText;

        public Form1()
        {
            InitializeComponent();

            dataGridView1.ShowCellToolTips = false;
            var ds = Sayings().ToList();

            dataGridView1.DataSource = ds;
        }


        public List<dynamic> Sayings()
        {
            return new List<dynamic>
            {
                new 
                { 
                    Human = "I'm hungry\nand waiting!",
                    BabyBear = "Well, too bad -- so much for your stamina, you should not be here!\nSo the little bear responds!"
                },
                new 
                { 
                    Human = "What a selfish bear!\n\n\nAt least you could do is wait for\nothers to join you!",
                    BabyBear = "Boo Hoo!"
                },
                new 
                { 
                    Human = "Oh, I'm sorry!",
                    BabyBear = "Now, I'm going to eat you!"
                },
                new 
                { 
                    Human = "\n\n\n!!!\n\nWhat?????\n\n\n\nI don't think so!\n\n(Human pulls out Honey Jar)",
                    BabyBear = "Yum!"
                },

            };
        }

        private void dataGridView1_MouseCellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != -1 && e.RowIndex != -1)
            {
                this.SuspendLayout();

                var rectC = dataGridView1.GetColumnDisplayRectangle(e.ColumnIndex, true);
                var left = rectC.Left + (int)(rectC.Width * .5f);

                var rectR = dataGridView1.GetRowDisplayRectangle(e.RowIndex, true);
                var top = (rectR.Top + (int)(rectR.Height * .5f));

                Point displayPoint = new Point(left + this.ClientRectangle.Left, top + this.ClientRectangle.Top + 40);

                var column = e.ColumnIndex;
                var row = e.RowIndex;

                for (int i = 0; i < 5; ++i)
                {
                    ttText.Show(dataGridView1[column, row].Value.ToString(), this, displayPoint);
                    ttText.Hide(this);
                }
                ttText.Show(dataGridView1[column, row].Value.ToString(), this, displayPoint);

                this.ResumeLayout();
            }
        }

        private void dataGridView1_MouseLeave(object sender, EventArgs e)
        {
            Rectangle mouseRect = new Rectangle(MousePosition, new Size(1, 1));

            var rectC = dataGridView1.GetColumnDisplayRectangle(dataGridView1.Columns.Count - 1, true);
            var right = rectC.Right;

            var rectR = dataGridView1.GetRowDisplayRectangle(dataGridView1.Rows.Count - 1, true);
            var bottom = rectR.Bottom;

            var rect = new Rectangle(
                dataGridView1.PointToScreen(dataGridView1.Location),
                new Size(right, bottom));

            if (!rect.IntersectsWith(mouseRect))
                ttText.Hide(this);
        }

        void event_MouseMove(object sender, MouseEventArgs e)
        {
            dataGridView1_MouseLeave(sender, EventArgs.Empty);
        }
    }

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
protected override void OnCellToolTipTextNeeded(DataGridViewCellToolTipTextNeededEventArgs e)
{
    if((e.RowIndex >= 0) && (e.ColumnIndex >= 0))
    {
        // By setting this explicitly we can make the ToolTip show the           
        // entire length even though the content itself has not changed.
        e.ToolTipText = this[e.ColumnIndex, e.RowIndex].Value.ToString();
    }
    base.OnCellToolTipTextNeeded(e);
}