代码之家  ›  专栏  ›  技术社区  ›  Avram

如何在工具提示中自动换行文字

  •  10
  • Avram  · 技术社区  · 15 年前

    如何对需要显示在工具提示中的文本进行自动换行

    5 回复  |  直到 15 年前
        1
  •  8
  •   rory.ap    5 年前

    如何对显示的工具提示进行自动换行?

    实现这个目标。

    [ 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 );
    

        2
  •  5
  •   Patrick D'Souza ob1    12 年前

    另一种方法是创建一个自动包装的regexp。

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

    link

        3
  •  1
  •   Iker Ruiz Arnauda    11 年前

    这是我最近写的一篇文章,我知道它不是最好的,但它很管用。您需要按如下方式扩展工具提示控件:

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

    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:");
    
        4
  •  1
  •   NielW    7 年前

    对于WPF,可以使用TextWrapping属性:

    <ToolTip>
        <TextBlock Width="200" TextWrapping="Wrap" Text="Some text" />
    </ToolTip>
    
        5
  •  0
  •   Mariusz Jamro    9 年前

    可以使用设置工具提示的大小 e.ToolTipSize

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