如何自动换行需要在工具提示中显示的文本
看起来它不是直接支持的:
如何自动换行显示的工具提示?
这是使用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 );
瑞德贡
这是我最近写的一篇文章,我知道它不是最好的,但它有效。您需要按如下方式扩展ToolTip控件:
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:");
对于WPF,您可以使用TextWrapping属性:
<ToolTip>
<TextBlock Width="200" TextWrapping="Wrap" Text="Some text" />
</ToolTip>
您可以使用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);
}
}