2010-07-12 22 views

回答

7

看起來它不直接支持文本文字:

如何換行的工具提示被展示?

這裏是一個使用Reflection的方法,以 實現這個。

[ 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; 
SendMessage(hwnd, 0x0418, 0, 300); 

瑞德宮

+0

嗨,這個代碼放在哪裏? – 2011-01-25 05:11:27

+3

理想情況下,此處的幻數0x418應該定義爲與相應頭文件Commctrl.h中使用的名稱相同的常量:「private const uint TTM_SETMAXTIPWIDTH = 0x418」 - 這使得Google可以更容易地獲取更多信息。 – Joe 2011-04-01 12:03:27

+0

在Windows 7中,我體驗到這種破解只能解決OS在OS中禁用視覺樣式時的問題。任何想法如何解決這種情況? – Mustafa 2011-09-23 09:38:33

5

另一種方式,是創建自動包裝了一個正則表達式。

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

link

+3

這看起來像一個PHP代碼或什麼的。這裏的C#版本: 正則表達式rgx = new Regex(「(。{50} \\ s)」); string WrappedMessage = rgx.Replace(longMessage,「$ 1 \ n」); – Patratacus 2013-08-02 01:22:55

1

這一塊我最近寫了,我知道它不是最好的,但它的工作原理。你需要如下擴展工具提示控件:

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:"); 
0

可以使用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); 
    } 
} 
1

對於WPF,您可以使用TextWrapping屬性:

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