gpt4 book ai didi

c# - 在 WinForms ListView C# 中删除突出显示并添加选择边框

转载 作者:太空宇宙 更新时间:2023-11-03 11:04:34 24 4
gpt4 key购买 nike

经过一番搜索,我还没有找到我的具体问题。

我想在 C# 中的 WinForm 上更改 ListView 选择的默认行为

我需要这样做,因为我在单元格中使用自定义颜色来表示用户所需的元信息。

(我只使用单行选择,即 MutiSelect = false;)

当我在 ListView 中选择一行时,整行默认以蓝色突出显示,

Selected Row with Blue Background

相反,我想知道,

如何勾勒出行的边框而不更改行中单元格的颜色?

如下所示

Selected Row without Blue Background and dotted line

最佳答案

是的,ListView 通过将 OwnerDraw 属性设置为 True 来支持自定义绘图。这往往很复杂,但您的需求很简单,您可以在这里使用很多默认绘图。只有当一个项目被选中时,你才需要一些不同的东西。 ControlPaint 类可以绘制您想要的虚线矩形。实现三个 Draw 事件处理程序,如下所示:

    private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e) {
e.DrawDefault = true;
}

private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e) {
e.DrawBackground();
e.DrawText();
if ((e.State & ListViewItemStates.Selected) == ListViewItemStates.Selected) {
var bounds = e.Bounds;
bounds.Inflate(-1, -1);
ControlPaint.DrawFocusRectangle(e.Graphics, bounds);
}
}

private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e) {
e.DrawBackground();
e.DrawText();
if ((e.ItemState & ListViewItemStates.Selected) == ListViewItemStates.Selected) {
var bounds = e.Bounds;
bounds.Inflate(-1, -1);
ControlPaint.DrawFocusRectangle(e.Graphics, bounds);
}
}

根据需要调整。请注意,您可能还想实现 MouseDown 事件,以便用户可以单击任何子项并选择该行。现在还不清楚它的行为是否像 ListView。使用 HitTest() 方法来实现它。

关于c# - 在 WinForms ListView C# 中删除突出显示并添加选择边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16396969/

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