利用C#的画图工具做表格

本文介绍了如何使用C#编程语言来实现画图功能,特别是创建一个简单的表格。通过提供的代码示例,读者可以了解绘制表格的具体步骤和技巧。
摘要由CSDN通过智能技术生成

利用C#画出如下的表格
在这里插入图片描述
代码附上

private void UpdateTable()
{
   
    /* 生成表格文件 */

    int x1 = 55;       /* 表框起始横坐标 */
    int y1 = 100;      /* 表框起始纵坐标 */
    int x2 = x1 + 2;   /* 文字起始横坐标 */
    int y2 = y1 + 5;   /* 文字起始纵坐标 */
    int z = 30;        /* 行宽 */
    int rowHead = 0;   /* 每行数据的高度初始位置 */
    int columnHead = 200; /* 第一列宽度 */

    int zWidth = 141;             /* 测量数据列宽 */
    int zWidthTwice = zWidth * 2; /* 两倍测量数据列宽 */

    int sum = 5;       /* 目标位置点个数 */
    int row = 3;         /* 测量次数 */
    int tempDelta = 50;  /* 调整文字居中变量 */

    int width = 320 + sum * zWidthTwice;             /* 图片宽度 */
    int height = 940 + 2 * z * row;                  /* 图片高度 */

    int tableWidth = columnHead + sum * zWidthTwice; /* 表格宽度 */

    Bitmap bmp = new Bitmap(width, height);          /* 新建一个图片对象 */
    Graphics g = Graphics.FromImage(bmp);            /* 利用该图片对象生成画板 */

    Font font = new Font("宋体", 15);                /* 设置字体颜色 */
    SolidBrush brush = new SolidBrush(Color.Black);  /* 新建一个画刷,到这里为止,我们已经准备好了画板、画刷和数据 */
    Pen p = new Pen(Color.Black, 1);                 /* 定义了一个黑色,宽度为1的画笔 */
    g.Clear(Color.FromArgb(255, 255, 255));          /* 设置白色背景 */

    /* 第1行数据 */
    rowHead = 0;
    g.DrawRectangle(p, x1, y1 + rowHead, tableWidth, z);            /* 在画板上画矩形,起始坐标为(x1,y1),宽为800,高为30 */
    string tempTitle = "GB/T 17421.2-2000 统计数表————线性测校————线性位移(x)";
    g.DrawString(tempTitle, font, brush, x2, y2);
    /* 绘制文字,起始坐标为(x2, y2) */


    /* 第2行数据 */
    rowHead = z;
    g.DrawRectangle(p, x1, y1 + rowHead, columnHead + zWidthTwice, z);
    g.DrawRectangle(p, x1 + columnHead + zWidthTwice, y1 + rowHead, zWidthTwice, z);
    g.DrawRectangle(p, x1 + columnHead + 2 * zWidthTwice, y1 + rowHead, zWidthTwice, z);
    g.DrawRectangle(p, x1 + tableWidth - (sum - 3) * zWidthTwice, y1 + rowHead, (sum - 3) * zWidthTwice, z);
    g.DrawString("机器编号:" + GlobalData.objMachineName, font, brush, x2, y2 + rowHead);
    g.DrawString("轴:" + GlobalData.objAxis, font, brush, x2 + columnHead + zWidthTwice, y2 + rowHead);
    g.DrawString("日期:" + GlobalData.objTime, font, brush, x2 + columnHead + 2 * zWidthTwice, y2 + rowHead);
    g.DrawString("操作者:" + GlobalData.objWorker, font, brush, x2 + tableWidth - (sum - 3) * zWidthTwice, y2 + rowHead);

    /* 第3行数据 */
    rowHead += z;
    g.DrawRectangle(p, x1, y1 + rowHead, tableWidth, z);
    tempTitle = "计算值和误差值(单位: ";
    switch (n)
    {
   
        case 1:
            if (GlobalData.meaUnit.Equals("公制"))
            {
   
                tempTitle = tempTitle + "mm)";
            }
            else
            {
   
                tempTitle = tempTitle + "inch)";
            }   
            break;
        case 2:
        case 3:
            tempTitle = tempTitle + "mrad)";
            break;
        default:
            break;
    }
    g.DrawString(tempTitle, font, brush, x2, y2 + rowHead);

    /* 第4行数据 */
    rowHead += z;
    g.DrawRectangle(p, x1, y1 + rowHead, columnHead, z);
    g.DrawString("目标点数i", font, brush, x2, y2 + rowHead);
    for (int i = 0; i < sum; i++)
    {
   
        g.DrawRectangle(p, x1 + 200 + i * zWidthTwice, y1 + 3 * z, zWidthTwice, z);
        g.DrawString((i + 1).ToString(), font, brush, x2 + columnHead+ i * zWidthTwice, y2 + 3 * z);
    }

    /* 第5行数据 */
    rowHead += z;
    g
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值