VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > 简明python教程 >
  • [原创][开源] SunnyUI.Net 开发日志:ListBox 增加跟随鼠标滑过高亮(3)

兄弟们,人家这花两年时间解决的,应该有用,继续再找找,又找到一篇洋文的:

https://stackoverflow.com/questions/1316027/listbox-drawitem-hotlight-state-in-the-ownerdraw-mode

It took me only two years to find the answer for you, but here it is:

The DrawItemState.HotLight only applies to owner drawn menus, not the listbox.

For the ListBox, you have to keep track of the item yourself:

看看,也是两年,估计上面中文的从这个翻译过来。

除了这俩,还真没找到。

继续撸代码,果真管用。不过还是有问题,鼠标滑快了,ListBox闪烁的厉害。

分析代码  listBox1.Invalidate(); 这是刷新全部的。鼠标滑过也就和本次选中和上次选中的有关系。

就刷这两个Item就行,有了思路,撸代码三连发:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
private int lastIndex = -1;
private int mouseIndex = -1;
 
[Browsable(false)]
public int MouseIndex
{
    get => mouseIndex;
    set
    {
        if (mouseIndex != value)
        {
            if (lastIndex >= 0 && lastIndex != SelectedIndex)
            {
                OnDrawItem(new DrawItemEventArgs(this.CreateGraphics(), Font, GetItemRectangle(lastIndex), lastIndex, DrawItemState.Grayed));
            }
 
            mouseIndex = value;
            if (mouseIndex >= 0 && mouseIndex != SelectedIndex)
            {
                OnDrawItem(new DrawItemEventArgs(this.CreateGraphics(), Font, GetItemRectangle(value), value, DrawItemState.HotLight));
            }
 
            lastIndex = mouseIndex;
        }
    }
}
 
protected override void OnMouseMove(MouseEventArgs e)
{
    base.OnMouseMove(e);
    MouseIndex = IndexFromPoint(e.Location);
}
 
protected override void OnMouseLeave(EventArgs e)
{
    base.OnMouseLeave(e);
    MouseIndex = -1;
}

相关教程