C# 如何更改ListView的默认选择颜色?
我正在尝试更改ListView中选择栏的默认(蓝色)颜色。C# 如何更改ListView的默认选择颜色?,c#,winforms,listview,colors,selection,C#,Winforms,Listview,Colors,Selection,我正在尝试更改ListView中选择栏的默认(蓝色)颜色。 我拒绝使用ObjectListView,因为我必须更改所有代码 我搜索了这个主题,在这里找到了一些答案: 但这指向了ObjectListView 当我以前使用ListBox时,这可以根据我的喜好设置选择栏的颜色: 将属性下的DrawMode设置为OwnerDrawFixed 将DrawItem设置为事件下的ListBox1_DrawItem private void ListBox1\u DrawItem(对象发送方,DrawIte
我拒绝使用ObjectListView,因为我必须更改所有代码 我搜索了这个主题,在这里找到了一些答案:
但这指向了ObjectListView 当我以前使用ListBox时,这可以根据我的喜好设置选择栏的颜色:
OwnerDrawFixed
private void ListBox1\u DrawItem(对象发送方,DrawItemEventArgs e)
{
如果(e.指数<0)返回;
//如果选择了项目状态,则会更改背景颜色
if((e.State&DrawItemState.Selected)=DrawItemState.Selected)
e=新的DrawItemEventArgs(如图形、,
e、 字体,
e、 界限,
e、 索引,
e、 状态^DrawItemState。已选择,
e、 前景色,
Color.FromArgb(43144188));//选择颜色
//为每个项目绘制ListBox控件的背景。
e、 牵引杆接地();
//绘制当前项文本
e、 Graphics.DrawString(lb_result.Items[e.Index].ToString(),e.Font,brush.Black,e.Bounds,StringFormat.GenericDefault);
//如果列表框具有焦点,请围绕选定项绘制一个焦点矩形。
e、 DrawFocusRectangle();
}
但我现在使用的是ListView
OwnerDraw
设置为TrueListView1\u DrawItem
如何将此代码正确用于ListView?所有者绘制ListView控件比绘制ListBox控件更复杂:需要处理更多的细节。 这是一个考虑ListView四种设置的示例:
View.Details
,View.List
,View.Tile
和View.SmallIcon
此处仅绘制文本(这就是为什么不包括View.LargeIcon
),以将代码包含在适当的范围内。下面是绘制链接到ListView的ImageList中包含的位图的示例 设置列表视图:
启用ListView
OwnerDraw
模式,然后按示例代码中所示订阅its和事件(如果希望ListView显示任何内容,则必须如此)
使用默认渲染绘制标题(设置e.DrawDefault=true
)
常见操作说明:项目文本的绘制使用:这是ListView绘制其项目所使用的原始方法。它允许精确匹配默认呈现,因此我们不会注意到文本的一些错误对齐
DrawItem
事件用于在所有视图
模式下绘制自定义背景,并将在除视图以外的所有模式下绘制项目文本。详细信息,其中DrawSubItems
事件起作用:我们将绘制第一个项目的文本两次,DrawItem
事件是否执行相同的任务
当视图
设置为平铺
或列表
时,不会调用绘图子项
事件
此处显示的代码的详细信息:辅助方法GetTextAlignment负责设置项目的对齐方式,因为每列都可以有特定的文本对齐方式 “颜色列表视图选择颜色”字段用于设置/更改所选项目的颜色。要修改选择颜色,请将此字段设置为任意值,并使其无效()列表视图:新颜色将立即应用 结果样本:
您只需要:
if(e.Item.Selected)=>
绘制背景(e.Graphics.FillRectangle()
)和文本。如果未选择,e.DrawDefault=true代码>。您可以使用e.Bounds
measure,用您想要的任何颜色填充矩形。如果ListView包含位图,您还需要绘制位图。@Jimi您能给我看一下代码示例吗?我正在努力,但没有做对。谢谢你的深入反馈。工作起来很有魅力!唯一的问题是,当我将鼠标悬停在listView中的第一个结果上时,我正在使用的其他列(listView详细信息模式)消失了:知道这是与代码相关还是需要分配鼠标指针吗?因为我在这里发布的代码没有出现这种情况,它可能与listView中我不知道的其他事件相关。你能用你现在正在使用的代码更新这个问题吗,这样我就可以测试它了?我认为它与此相关(请参见注释):这一点:这里的代码已经处理过了。阅读常见操作说明:部分。您需要实现这里描述的代码。制作一个包含3列的测试列表视图,其中包含一些项/子项,并准确地使用此代码。它的行为应该与您在图形示例中看到的一样。
private void ListBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index < 0) return;
//if the item state is selected them change the back color
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
e = new DrawItemEventArgs(e.Graphics,
e.Font,
e.Bounds,
e.Index,
e.State ^ DrawItemState.Selected,
e.ForeColor,
Color.FromArgb(43, 144, 188));//Choose the color
// Draw the background of the ListBox control for each item.
e.DrawBackground();
// Draw the current item text
e.Graphics.DrawString(lb_result.Items[e.Index].ToString(), e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
// If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle();
}
bool lvEditMode = false;
Color listViewSelectionColor = Color.Orange;
protected void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
var lView = sender as ListView;
if (lvEditMode || lView.View == View.Details) return;
TextFormatFlags flags = GetTextAlignment(lView, 0);
Color itemColor = e.Item.ForeColor;
if (e.Item.Selected) {
using (var bkBrush = new SolidBrush(listViewSelectionColor)) {
e.Graphics.FillRectangle(bkBrush, e.Bounds);
}
itemColor = e.Item.BackColor;
}
else {
e.DrawBackground();
}
TextRenderer.DrawText(e.Graphics, e.Item.Text, e.Item.Font, e.Bounds, itemColor, flags);
if (lView.View == View.Tile && e.Item.SubItems.Count > 1) {
var subItem = e.Item.SubItems[1];
flags = GetTextAlignment(lView, 1);
TextRenderer.DrawText(e.Graphics, subItem.Text, subItem.Font, e.Bounds, SystemColors.GrayText, flags);
}
}
private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
var lView = sender as ListView;
TextFormatFlags flags = GetTextAlignment(lView, e.ColumnIndex);
Color itemColor = e.Item.ForeColor;
if (e.Item.Selected && !lvEditMode) {
if (e.ColumnIndex == 0 || lView.FullRowSelect) {
using (var bkgrBrush = new SolidBrush(listViewSelectionColor)) {
e.Graphics.FillRectangle(bkgrBrush, e.Bounds);
}
itemColor = e.Item.BackColor;
}
}
else {
e.DrawBackground();
}
TextRenderer.DrawText(e.Graphics, e.SubItem.Text, e.SubItem.Font, e.Bounds, itemColor, flags);
}
protected void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
=> e.DrawDefault = true;
private TextFormatFlags GetTextAlignment(ListView lstView, int colIndex)
{
TextFormatFlags flags = (lstView.View == View.Tile)
? (colIndex == 0) ? TextFormatFlags.Default : TextFormatFlags.Bottom
: TextFormatFlags.VerticalCenter;
if (lstView.View == View.Details) flags |= TextFormatFlags.LeftAndRightPadding;
if (lstView.Columns[colIndex].TextAlign != HorizontalAlignment.Left) {
flags |= (TextFormatFlags)((int)lstView.Columns[colIndex].TextAlign ^ 3);
}
return flags;
}
private void listView1_BeforeLabelEdit(object sender, LabelEditEventArgs e) => lvEditMode = true;
private void listView1_AfterLabelEdit(object sender, LabelEditEventArgs e) => lvEditMode = false;