ListBox DrawItem HotLight State in the OwnerDraw mode?(ListBox DrawItem HotLight 在 OwnerDraw 模式下的状态?)
问题描述
我在我的 WinForms 应用程序中使用 OwnerDrawFixed
作为自定义 ListBox 控件的 DrawMode.
I'm using OwnerDrawFixed
as a DrawMode for the custom ListBox control in my WinForms app.
当用户将鼠标悬停在列表框项目上时,我想重新绘制 ListBoxItem 的背景(或执行一些其他操作),即在 MouseMove...
I want to repaint the background (or do some other action) of the ListBoxItem when the user hovers over the listbox item, that is, at the MouseMove...
DrawItemState.HotLight
从不适用于 ListBox,所以我想知道如何模拟它,如何解决这个问题.
DrawItemState.HotLight
never works for the ListBox, so i wonder how to emulate it, how to workaround this problem.
推荐答案
我只用了两年的时间才为你找到答案,但这里是:
It took me only two years to find the answer for you, but here it is:
DrawItemState.HotLight 仅适用于所有者绘制的菜单,而不适用于列表框.对于 ListBox,您必须自己跟踪项目:
The DrawItemState.HotLight only applies to owner drawn menus, not the listbox. For the ListBox, you have to keep track of the item yourself:
public partial class Form1 : Form
{
private int _MouseIndex = -1;
public Form1()
{ InitializeComponent(); }
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Brush textBrush = SystemBrushes.WindowText;
if (e.Index > -1)
{
if (e.Index == _MouseIndex)
{
e.Graphics.FillRectangle(SystemBrushes.HotTrack, e.Bounds);
textBrush = SystemBrushes.HighlightText;
}
else
{
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds);
textBrush = SystemBrushes.HighlightText;
}
else
e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
}
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, textBrush, e.Bounds.Left + 2, e.Bounds.Top);
}
}
private void listBox1_MouseMove(object sender, MouseEventArgs e)
{
int index = listBox1.IndexFromPoint(e.Location);
if (index != _MouseIndex)
{
_MouseIndex = index;
listBox1.Invalidate();
}
}
private void listBox1_MouseLeave(object sender, EventArgs e)
{
if (_MouseIndex > -1)
{
_MouseIndex = -1;
listBox1.Invalidate();
}
}
}
这篇关于ListBox DrawItem HotLight 在 OwnerDraw 模式下的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ListBox DrawItem HotLight 在 OwnerDraw 模式下的状态?


- C# 中多线程网络服务器的模式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01