加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

vb.net – 如何在.Net中的ListView子项上设置工具提示

发布时间:2020-12-17 07:11:49 所属栏目:百科 来源:网络整理
导读:我正在尝试为listview控件中的一些子项设置工具提示文本.我无法获得显示的工具提示. 有人有什么建议吗? Private _timer As TimerPrivate Sub Timer() If _timer Is Nothing Then _timer = New Timer _timer.Interval = 500 AddHandler _timer.Tick,AddressO
我正在尝试为listview控件中的一些子项设置工具提示文本.我无法获得显示的工具提示.

有人有什么建议吗?

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 Sub

解决方法

ObjectListView(围绕.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,但你明白了.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读