2017-09-13 6 views
0

カスタムツールチップを作成しましたが、OwnerDrawフラグをオンにしないと、システムと同じスタイルで描画されます。 「オリジナル」のようなカスタムツールチップを作成するにはどうすればよいですか?カスタム描画のツールチップスタイルとサイズの重複

ttSessionInfo.ToolTipTitle = UiTranslator.Instance.GetLabel(UiLabels.DC_DSE_Session); 

var toolTipSessionsText = sessions.Aggregate(
         new StringBuilder(), 
         (p_strBuilder, p_session) => p_strBuilder.AppendLine(string.Format("{0}: {1}", p_session.SessionName, 
          p_session.IsConnected ? connectedText : disconnectedText))).ToString(); 

ttSessionInfo.SetToolTip(LiveUpdatePb, toolTipSessionsText); 

結果は次のとおりです。This looks like

は、私が言うことができます、別のコントロールに表示するまったく同じツールチップが必要ですが、ペイントする、第二列「アレックス・セッション:接続」赤い色で。

+0

以下のあなたがやっていることを示すために良いでしょう。いくつかのサンプルコード... – saeed

+0

@saeed私は私の質問を更新しました。私は同じ機能を(システムのデフォルトとして)したいが、私が望むように私のテキストをペイントする能力を持っている。 –

+0

ttSessionInfoの基本クラスは何ですか – saeed

答えて

0

私は、ToolTipクラスとそれを使用するためのコードを再実装したサンプルを追加しました。

クラス:

class CustomToolTip : ToolTip 
    { 
     public CustomToolTip() 
     { 
      this.OwnerDraw = true; 
      this.Popup += new PopupEventHandler(this.OnPopup); 
      this.Draw += new DrawToolTipEventHandler(this.OnDraw); 
     } 

     string m_EndSpecialText; 
     Color m_EndSpecialTextColor =Color.Red; 

     public Color EndSpecialTextColor 
     { 
      get { return m_EndSpecialTextColor; } 
      set { m_EndSpecialTextColor = value; } 
     } 

     public string EndSpecialText 
     { 
      get { return m_EndSpecialText; } 
      set { m_EndSpecialText = value; } 
     } 

     private void OnPopup(object sender, PopupEventArgs e) // use this event to set the size of the tool tip 
     { 
      e.ToolTipSize = new Size(200, 100); 
     } 

     private void OnDraw(object sender, DrawToolTipEventArgs e) // use this event to customise the tool tip 
     { 
      Graphics g = e.Graphics; 

      LinearGradientBrush b = new LinearGradientBrush(e.Bounds, 
       Color.GreenYellow, Color.MintCream, 45f); 

      g.FillRectangle(b, e.Bounds); 

      g.DrawRectangle(new Pen(Brushes.Red, 1), new Rectangle(e.Bounds.X, e.Bounds.Y, 
       e.Bounds.Width - 1, e.Bounds.Height - 1)); 

      //g.DrawString(e.ToolTipText, new Font(e.Font, FontStyle.Bold), Brushes.Silver, 
      // new PointF(e.Bounds.X + 6, e.Bounds.Y + 6)); // shadow layer 
      g.DrawString(e.ToolTipText, new Font(e.Font, FontStyle.Bold), Brushes.Black, 
       new PointF(e.Bounds.X + 5, e.Bounds.Y + 5)); // top layer 

      SolidBrush brush = new SolidBrush(EndSpecialTextColor); 

      g.DrawString(EndSpecialText, new Font(e.Font, FontStyle.Bold), brush, 
       new PointF(e.Bounds.X + 5, e.Bounds.Bottom - 15)); // top layer 

      brush.Dispose(); 
      b.Dispose(); 
     } 
    } 

上記のクラスの使用

private void button1_Click(object sender, EventArgs e) 
    { 
     CustomToolTip toolTip1 = new CustomToolTip(); 
     toolTip1.ShowAlways = true; 
     toolTip1.SetToolTip(button1, "Click me to execute."); 
     toolTip1.EndSpecialText = "Hello I am special"; 
    } 

enter image description here

+0

@Geka Pはこの記事をあなたに役立てました。 – saeed

関連する問題