[原创][开源] SunnyUI.Net 开发日志:ListBox 增加跟随鼠标滑过高亮(开源blog)

网友投稿 249 2022-08-17

[原创][开源] SunnyUI.Net 开发日志:ListBox 增加跟随鼠标滑过高亮(开源blog)

SunnyUI.Net, 基于 C# .Net WinForm 开源控件库、工具类库、扩展类库、多页面开发框架

Blog: https://cnblogs.com/yhuse

Gitee: https://gitee.com/yhuse/SunnyUI

GitHub: https://github.com/yhuse/SunnyUI

SunnyUI.Net 系列文章目录 

欢迎交流,QQ群:56829229 (SunnyUI技术交流群)

SunnyUI.Net 开发日志:ListBox 增加跟随鼠标滑过高亮

QQ群里,寸木说,ListBox鼠标移动时,当前行需要焦点,我想了想,不难实现啊

不就是在鼠标移动时重绘Item嘛,何况选中的Item已经改了颜色了。

见UIListBox代码:

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

protected override void OnDrawItem(DrawItemEventArgs e)

        {

            base.OnDrawItem(e);

            BeforeDrawItem?.Invoke(this, Items, e);

            if (Items.Count == 0)

            {

                return;

            }

 

            e.DrawBackground();

 

            if (e.Index < 0 || e.Index >= Items.Count)

            {

                return;

            }

 

            StringFormat sStringFormat = new StringFormat();

            sStringFormat.LineAlignment = StringAlignment.Center;

 

            Color backColor = (e.State & DrawItemState.Selected) == DrawItemState.Selected ? ItemSelectBackColor : BackColor;

            Color foreColor = (e.State & DrawItemState.Selected) == DrawItemState.Selected ? ItemSelectForeColor : ForeColor;

 

            Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1);

            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);

        }

看:(e.State & DrawItemState.Selected) == DrawItemState.Selected 选中行状态嘛

看了e.State有e.State == DrawItemState.HotLight,不就是高亮的么,于是开始撸代码,加状态判断

Run

晕。。。没变,这HotLight不起作用,好吧,问度娘。。。

翻山越岭,跋山涉水。。。

找到这篇:https://jb51.cc/csharp/101121.html

其中提到:

我在我的WinForms应用程序中使用OwnerDrawFixed作为DrawMode用于自定义ListBox控件.当用户将鼠标悬停在列表框项目上时,我希望重新绘制ListBoxItem的背景(或执行其他操作),即在MouseMove …DrawItemState.HotLight永远不适用于ListBox,所以我想知道如何模拟它,如何解决这个问题.

共5页: 上一页12345下一页

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:C#设计模式之订阅发布模式(c1驾照能开什么车)
下一篇:Python大佬分析了15万歌词,告诉你民谣歌手们到底在唱什么
相关文章

 发表评论

暂时没有评论,来抢沙发吧~