C#使用DataGridView模拟绘图

这篇具有很好参考价值的文章主要介绍了C#使用DataGridView模拟绘图。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

  接到一个需求,绘制一个水管线的图片,这种管线可以有12种分段方法,最后将这12种分段方法合并后在一条水管线上展示,要求:

  ⒈支持分段的属性展示;

  ⒉要求每个分段都能清晰展示,分段数在0(没有分段)~100之间,水管线长度不定;

  3、每个分段的属性都有值,可以更改,使用XML存储;

  4、不同材质管线使用不同的背景;

  5、根据不同的值,分段显示不同的颜色;

  6、支持鼠标滚动。

  因为需要快速完成,时间紧,第一时间想到使用C#的GDI+,可是在支持热点和放大与缩小时卡壳了,赶紧换了一种方法,制定一个自定义控件,在Panel上绘图,支持拖动,放大与缩小,可是总是会出现这样那样的问题,也想使用SVG,可是担心还是完不成,换成DataGridView,也算完成了,效果如下:

C#使用DataGridView模拟绘图,C#,c#,绘图

  经常在网上看到说在中国35岁以后就不能写代码了,感觉一个是热爱不够了,第二个可能是身体状况也不允许了。在一般企业中,也没有动力去费神像年轻时候完成一个程序,那个时候有冲劲和干劲,特别有耐心,希望得到别人的肯定与赞扬,现在50多岁了,没有动力了,想一想以前也是觉得可笑了。

  这个完成后,自定义一个控件就可以使用了。

   下面是完成的代码:

  定义:

        private static ToolTip toolTip = new ToolTip();
        // 定义一个List,用于存储点的信息
        List<float> Points = new List<float>() {0 };

        int MinColWidth=48;//定义最小分段对应的宽度
        float pipelineLength;//定义管线长度
        float RateF;//实际进行计算的比例 

  初始化:

            //初始化DataGridView
            dataGridView1.ColumnHeadersVisible = false;//隐藏标题栏          
            dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;//取消分割线
            //dataGridView1.ScrollBars = ScrollBars.Horizontal;
            dataGridView1.ScrollBars = ScrollBars.None;//去掉滚动条
            dataGridView1.AllowUserToAddRows = false;//不允许用户增加行
            dataGridView1.AllowUserToDeleteRows = false;//不允许用户删除行
            dataGridView1.AllowUserToResizeRows = false;//不允许用户改变行高度
            //dataGridView1.Columns.Add("Column1", "列1");
            dataGridView1.RowTemplate.Height = 32;//设置行高度
            //dataGridView1.Rows.Add();
            //dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.LightGray;
            dataGridView1.AllowUserToResizeColumns = false;//不允许用户改变栏宽度
            dataGridView1.RowHeadersVisible = false;//隐藏最前面的选择列            
            dataGridView1.Columns.Clear();//删除所有的列            
            dataGridView1.Columns.Add("Column1", "列1");//添加一列
            dataGridView1.Rows.Add();//添加一行
            dataGridView1.Rows[0].ReadOnly = true;//设置第一行为只读
            dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.LightGray;//设置行背景色           
            dataGridView1.Columns[0].Visible = false;//隐藏标题栏
                                                     
            dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;// 设置编辑模式为EditProgrammatically
            //注册事件
            dataGridView1.CellPainting += dataGridView1_CellPainting;//单元格渲染
            dataGridView1.CellFormatting += dataGridView1_CellFormatting;
            dataGridView1.MouseEnter += dataGridView1_MouseEnter;//鼠标移入
            dataGridView1.MouseLeave += dataGridView1_MouseLeave;//鼠标离开
            dataGridView1.MouseWheel += dataGridView1_MouseWheel;//鼠标滚动
            dataGridView1.RowPrePaint+=dataGridView1_RowPrePaint;//行渲染

            //List<float> P = new List<float>() { 12, 27, 41, 73, 89, 105, 119, 126, 138, 166, 192, 208, 255, 377, 410, 439 };
            List<float> P = new List<float>() { 31, 25, 45,39, 73, 89, 115, 121 };
            UpdatePoints(P, 138);

  事件以及函数内容:

        void UpdatePoints(List<float> NewPoints,float NowDistance)
        {
            Points = NewPoints;
            pipelineLength = NowDistance;

            Points.Add(0);
            Points.Add(pipelineLength);
            Points.Sort();

            //计算实际的换算比例
            RateF = ((dataGridView1.Width - (Points.Count - 1) * 5) / pipelineLength);
            textBox1.Text += "计算比例:" + RateF.ToString() + Environment.NewLine;
            textBox1.Text += "管线长度:" + pipelineLength.ToString() + Environment.NewLine;
            textBox1.Text += "显示宽度:" + dataGridView1.Width.ToString() + Environment.NewLine;
            //得到最小的分段
            float MinSectionLength = CalculateMinDistance();
            textBox1.Text += "最小分段:" + MinSectionLength.ToString() + Environment.NewLine;
            if (MinSectionLength * RateF < MinColWidth)
            {
                //重新设定比例
                RateF = MinColWidth / MinSectionLength;
            }

            textBox1.Text += "最后比例:" + RateF.ToString() + Environment.NewLine;

            //更新DataGridView
            //设置每一列的宽度
            //foreach (int point in Points)
            for(int i=1; i<Points.Count; i++)
            {
               float ColWidth = (Points[i] - Points[i - 1]) * RateF;
                AddColumn(dataGridView1,"Section", (int)ColWidth,  Color.Brown, Color.Cyan, Points[i - 1], Points[i]);//增加分段
                if (i!=Points.Count-1)
                {
                    AddColumn(dataGridView1, "Dot", 5, Color.Brown, Color.Blue, Points[i - 1], Points[i]);//增加点
                }                
            }
        }

        // 动态增加列
        void AddColumn(DataGridView dataGridView, string StrType, int width, Color textColor, Color cellBackColor,float StartDot,float EndDot)
        {
            DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
            column.Width = width;
            //column.HeaderText = text;
            column.DefaultCellStyle.ForeColor = textColor;
            column.DefaultCellStyle.BackColor = cellBackColor;

            // 为每一列添加属性
            column.Tag = new ColumnProperty
            {
                SectionType = StrType,
                StartPoint = StartDot,
                EndPoint = EndDot,
                Distance = EndDot - StartDot
            };
            dataGridView.Columns.Add(column);
            // 获取第一行
            DataGridViewRow firstRow = dataGridView.Rows[0];

            // 设置特定列的单元格属性
            //firstRow.Cells[column.Index].Value = text;
            firstRow.Cells[column.Index].Style.BackColor = cellBackColor;
            firstRow.Cells[column.Index].Style.ForeColor = Color.Red;

            //dataGridView.Columns.Add(column);
        }

        class ColumnProperty
        {
            public int SectionIndex { get; set; }   //分段序号
            public string SectionType { get; set; } //类型
            public float StartPoint { get; set; }  //起点
            public float EndPoint { get; set; }    //终点
            public float Distance { get; set; }      //距离
            public string Rule01 { get; set; }      //
            public string Rule02 { get; set; }      //
            public string Rule03 { get; set; }      //
            public string Rule04 { get; set; }      //
            public string Rule05 { get; set; }      //
            public string Rule06 { get; set; }      //
            public string Rule07 { get; set; }      //
            public string Rule08 { get; set; }      //
            public string Rule09 { get; set; }      //
            public string Rule10 { get; set; }      //
            public string Rule11 { get; set; }      //
            public string Rule12 { get; set; }      //管理站队
            public float Pvalue { get; set; }       //失效可能性
            public float Cvalue { get; set; }       //风险
            public float Rvalue { get; set; }       //后果
        }
        
        float CalculateMinDistance()
        {
            // 实现计算功能,计算相邻两个点之间的距离,得到最小值
            List<float> segmentLengths = new List<float>();
            for (int i = 0; i < Points.Count - 1; i++)
            {
                float segmentLength = Points[i + 1] - Points[i];
                segmentLengths.Add(segmentLength);
            }
            float minSegmentLength = segmentLengths.Count > 0 ? segmentLengths.Min() : pipelineLength;
            return FloatFormat(minSegmentLength);
        }

        private float FloatFormat(float f)
        {
            return (float)Math.Round(f, 2); ;
        }

        private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
        {
            // 判断是否是水平滚动
            if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
            {
                // 水平滚动内容
                dataGridView1.HorizontalScrollingOffset = e.NewValue;
            }
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            //if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            //{
            //    if (e.ColumnIndex % 2 == 0)
            //    {
            //        e.CellStyle.BackColor = Color.DarkGray;
            //    }
            //    else
            //    {
            //        e.CellStyle.BackColor = Color.LightGray;
            //    }
            //}
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && e.ColumnIndex % 2 == 0)
            //{
            //    dataGridView1.Rows[e.RowIndex].DefaultCellStyle.SelectionBackColor = Color.Transparent;
            //    dataGridView1.Rows[e.RowIndex].DefaultCellStyle.SelectionForeColor = dataGridView1.DefaultCellStyle.ForeColor;
            //}
        }

        private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            e.PaintParts &= ~DataGridViewPaintParts.Background;
            if (e.RowIndex >= 0)
            {
                Image image = Image.FromFile("D:/CSharp/TestTreeview/WinFormsApp2/WinFormsApp2/image/TRQpipeline.jpg"); // 替换为实际的图片路径
                Rectangle rect = new Rectangle(e.RowBounds.Left, e.RowBounds.Top, dataGridView1.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - 1, e.RowBounds.Height - 1);
                e.Graphics.DrawImage(image, rect);
            }
        }

        private void dataGridView1_MouseEnter(object sender, EventArgs e)
        {
            dataGridView1.BorderStyle = BorderStyle.FixedSingle;
            //dataGridView1.BorderColor = Color.Red;            
        }

        private void dataGridView1_MouseLeave(object sender, EventArgs e)
        {
            dataGridView1.BorderStyle = BorderStyle.None;
        }

        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            //if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            //{
            //    // 设置单元格背景颜色
            //    e.CellStyle.BackColor = Color.Red;

            //    // 设置单元格背景色的透明度
            //    e.CellStyle.BackColor = Color.FromArgb(192, e.CellStyle.BackColor);
            //}

            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                if (e.ColumnIndex % 2 != 0)
                {
                    // 设置偶数列单元格的前景色为半透明背景色
                    using (Brush brush = new SolidBrush(Color.FromArgb(0, Color.White)))
                    {
                        e.Graphics.FillRectangle(brush, e.CellBounds);
                    }
                }
                else
                {
                    // 设置奇数列单元格的前景色为深灰色并覆盖行背景色
                    using (Brush brush = new SolidBrush(Color.Black))
                    {
                        e.Graphics.FillRectangle(brush, e.CellBounds);
                    }
                }

                // 绘制单元格内容
                e.PaintContent(e.CellBounds);
                e.Handled = true;
            }

        }

        // 处理鼠标滚轮事件
        private void dataGridView1_MouseWheel(object sender, MouseEventArgs e)
        {
            int delta = e.Delta;
            int newOffset = dataGridView1.HorizontalScrollingOffset - delta;

            // 检查滚动范围是否超出边界
            if (newOffset < 0)
            {
                newOffset = 0; // 将滚动范围限制在最左边
            }
            else if (newOffset > GetHorizontalScrollingOffsetMax())
            {
                newOffset = GetHorizontalScrollingOffsetMax(); // 将滚动范围限制在最右边
            }

            // 设置新的水平滚动偏移量
            dataGridView1.HorizontalScrollingOffset = newOffset;
        }


        private int GetHorizontalScrollingOffsetMax()
        {
            int maxOffset = 0;
            foreach (DataGridViewColumn column in dataGridView1.Columns)
            {
                maxOffset += column.Width;
            }
            maxOffset -= dataGridView1.ClientSize.Width;
            return maxOffset;
        }

        private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex % 2 != 0)
            {
                DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
                ColumnProperty properties = (ColumnProperty)row.Cells[e.ColumnIndex].OwningColumn.Tag;
                string tooltip = $"Type: {properties.SectionType}\nStart Point: {properties.StartPoint}\nEnd Point: {properties.EndPoint}";
                toolTip.SetToolTip(dataGridView1, tooltip);
            }
            else
            {
                string tooltip = "";
                toolTip.SetToolTip(dataGridView1, tooltip);
            }
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //判断分段属性如果是Section
            if (e.RowIndex >= 0)
            {
                DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
                ColumnProperty properties = (ColumnProperty)row.Cells[e.ColumnIndex].OwningColumn.Tag;
                if (properties.SectionType == "Section")
                {
                    MessageBox.Show(properties.StartPoint.ToString()+ "|"+properties.EndPoint.ToString());
                }
            }
        }

  功能是完成了,后面还需要完善细节,就是属性值的更新,主要是TreeView与XML内容的同步,包括自动计算。

  文章来源地址https://www.toymoban.com/news/detail-574002.html

