-
C#教程之[原创][开源] 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); } |
栏目列表
最新更新
C# 面向对象
假设客车的座位数是9行4列,使用二维数
C#基于接口设计三层架构Unity篇
C#线程 入门
C#读取静态类常量属性和值
C# 插件式编程
C# 委托与事件有啥区别?
C#队列学习笔记:队列(Queue)和堆栈(Stack
linq 多表分组左连接查询查询统计
C#队列学习笔记:MSMQ入门一
C# 在Word中添加Latex 数学公式和符号
inncheck命令 – 检查语法
基于UDP的服务器端和客户端
再谈UDP和TCP
在socket编程中使用域名
网络数据传输时的大小端问题
socket编程实现文件传输功能
如何优雅地断开TCP连接?
图解TCP四次握手断开连接
详细分析TCP数据的传输过程
SqlServer 利用游标批量更新数据
BOS只读状态修改
SQL Server等待事件—PAGEIOLATCH_EX
数据库多行转换为单一列
获取数据表最后最后访问,修改,更新,
计算经历的时间
SQL查询结果自定义排序
修改数据库默认位置
日期简单加或减
从日期获取年,月或日