how to wordwrap text in tooltip

前端 未结 5 2057
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-11 17:45

How to wordwrap text that need to be appear in ToolTip

相关标签:
5条回答
  • 2021-01-11 18:02

    For WPF, you can use the TextWrapping property:

    <ToolTip>
        <TextBlock Width="200" TextWrapping="Wrap" Text="Some text" />
    </ToolTip>
    
    0 讨论(0)
  • 2021-01-11 18:07

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

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

    link

    0 讨论(0)
  • 2021-01-11 18:08

    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

    0 讨论(0)
  • 2021-01-11 18:17

    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);
        }
    }
    
    0 讨论(0)
  • 2021-01-11 18:21

    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:");
    
    0 讨论(0)
提交回复
热议问题