2012-02-23 21 views
1

在Windows窗體(C#)中是否有動態設置工具提示高度和寬度的方法(在代碼中指示)。我正在使用DataViewGrid控件,所以我不得不使用Show方法。不過,我已經注意到(左到它自己的設備時),該工具提示控件並不總是調整到所提供的內容....在Windows窗體(C#)中是否有動態設置工具提示高度和寬度的方法

一個例子:

有一個工具提示控制添加到窗體(稱爲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需要一個機制,因此顯示工具提示幫助使用的顯示方法和我在非DataViewGrids其他地方看到這種現象......

下面是代碼的例子: using System.Collections.Generic; using System.Drawing;使用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); 
      } 
     } 
    } 
} 

如果您將鼠標懸停在第一列上,則第二列中的工具提示文本將被截斷。

+0

只有一個字符串參數的Show()方法在哪裏?你能鏈接到MSDN中的方法嗎? – 2012-02-23 03:51:46

+0

http://msdn.microsoft.com/en-us/library/ms752368.aspx,可能會有幫助嗎? – 2012-02-23 04:08:16

+0

而在任何人譴責我沒有添加代碼示例之前,在這裏你去: – Silverten 2012-02-23 04:13:14

回答

0

MSDN它提到使用RenderSize屬性這種事情,但有一些注意事項。

+0

對不起 - 不使用WPF - 只有標準的Windows窗體。 – Silverten 2012-02-23 04:28:01

+0

它可能與氣球模式或其中一個屬性有關...該示例工作正常,直到達到臨界閾值... – Silverten 2012-02-23 04:34:44

+0

不使用氣球模式似乎可以修復它 - C'est la vie ... – Silverten 2012-02-23 05:11:33

2

好吧,自從我發佈這個問題以來已經有好幾個月了。從那以後,我沒有真正考慮過這個問題 - 只是接受了這個行爲...直到今晚。

由於沒有其他人想到答案(或關心提供一個替代的窗體形式的想法),我想到了重新審視一般問題。氣球工具提示似乎是一種自由形式的東西......它採用了它遇到的以前的形狀。這可能是微軟懶惰的工作。或者更好 - 這是微軟將開發人員指向不同方向的方式。畢竟,唯一提供的新方向是wpf ...

但是,正如我在原始問題中所述,我需要的解決方案只有Windows窗體,不能將WPF或Silverlight或其他技術混合使用。畢竟,如果你是WPF程序員,你可能想要將Windows Forms控件添加到組合中,但對於最純粹的,這不會。

所以這裏更多的是解決這一難題一點 - 如何使氣球調整到所需的尺寸...

我重新看了看問題,發現氣球將每次調整將鼠標指針進入細胞,所以要爲此,下面推導:

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

我已經擴大了在網格中的敘述,以幫助證明的一般問題。

我知道這不是一個完美的解決方案 - 但這就是封裝的全部內容。

新問題......是否有可能在鼠標點顯示氣球工具提示的要點 - 或者我只是過着一個夢幻般的夢想?

現在,只要可能的解決方案...我已經讀了一點關於繼承工具提示對象來執行與自定義類的繪圖。使用它來確定文本的大小是不可行的,然後確定氣球將顯示哪個方向以及文本的偏移量?

相關問題