亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何更改 ListView 的默認選擇顏色?

如何更改 ListView 的默認選擇顏色?

C#
慕田峪7331174 2022-11-22 16:44:07
我正在嘗試更改 ListView 中選擇欄的默認(藍色)顏色。我拒絕使用 ObjectListView,因為我必須更改所有代碼。我搜索了這個主題并在這里找到了一些答案:Change background selection color of ListView?但這指向 ObjectListView。當我以前使用 ListBox 時,這可以根據我的喜好設置選擇欄顏色:將 DrawMode 設置為OwnerDrawFixed屬性下將 DrawItem 設置為ListBox1_DrawItem事件下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();}但我現在正在使用 ListView。我設置OwnerDraw為真我將 DrawItem 設置為ListView1_DrawItem...并使用上面的代碼。我希望它能按照說明向我顯示不同的選擇顏色,但我卻遇到了一些錯誤:我將如何為 ListView 正確使用此代碼?
查看完整描述

1 回答

?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

所有者繪制 ListView 控件比 ListBox 控件更復雜:需要處理更多細節。這是一個考慮ListView 的四個視圖設置的示例
View.Details、View.List和。View.TileView.SmallIcon

此處僅繪制文本(這View.LargeIcon就是不包括在內的原因),以將代碼包含在合適的范圍內。此處
是繪制鏈接到 ListView 的 ImageList 中包含的位圖的示例。

設置 ListView
啟用您的 ListViewOwnerDraw模式,然后訂閱它的DrawItem、DrawSubItemDrawColumnHeader事件,如示例代碼所示(強制,如果您希望 ListView 顯示任何內容)。

使用默認渲染(設置e.DrawDefault = true)繪制標題。

常用操作說明
使用TextRenderer.DrawText繪制 Item Text :這是 ListView 用于繪制其項目的原始方法。它允許完全匹配默認呈現,因此我們不會注意到文本的一些未對齊。

DrawItem事件用于在所有模式下繪制自定義背景,并將在除View.DetailsView之外的所有模式下繪制項目的文本,事件開始發揮作用:如果事件執行相同的任務,我們將繪制第一個項目的文本兩次.DrawSubItemsDrawItem

當設置為時,不會調用DrawSubItems事件。ViewTileList

此處提供的代碼的詳細信息
輔助方法GetTextAlignment負責設置項目的對齊方式,因為每個列都可以具有特定的文本對齊方式。

字段Color listViewSelectionColor用于設置/更改所選項目的顏色。要修改選擇顏色,請將此字段設置為任何值,Invalidate()然后 ListView: 將立即應用新顏色。

結果樣本

https://i.stack.imgur.com/NTpWn.gif

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;  


查看完整回答
反對 回復 2022-11-22
  • 1 回答
  • 0 關注
  • 228 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號