到了这里,关于C#使用DataGridView模拟绘图的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • C# Winform DataGridView 控件和 DataTable

    目录 一、概述 二、DataTable 的用法 1.创建表和列 2.添加行 3.取值和赋值 4.删除行 5.遍历 DataTable 6.判断 DataTable 列中是否存在某个值 7.设置主键 8.获取 DataRow 所在的行号 9.DataTable 转换为 List 10.将 List 转 DataTable 三、DataGridView 的用法 1.绑定数据 2.获取绑定的数据源 3.获取 / 设置

    2024年02月10日
    浏览(32)
  • C# Winfrom将DataGridView数据导入Excel

      1.项目添加Word和Excel的COM类型库引用 2.创建Excel工作表 3.设置Excel表格的标题 4.加载数据进入Excel表格 5.显示和释放工作簿对象 6.补充         一:                 在前面老是用到Microsoft.Office.Interop.Excel这个命名空间,可以给它起个别名方便书写                 例

    2024年02月16日
    浏览(31)
  • C#中的DataGridView中添加按钮并操作数据

    目录 背景 一、在DataGridView中显示需要的按钮 二、给DataGridView添加事件,可以通过按钮来操作数据库 三、在按钮上鼠标箭头变成小手样式 四、总结 最近在项目中有需求需要在DataGridView中添加“删除”、“修改”按钮,用来对数据的操作以及显示。  首先在DataGridView中添加需

    2023年04月11日
    浏览(24)
  • C# winform 将excel表格导入datagridView 的方式

    方式一: 方式二:

    2024年02月05日
    浏览(36)
  • C# 读取 Excel xlsx 文件,显示在 DataGridView 中

    编写 read_excel.cs 如下 SET PATH=C:WINDOWSMicrosoft.NETFrameworkv4.0.30319;%PATH% 编译:csc.exe  /t:exe read_excel.cs   环境:win10 64位系统 运行 yourpathread_excel.exe  test1.xlsx 错误信息:未在本地计算机上注册“ Microsoft.ACE.OLEDB.12.0 ”提供程序。 搜索 Microsoft Access Database Engine 2016  我先下载了

    2024年02月07日
    浏览(37)
  • C# Winform中在DataGridView中添加Button按钮,操作Button按钮

    .Net的DataGridView控件中,提供了一种列的类型,叫 DataGridViewButtonColumn ,这种列类型是展示为一个 按钮,可以给button赋予相应的text,并且,此button可以用来做处理事件的判断依据。 DataGridViewButtonColumn,虽然在UI展现上,是一个BUTTON的样子,但是,它的实际形态,并不是传统意

    2024年02月07日
    浏览(38)
  • c# 从零到精通 读取连接数据库-并将数据填入dataGridView控件中

    c# 从零到精通 读取连接数据库-并将数据填入dataGridView控件中 using System; using System.Data; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace Test01 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { SqlConnection conn

    2024年02月09日
    浏览(35)
  • 循环可变化的集合 数组 datatable 等 || c# winfrom DataGridView 动态UI下载功能

          1,使用组件DataGridView 2,使用DataSource来控制表格展示的数据来源(注意:来源需要是DataTable类型) 3,需要用到异步线程。如果是不控制数据源的话,需要使用UI安全线程;(使用Control.Invoke或Control.BeginInvoke方法) 4,DataGridView的列如果设置图片,尽量代码设置 5,DataT

    2024年02月19日
    浏览(29)
  • C# Winfrom通过COM接口访问和控制Excel应用程序,将Excel数据导入DataGridView

    1.首先要创建xlsx文件 2.在Com中添加引用 3. 添加命名空间         using ApExcel = Microsoft.Office.Interop.Excel;         --这样起个名字方面后面写 4.样例 5.获取sheet.Cells值的时候也可以不使用强制转换,还要注意这个的下标是从1开始的

    2024年02月11日
    浏览(36)
  • c# winfrom DataGridView 动态UI下载功能(内含GIF图) || 循环可变化的集合 数组 datatable 等

      1,使用组件DataGridView 2,使用DataSource来控制表格展示的数据来源(注意:来源需要是DataTable类型) 3,需要用到异步线程。如果是不控制数据源的话,需要使用UI安全线程;(使用Control.Invoke或Control.BeginInvoke方法) 4,DataGridView的列如果设置图片,尽量代码设置 5,DataTable类型

    2024年02月20日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包