我正在尝试为listview控件中的一些子项设置工具提示文本.我无法获得显示的工具提示. 有人有什么建议吗? Private _timer As TimerPrivate Sub Timer() If _timer Is Nothing Then _timer = New Timer _timer.Inte
有人有什么建议吗?
Private _timer As Timer Private Sub Timer() If _timer Is Nothing Then _timer = New Timer _timer.Interval = 500 AddHandler _timer.Tick, AddressOf TimerTick _timer.Start() End If End Sub Private Sub TimerTick(ByVal sender As Object, ByVal e As EventArgs) _timer.Enabled = False End Sub Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs) If Not _timer.Enabled Then Dim item = Me.HitTest(e.X, e.Y) If Not item Is Nothing AndAlso Not item.SubItem Is Nothing Then If item.SubItem.Text = "" Then Dim tip = New ToolTip Dim p = item.SubItem.Bounds tip.ToolTipTitle = "Status" tip.ShowAlways = True tip.Show("FOO", Me, e.X, e.Y, 1000) _timer.Enabled = True End If End If End If MyBase.OnMouseMove(e) End SubObjectListView(围绕.NET WinForms ListView的开源包装器)内置了对单元工具提示的支持(是的,它确实适用于VB).你监听一个CellToolTip事件,你可以做这样的事情(这无疑是过分的):
alt text http://i31.tinypic.com/20udbgo.png
如果您不想使用ObjectListView,则需要继承ListView,侦听WM_NOTIFY消息,然后在这些消息中,以类似于此的方式响应TTN_GETDISPINFO通知:
case TTN_GETDISPINFO: ListViewHitTestInfo info = this.HitTest(this.PointToClient(Cursor.Position)); if (info.Item != null && info.SubItem != null) { // Call some method of your own to get the tooltip you want String tip = this.GetCellToolTip(info.Item, info.SubItem); if (!String.IsNullOrEmpty(tip)) { NativeMethods.TOOLTIPTEXT ttt = (NativeMethods.TOOLTIPTEXT)m.GetLParam(typeof(NativeMethods.TOOLTIPTEXT)); ttt.lpszText = tip; if (this.RightToLeft == RightToLeft.Yes) ttt.uFlags |= 4; Marshal.StructureToPtr(ttt, m.LParam, false); return; // do not do normal processing } } break;
显然,这是C#,而不是VB,但你明白了.