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

其中 new DrawItemEventArgs(this.CreateGraphics(), Font, GetItemRectangle(value), value, DrawItemState.HotLight) 

第一个参数Graphics,也找了一会儿,后来看这篇:

https://www.cnblogs.com/yuanyeguhong/archive/2013/09/20/3330606.html

 

其实就是Graphics对象的DrawString方法,而参数e中的Graphics是如何来的呢。
我们接着分析DrawItemEventArgs这个类,他既然是对listBox1某一项的属性的打包,
那么我估计其中的Graphics对象就是由listBox1.creatgraphics而来的。
好了,到此我们就可以自定义重绘listbox某项的函数了,可任意调用的哦!

好了,至此,问题都已找到答案,再捋一下思路,把逻辑理顺。撸代码,调试,OK!!!

主要代码如下:

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
protected override void OnDrawItem(DrawItemEventArgs e)
        {
            base.OnDrawItem(e);
 
            BeforeDrawItem?.Invoke(this, Items, e);
            if (Items.Count == 0)
            {
                return;
            }
 
            bool otherState = e.State == DrawItemState.Grayed || e.State == DrawItemState.HotLight;
            if (!otherState)
            {
                e.DrawBackground();
            }
 
            if (e.Index < 0 || e.Index >= Items.Count)
            {
                return;
            }
 
            StringFormat sStringFormat = new StringFormat();
            sStringFormat.LineAlignment = StringAlignment.Center;
 
            bool isSelected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;
            Color backColor = isSelected ? ItemSelectBackColor : BackColor;
            Color foreColor = isSelected ? ItemSelectForeColor : ForeColor;
 
            Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1);
            if (!otherState)
            {
                e.Graphics.FillRectangle(BackColor, e.Bounds);
                e.Graphics.FillRoundRectangle(backColor, rect, 5);
                e.Graphics.DrawString(Items[e.Index].ToString(), e.Font, foreColor, e.Bounds, sStringFormat);
            }
            else
            {
                if (e.State == DrawItemState.Grayed)
                {
                    backColor = BackColor;
                    foreColor = ForeColor;
                }
 
                if (e.State == DrawItemState.HotLight)
                {
                    backColor = HoverColor;
                    foreColor = ForeColor;
                }
 
                e.Graphics.FillRectangle(BackColor, e.Bounds);
                e.Graphics.FillRoundRectangle(backColor, rect, 5);
                e.Graphics.DrawString(Items[e.Index].ToString(), e.Font, foreColor, e.Bounds, sStringFormat);
            }
 
            AfterDrawItem?.Invoke(this, Items, e);
        }

相关教程