开发者

how to wordwrap text in tooltip

How to wordwrap text that开发者_StackOverflow need to be appear in ToolTip


It looks like it isn't supported directly:

How do I word-wrap the Tooltip that is displayed?

Here is a method using Reflection to achieve this.

[ DllImport( "user32.dll" ) ] 
private extern static int SendMessage( IntPtr hwnd, uint msg,
  int wParam, int lParam); 

object o = typeof( ToolTip ).InvokeMember( "Handle",
   BindingFlags.NonPublic | BindingFlags.Instance |
   BindingFlags.GetProperty, 
   null, myToolTip, null ); 
IntPtr hwnd = (IntPtr) o; 
private const uint TTM_SETMAXTIPWIDTH = 0x418;
SendMessage( hwnd, TTM_SETMAXTIPWIDTH, 0, 300 );

Rhett Gong


Another way, is to create a regexp that wraps automatically.

WrappedMessage := RegExReplace(LongMessage,"(.{50}\s)","$1`n")

link


This is a piece I wrote recently, I know its not the best but it works. You need to extend the ToolTip Control as follows:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

public class CToolTip : ToolTip
{
   protected Int32 LengthWrap { get; private set; }
   protected Control Parent { get; private set; }
   public CToolTip(Control parent, int length)
      : base()
   {
    this.Parent = parent;
    this.LengthWrap = length;
   }

   public String finalText = "";
   public void Text(string text)
   {
      var tText = text.Split(' ');
      string rText = "";

      for (int i = 0; i < tText.Length; i++)
      {
         if (rText.Length < LengthWrap)
         {
           rText += tText[i] + " ";
         }
         else
         {
             finalText += rText + "\n";
             rText = tText[i] + " ";
         }

         if (tText.Length == i+1)
         {
             finalText += rText;
         }
      }
  }
      base.SetToolTip(Parent, finalText);
  }
}

And you will use it like:

CToolTip info = new CToolTip(Control,LengthWrap);
         info.Text("It looks like it isn't supported directly. There is a workaround at
         http://windowsclient.net/blogs/faqs/archive/2006/05/26/how-do-i-word-wrap-the-
         tooltip-that-  is-displayed.aspx:");


You can set the size of the tooltip using e.ToolTipSize property, this will force word wrapping:

public class CustomToolTip : ToolTip
{
    public CustomToolTip () : base()
    {
        this.Popup += new PopupEventHandler(this.OnPopup);
    }

    private void OnPopup(object sender, PopupEventArgs e) 
    {
        // Set custom size of the tooltip
        e.ToolTipSize = new Size(200, 100);
    }
}


For WPF, you can use the TextWrapping property:

<ToolTip>
    <TextBlock Width="200" TextWrapping="Wrap" Text="Some text" />
</ToolTip>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