Bas*_*tiz 4 .net c# listview selection winforms
我正在尝试更改 ListView 中选择栏的默认(蓝色)颜色。
我拒绝使用 ObjectListView 因为我必须更改所有代码。
我搜索过这个主题,并在这里找到了一些答案:
更改 ListView 的背景选择颜色?
但它指向 ObjectListView。
当我之前使用 ListBox 时,这可以根据我的喜好设置选择栏颜色:
OwnerDrawFixed在“属性”下 将“绘制模式”设置为ListBox1_DrawItemEvents 下 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();
}
Run Code Online (Sandbox Code Playgroud)
但我现在使用ListView。
OwnerDraw为True ListView1_DrawItem ...并使用上面的代码。
我希望它能向我显示不同的选择颜色,但我收到了一些错误:
我如何正确地将此代码用于 ListView?
所有者绘制 ListView 控件比 ListBox 控件更复杂:需要处理更多细节。此示例考虑了ListView 的四个视图设置:
View.Details、View.List和View.Tile。View.SmallIcon
这里仅绘制文本(这就是View.LargeIcon不包括在内的原因),以将代码包含在适当的限制内。这里
是绘制链接到 ListView 的 ImageList 中包含的位图的示例。
设置 ListView:
启用 ListViewOwnerDraw模式,然后订阅其DrawItem、DrawSubItem和DrawColumnHeader事件,如示例代码所示(强制,如果您希望 ListView 显示任何内容)。
标题使用默认渲染(设置e.DrawDefault = true)进行绘制。
常见操作说明:
项目文本是使用TextRenderer.DrawText绘制的:这是 ListView 用来绘制其项目的原始方法。它允许完全匹配默认渲染,因此我们不会注意到文本的一些未对齐情况。
该DrawItem事件用于在所有模式下绘制自定义背景,并将在除View.DetailsView之外的所有模式下绘制 Items 的文本,其中事件发挥作用:如果事件执行相同的任务,我们将绘制第一个 Item 的文本两次。DrawSubItemsDrawItem
当设置为或时,不会调用该DrawSubItems事件。ViewTileList
有关此处提供的代码的详细信息:
辅助方法GetTextAlignment负责设置项目的对齐方式,因为每个列都可以有特定的文本对齐方式。
字段 ,Color listViewSelectionColor用于设置/更改所选项目的颜色。要修改选择颜色,请将此字段设置为任意值,并且Invalidate()ListView:将立即应用新颜色。
结果示例:
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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2844 次 |
| 最近记录: |