2012-02-23 13 views
1

Windowsフォーム(C#)にはツールヒントの高さと幅を動的に設定する方法があります。私はDataViewGridコントロールで作業しているので、Showメソッドを使用する必要があります。 (それ自身のデバイスに去ったとき)しかし、私は、ツールヒントコントロールは、常に提供するコンテンツに合わせて調整しないことに気づいた....Windowsフォーム(C#)には、ツールヒントの高さと幅を動的に設定する方法があります。

例を:ToolTipコントロールを持つ

は(と呼ばれるフォームに追加しましたこの上

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

任意の考え:

ttText.Show("I'm hungry\nand waiting!"); 

は、次の呼び出しを切り捨てます:ttText)とは、それが最初のテキストを表示しましたか?

ここ

はコードの例です... DataGridViewのは、それゆえ表示方法の使用をツールチップヘルプを表示する機構を必要とし、私は非DataViewGridsの別の場所で、この動作を見てきたことに留意してください: 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(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列目の上に置くと、2番目は、ツールチップのテキストが切り捨てられます。

+0

文字列パラメータだけをとる 'Show()'メソッドはどこにありますか? MSDNのそのメソッドにリンクできますか? –

+0

http://msdn.microsoft.com/en-us/library/ms752368.aspx、これは役立つかもしれませんか? –

+0

そして、コード例を追加しないと誰かが嫌がる前に、ここに行く: – Silverten

答えて

0

MSDNには、RenderSizeプロパティを使用してこのようなことが言及されていますが、いくつかの注意点があります。

+0

申し訳ありません - WPF - 標準Windowsフォームのみを使用していません。 – Silverten

+0

バルーンモードやプロパティの1つと関係するかもしれません...例は重大な閾値までうまくいきました... – Silverten

+0

バルーンモードを使用していないと思われます - C'est la vie ... – Silverten

2

OK、この質問を投稿してから数ヶ月が経ちました。私は本当にこの問題をそれ以来考えていない - 今晩まで行動を受け入れた。

誰かが答えを考えていない(または別のWindowフォームの考え方を提供している)のではないので、一般的な問題を見直すことができました。バルーンツールの先端は、自由形式のものであるように見えます...それは直前の形になります。おそらくMicrosoftの仕事の怠け者だろう。それよりも、開発者を別の方向に向けるマイクロソフトの方法です。結局のところ、提供される新しい方向はwpfに向いていました...

しかし、元の質問で述べたように、WPFやSilverlightなどの技術をポットに混ぜるのではなく、Windowsフォームのみのソリューションが必要でした。結局のところ、あなたが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()); 
     } 
    } 
} 

私は一般的な問題を示す助けるために、グリッドに物語を展開しています。

これは完全にエレガントな解決策ではないことがわかりましたが、これがカプセル化の全体像です。

新しい問題点...マウスポイントにバルーンツールチップのポイントを表示することは可能ですか?それとも、私はただパイプの夢を生きていますか?

可能な解決策として...私は、カスタムクラスを使用して描画を実行するためにツールチップオブジェクトを継承することについて少しは読んでいます。それを使用してテキストのサイズを決定し、バルーンが表示する方向とテキストのオフセットを判断することは現実的ではありませんか?

関連する問題