gpt4 book ai didi

c# - 仅使 ListBox 中的单个项目无效

转载 作者:太空狗 更新时间:2023-10-30 00:44:48 24 4
gpt4 key购买 nike

有没有办法只使 ListBox 中的单个项目无效?似乎没有获取项目矩形的方法,或者即使特定项目可见(除了为控件中的每个像素/至少一列中的每个像素调用 IndexFromPoint 之外)。

这适用于 C# WinForms(而非 WPF)。

有关我正在尝试做的事情的更多信息:

我有一个 ListBox,里面有很多项目,我想要一组“按钮”出现在您将鼠标悬停在上面的项目上(例如红色的 X 表示删除)。我的这个工作很好,除了在包含 10 个或更多项目的列表上,每次将鼠标悬停在一个新项目上时,它都会导致可见的重绘,因为我使整个控件无效。数据没有改变,只是显示改变了。

编辑:更多信息和以前的尝试

我已经将 ListBox 子类化并在 OnDrawItem 中执行绘图,因此可以使用 ListBox 的 protected 方法。

我已经尝试了以下不同程度的成功。变量 this 是扩展的 ListBoxindex 是要绘制的项目,old_index 是之前正在绘制的项目画过来。

// Causes flicker of entire list, but works
this.Invalidate();

// Causes flicker of selected item, but works
int sel_index = this.SelectedIndex;
this.SelectedIndex = old_index;
this.SelectedIndex = index;
this.SelectedIndex = sel_index;

// Does not work
if (old_index >= 0)
this.RefreshItem(old_index);
if (index >= 0)
this.RefreshItem(index);

最佳答案

好吧,我是傻了。感谢@LarsTech,我决定再次查看整个 ListBox 函数列表并找到合适的函数:GetItemRectangle,它甚至是公开的!我不知道过去一个小时我是怎么错过这个的……

工作解决方案是:

if (old_index >= 0)
this.Invalidate(this.GetItemRectangle(old_index));
if (index >= 0)
this.Invalidate(this.GetItemRectangle(index));

这仍然会产生一点闪烁,但明显更少(仅当我将鼠标快速悬停在移动单个项目之前导致闪烁的位置时才可见)。

关于c# - 仅使 ListBox 中的单个项目无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6962892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com