c# listview 自绘相关

 

2、在这个程序上改?

复制代码
sing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Net;

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Globalization;
using System.Windows.Forms;

namespace testcsharp1
{
    public partial class Form1 : Form
    {

        private void MyListView_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
           // e.Graphics.FillRectangle(Brushes.Yellow, e.Bounds);
            e.DrawText();
        }
        private void listView1_DrawColumnHeader(object sender,DrawListViewColumnHeaderEventArgs e)
        {
            /*
            using (var sf = new StringFormat())
            {
                sf.Alignment = StringAlignment.Center;

                

                using (var headerFont = new Font("Microsoft Sans Serif", 9, FontStyle.Regular))
                {
                    e.Graphics.FillRectangle(Brushes.Pink, e.Bounds);
                    e.Graphics.DrawString(e.Header.Text, headerFont,
                        Brushes.Black, e.Bounds, sf);
                }
            }

            return;
            */
            using (StringFormat sf = new StringFormat())
            {
                // Store the column text alignment, letting it default
                // to Left if it has not been set to Center or Right.
                switch (e.Header.TextAlign)
                {
                    case HorizontalAlignment.Center:
                        sf.Alignment = StringAlignment.Center;
                        break;
                    case HorizontalAlignment.Right:
                        sf.Alignment = StringAlignment.Far;
                        break;
                }

                // Draw the standard header background.
                e.DrawBackground();

                // Draw the header text.
                using (Font headerFont =
                            new Font("宋体", 10, FontStyle.Regular))
                {
                    //e.Graphics.FillRectangle(Brushes.Pink, e.Bounds);
                    Rectangle r2 = e.Bounds;
                    r2.X = r2.X + 1;
                    //Brushes.BurlyWood,
                    e.Graphics.FillRectangle(Brushes.SkyBlue, r2);

                    Rectangle r1 = e.Bounds;
                    r1.Y = r1.Y + 4;


                   //e.Graphics.DrawString(e.Header.Text, headerFont,  Brushes.Black, e.Bounds, sf);

                   e.Graphics.DrawString(e.Header.Text, headerFont,Brushes.Black, r1, sf);

                    /*
                    Point tPoint = new Point();
                    SolidBrush tFrontBrush = new SolidBrush(Color.Black);
                    Font tFont = new Font("宋体", 9, FontStyle.Regular);
                    tPoint.X = e.Bounds.X ;
                    tPoint.Y = e.Bounds.Y + 8;
                    e.Graphics.DrawString(e.Header.Text, tFont, tFrontBrush, tPoint);
                    */
                }
            }
            return;
        }
        private void MyListView_DrawItem(object sender, DrawListViewItemEventArgs e)
        {

            e.DrawBackground();
            e.DrawFocusRectangle();
            return;
            /*
            e.DrawBackground();
            e.DrawFocusRectangle();

            if ((e.State & ListViewItemStates.Selected) != 0)
            {
                // Draw the background and focus rectangle for a selected item.
                e.Graphics.FillRectangle(Brushes.Maroon, e.Bounds);
                e.DrawFocusRectangle();
            }
            else
            {
                // Draw the background for an unselected item.
                using (LinearGradientBrush brush =
                    new LinearGradientBrush(e.Bounds, Color.White,
                    Color.Maroon, LinearGradientMode.Horizontal))
                {
                    e.Graphics.FillRectangle(brush, e.Bounds);
                }
            }
            return;*/

            if ((e.State & ListViewItemStates.Selected) != 0)
            {
                // Draw the background and focus rectangle for a selected item.
                e.Graphics.FillRectangle(Brushes.Maroon, e.Bounds);
                e.DrawFocusRectangle();
            }
            else
            {
                // Draw the background for an unselected item.
                using (LinearGradientBrush brush =
                    new LinearGradientBrush(e.Bounds, Color.Orange,
                    Color.Maroon, LinearGradientMode.Horizontal))
                {
                    e.Graphics.FillRectangle(brush, e.Bounds);
                }
            }
            e.DrawText();
            // Draw the item text for views other than the Details view.
            if (listView1.View != View.Details)
            {
                e.DrawText();
            }
            
            //e.DrawDefault = true;
        }

