zoukankan      html  css  js  c++  java
  • C#画表格

    下面给一个简单的例子,至于多个单元格合并,请自己去实现,也就是坐标计算的事情。

    至于画图,用GDI,还是DirectX画,自己选择,不过这里主要讲的是算法:坐标计算以及画的过程。

    注意不要每个列都画一个矩形,很浪费的,因为都重叠了。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    using System;
    using System.Drawing;
    using System.Windows.Forms;
     
    namespace GDITableDemo {
        public partial class Form1 : Form {
            public Form1() {
                InitializeComponent();
            }
     
            private void button1_Click(object sender, EventArgs e) {
                string[] columns = textBox1.Text.Split(',');
     
                Font font = panel1.Font;//字体
                Brush color = Brushes.Black;//颜色
                Brush border = new SolidBrush(panel1.ForeColor);//用前景色画边框
                Pen borderStyle =new Pen(border, 1);
                 
                //从什么位置开始画
                float top = 0F;//Y坐标
                float left = 0F;//X坐标
                //画笔X坐标偏移量,left1:最后一次位置,left2当前最远位置
                float left1 = left, left2 = 0F;
                float textLeft=0F;//文本X坐标
                float textTop = 0F;//文本Y坐标
                float textWidth=0F;//文本宽度
                float textHeight=0F;//文本高度
                const float columnHeight = 30F;//行高,包括边框在内
                const float columnPadding = 10F;//每一列左右多出10像素
     
                Graphics g = Graphics.FromHwnd(panel1.Handle);
                textHeight = font.GetHeight(g);//高
                textTop = (columnHeight - textHeight) / 2;//上边
                for (int i = 0; i < columns.Length; i++) {
                    //先计算文本
                    textWidth= g.MeasureString(columns[i],font).Width;//宽
                    textLeft = left1 + columnPadding;//左边
                    left2=textLeft+textWidth+columnPadding;
     
                    //先画左边框
                    g.DrawLine(borderStyle, left1, top, left1, columnHeight);
     
                    //画文字
                    g.DrawString(columns[i], font, color, textLeft, textTop);
                    //注意左边的位置要开始偏移了
                    left1 = left2;
                }
                g.DrawLine(borderStyle, left, top, left2, top);//上边框
                g.DrawLine(borderStyle, left, columnHeight, left2, columnHeight);//下边框
                g.DrawLine(borderStyle, left2, top, left2, columnHeight);//右边框
     
            }
        }
    }
  • 相关阅读:
    Servlet(简介,请求参数,页面跳转,生命周期,创建,配置,ServletContext,线程)
    zookeeper 集群配置
    centos yum 安装 mariadb
    Oracle jdbc 插入 clob blob
    oracle 删除用户,提示“无法删除当前已连接的用户”
    oracle 创建表空间,用户并授权
    Linux 根分区扩容
    同时运行多个 tomcat 修改端口
    mysql新建用户及授权
    php sql 类似 mybatis 传参
  • 原文地址:https://www.cnblogs.com/zcm123/p/6535083.html
Copyright © 2011-2022 走看看