当鼠标悬停在ListViewSubItem上时,我需要显示一个带有图标、标题和文本的工具提示。但是工具提示只有当单元格的基础文本被用省略号修剪时才会弹出。
到目前为止,我有以下代码:
private void ListView_ItemMouseHover(object sender, ListViewItemMouseHoverEventArgs e)
{
if (e.Item != null)
{
// get last subItem
ListViewItem.ListViewSubItem mySubItem = e.Item.SubItems[item.SubItems.Count - 1];
// TODO -> how to check if text is trimmmed?
// e.g. "This is the displayed text in the subitem whi..."
//if (mySubItem.IsTrimmed???)
{
// mToolTip is an instance of ToolTip class
mToolTip.ToolTipIcon = // any icon...
mToolTip.ToolTipTitle = "some title text";
mToolTip.SetToolTip(ListView, "some body text");
}
}
else
{
mToolTip.Hide(ListView);
}
}
有什么想法吗?
首先测量绘制字符串的大小:
float realWidth;
using (Graphics g = listView.CreateGraphics()) {
realWidth= g.MeasureString(mySubItem.Text, mySubItem.Font).Width;
}
并找出它是否大于当前大小:
if (mySubItem.Bounds.Width > realWidth) {
// Show tool tip...
}
要制作自定义工具提示框,我认为您必须放弃ToolTip
。相反,创建一个具有所需布局的Panel
,然后在需要显示时更改Panel
的Location
。您还可以添加某种动画,将Panel
"滑动"到视图中或其他位置。加载您想要在Panel
中显示的信息,然后显示。
至于检测ListViewItem
中的文本,有一个ItemMouseHover
事件可能会起作用。