        private void MyListView_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {


           // e.DrawDefault = true;
          //  return;

            /*
            string searchTerm = "Term";
            int index = e.SubItem.Text.IndexOf(searchTerm);
            if (index >= 0)
            {
                string sBefore = e.SubItem.Text.Substring(0, index);

                Size bounds = new Size(e.Bounds.Width, e.Bounds.Height);
                Size s1 = TextRenderer.MeasureText(e.Graphics, sBefore, this.Font, bounds);
                Size s2 = TextRenderer.MeasureText(e.Graphics, searchTerm, this.Font, bounds);

                Rectangle rect = new Rectangle(e.Bounds.X + s1.Width, e.Bounds.Y, s2.Width, e.Bounds.Height);
                e.Graphics.FillRectangle(new SolidBrush(Color.Yellow), rect);
            }
            */
           // e.DrawText();


            if (e.ItemIndex == 1) { 
                using (StringFormat sf = new StringFormat())
                {
                    sf.Alignment = StringAlignment.Center;
                    e.Graphics.DrawString(e.SubItem.Text, listView1.Font, Brushes.Red, e.Bounds, sf);
                }
            }
            else e.DrawText();
            return;


            e.DrawDefault = true;
        }
        private void listView1_DrawSubItem(object sender,
          DrawListViewSubItemEventArgs e)
        {
            TextFormatFlags flags = TextFormatFlags.Left;

            using (StringFormat sf = new StringFormat())
            {
                // Store the column text alignment, letting it default
                // to Left if it has not been set to Center or Right.
                switch (e.Header.TextAlign)
                {
                    case HorizontalAlignment.Center:
                        sf.Alignment = StringAlignment.Center;
                        flags = TextFormatFlags.HorizontalCenter;
                        break;
                    case HorizontalAlignment.Right:
                        sf.Alignment = StringAlignment.Far;
                        flags = TextFormatFlags.Right;
                        break;
                }

                // Draw the text and background for a subitem with a 
                // negative value. 
                double subItemValue;
                if (e.ColumnIndex > 0 && Double.TryParse(
                    e.SubItem.Text, NumberStyles.Currency,
                    NumberFormatInfo.CurrentInfo, out subItemValue) &&
                    subItemValue < 0)
                {
                    // Unless the item is selected, draw the standard 
                    // background to make it stand out from the gradient.
                    if ((e.ItemState & ListViewItemStates.Selected) == 0)
                    {
                        e.DrawBackground();
                    }

                    // Draw the subitem text in red to highlight it. 
                    e.Graphics.DrawString(e.SubItem.Text,
                        listView1.Font, Brushes.Red, e.Bounds, sf);

                    return;
                }

                // Draw normal text for a subitem with a nonnegative 
                // or nonnumerical value.
                e.DrawText(flags);
            }
        }
        private void listView1_MouseMove(object sender, MouseEventArgs e)
        {
            ListViewItem item = listViewTestItems.GetItemAt(e.X, e.Y);
            if (item != null && item.Tag == null)
            {
                listViewTestItems.Invalidate(item.Bounds);
                item.Tag = "tagged";
            }
        }

        // Resets the item tags. 
        void listView1_Invalidated(object sender, InvalidateEventArgs e)
        {
            foreach (ListViewItem item in listViewTestItems.Items)
            {
                if (item == null) return;
                item.Tag = null;
            }
        }

        // Forces the entire control to repaint if a column width is changed.
        void listView1_ColumnWidthChanged(object sender,
            ColumnWidthChangedEventArgs e)
        {
            listViewTestItems.Invalidate();
        }
        private void listView1_MouseUp(object sender, MouseEventArgs e)
        {
            ListViewItem clickedItem = listViewTestItems.GetItemAt(5, e.Y);
            if (clickedItem != null)
            {
                clickedItem.Selected = true;
                clickedItem.Focused = true;
            }
        }

        private ListView listView1 = new ListView();

        public Form1()
        {
            InitializeComponent();

            //--------------------------------------------
            //listViewTestItems.OwnerDraw = true;

            listView1.OwnerDraw = true;
            //listView1.GridLines = true;

    
            //listViewTestItems.DrawColumnHeader += new DrawListViewColumnHeaderEventHandler(MyListView_DrawColumnHeader);
            listView1.DrawColumnHeader += new DrawListViewColumnHeaderEventHandler(listView1_DrawColumnHeader);
            
            listView1.DrawItem += new DrawListViewItemEventHandler(MyListView_DrawItem);
            listView1.DrawSubItem += new DrawListViewSubItemEventHandler(MyListView_DrawSubItem);
            //listView1.DrawSubItem += new DrawListViewSubItemEventHandler(listView1_DrawSubItem);

            //listView1.MouseMove += new MouseEventHandler(listView1_MouseMove);
           // listView1.ColumnWidthChanged += new ColumnWidthChangedEventHandler(listView1_ColumnWidthChanged);
            //listView1.Invalidated += new InvalidateEventHandler(listView1_Invalidated);
 
            listView1.Dock = DockStyle.Fill;
            listView1.View = View.Details;
            listView1.FullRowSelect = true;

            //--------------------------------------------

            listView1.HideSelection = false;
            listView1.FullRowSelect = true;
            this.listView1.MultiSelect = false;
            this.listView1.View = System.Windows.Forms.View.Details;

            this.listView1.Columns.Add("测试项", 150, HorizontalAlignment.Center);
            this.listView1.Columns.Add("内容", 250, HorizontalAlignment.Center);
            this.listView1.Columns.Add("进展", 100, HorizontalAlignment.Center);
            this.listView1.Columns.Add("结果", 150, HorizontalAlignment.Center);

            ImageList imgList = new ImageList();
            imgList.ImageSize = new Size(1, 20);
            // 这里设置listView的SmallImageList ,用imgList将其撑大
            listView1.SmallImageList = imgList;

            this.listView1.Items.Clear();

            ListViewItem lvi = new ListViewItem();
            lvi.Text = "连接设备";
            lvi.SubItems.Add("检查本地IP");
            lvi.SubItems.Add("完成");
            lvi.SubItems.Add("成功");
            lvi.UseItemStyleForSubItems = false;
            this.listView1.Items.Add(lvi);
            ListViewItem lvi1 = new ListViewItem();
            lvi1.Text = "LED测试";
            lvi1.SubItems.Add("全灭失败"); //失败这一栏显示失败项???
            lvi1.SubItems.Add("完成");
            lvi1.SubItems.Add("失败");
            lvi1.UseItemStyleForSubItems = false;
            this.listView1.Items.Add(lvi1);

            this.panel1.Controls.Add(listView1);

            //this.listView1.Items[1].SubItems[3].BackColor = Color.Red;
            this.listView1.Items[1].SubItems[3].ForeColor = Color.Red;

            //this.Controls.Add(listView1);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }
    }
}
复制代码

 

 

 

1、

比如这个

https://blog.csdn.net/ld490832353/article/details/10149499

2、晚上要看video

posted @   cnchengv  阅读(101)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2018-10-20 录制
点击右上角即可分享
微信分享提示