Visual Studio Winforms -列表框项悬停并选择颜色

iecba09b  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(126)

我正在尝试为我的Winforms应用程序从WPF应用程序重新创建一个列表框。但我真的不知道从哪里开始。
所选项目的边框为蓝色(1),当它未聚焦时,它将变为白色(2)。悬停颜色应为浅蓝色(3
谢谢你的帮助!
(By我使用的是.NET Framework 4.8)

ccgok5k5

ccgok5k51#

您可以从原始列表框派生创建自己的列表框。新列表首次编译后,它将出现在工具箱中,以便您可以将其拖放到表单中。或者您可以在Form.designer.cs中手动将现有列表框的类型更改为ListBoxEx

  1. public class ListBoxEx : ListBox
  2. {
  3. public ListBoxEx()
  4. {
  5. DrawMode = DrawMode.OwnerDrawFixed;
  6. DoubleBuffered = true; // Eliminates flicker (optional).
  7. }
  8. private int _hotTrackedIndex = -1;
  9. private int HotTrackedIndex
  10. {
  11. get => _hotTrackedIndex;
  12. set {
  13. if (value != _hotTrackedIndex) {
  14. if (_hotTrackedIndex >= 0 && _hotTrackedIndex < Items.Count) {
  15. Invalidate(GetItemRectangle(_hotTrackedIndex));
  16. }
  17. _hotTrackedIndex = value;
  18. if (_hotTrackedIndex >= 0) {
  19. Invalidate(GetItemRectangle(_hotTrackedIndex));
  20. }
  21. }
  22. }
  23. }
  24. protected override void OnDrawItem(DrawItemEventArgs e)
  25. {
  26. var borderRect = e.Bounds;
  27. borderRect.Width--;
  28. borderRect.Height--;
  29. if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
  30. if (Focused) {
  31. e.Graphics.FillRectangle(Brushes.Teal, e.Bounds);
  32. e.Graphics.DrawRectangle(Pens.LightSkyBlue, borderRect);
  33. } else {
  34. e.Graphics.FillRectangle(Brushes.DimGray, e.Bounds);
  35. e.Graphics.DrawRectangle(Pens.White, borderRect);
  36. }
  37. } else if (e.Index == HotTrackedIndex) {
  38. e.Graphics.FillRectangle(Brushes.DarkSlateGray, e.Bounds);
  39. e.Graphics.DrawRectangle(Pens.DarkCyan, borderRect);
  40. } else {
  41. e.DrawBackground();
  42. }
  43. if (Items[e.Index] != null) {
  44. e.Graphics.DrawString(Items[e.Index].ToString(), e.Font, Brushes.White, 6, e.Bounds.Top, StringFormat.GenericTypographic);
  45. }
  46. }
  47. protected override void OnMouseLeave(EventArgs e)
  48. {
  49. HotTrackedIndex = -1;
  50. base.OnMouseLeave(e);
  51. }
  52. protected override void OnMouseMove(MouseEventArgs e)
  53. {
  54. HotTrackedIndex = IndexFromPoint(e.Location);
  55. base.OnMouseMove(e);
  56. }
  57. protected override void OnGotFocus(EventArgs e)
  58. {
  59. if (SelectedIndex >= 0) {
  60. RefreshItem(SelectedIndex);
  61. }
  62. base.OnGotFocus(e);
  63. }
  64. protected override void OnLostFocus(EventArgs e)
  65. {
  66. if (SelectedIndex >= 0) {
  67. RefreshItem(SelectedIndex);
  68. }
  69. base.OnLostFocus(e);
  70. }
  71. }

字符串
我们通过重写OnDrawItem来改变列表框的外观。在构造函数中,我们设置DrawMode = DrawMode.OwnerDrawFixed;以启用所有者绘制。
我们必须考虑选中项和热跟踪项,即鼠标移动过的项。如果要绘制的项是选中项,我们进一步区分列表框是否具有焦点。
FillRectangle绘制背景。DrawRectangle绘制边框。请注意,边框矩形必须比e.Bounds矩形小一个像素,否则不会绘制右边框和下边框。
如果当前项目没有被选中,我们测试它是否是热跟踪。如果是,我们用不同的颜色绘制。否则我们用e.DrawBackground();绘制默认背景。
然后我们用DrawString在背景上绘制文本。
为了使所有这些工作,我们还必须使列表框中颜色发生变化的区域无效。我们在OnMouseMoveOnMouseLeave中检测热跟踪的变化。在那里我们设置HotTrackedIndex。这是一个在必要时触发绘制的属性。
OnGotFocusOnLostFocus中,我们刷新所选项目以根据焦点状态更改其颜色。
我的颜色与您的图像不匹配,但您可以轻松调整它们。如果您需要以非标准颜色创建画笔和钢笔,请将其创建为静态和只读,或者不要忘记将其丢弃。

  1. private static readonly Brush HotTrackBrush = new SolidBrush(new Color(123, 45, 67));
  2. private static readonly Pen HotTrackPen = new Pen(new Color(234, 56, 78));


此列表框的改进版本可以将不同的选择和热跟踪颜色显示为属性,以便您可以在属性窗口中轻松更改它们。(属性自动显示在那里。)

展开查看全部

相关问题