C#打印绘图的实现方法

所属分类: 软件编程 / C#教程 阅读数: 46
收藏 0 赞 0 分享

本文实例讲述了C#打印绘图的实现方法。分享给大家供大家参考。具体实现方法如下:

复制代码 代码如下:

String drawString = "";
Font drawFont = null;
SolidBrush drawBrush = null;
float x = 0F;
float y = 0F;
StringFormat drawFormat = new StringFormat();

string test = "";

public string Test
{
    get { return test; }
    set { test = value; }
}
public Form1()
{
    InitializeComponent();
    //设置纸张大小
    PaperSize paperSize = new PaperSize("DataOrder", 1023, 614);
    printDocument1.DefaultPageSettings.PaperSize = paperSize;
}

//打印
private void button1_Click(object sender, EventArgs e)
{
    printDocument1.PrintPage += new PrintPageEventHandler(MyPrintDoc_PrintPage);
    try
    {
 printPreviewDialog1.Document = printDocument1;
 printPreviewDialog1.FormBorderStyle = FormBorderStyle.Fixed3D;
 printPreviewDialog1.ShowDialog();
 printDocument1.Print();
    }
    catch
    {

 MessageBox.Show("请安装打印机", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

protected void MyPrintDoc_PrintPage(object sender, PrintPageEventArgs e)
{
    //字体 颜色 格式 坐标
    drawFont = new Font("Arial", 8);
    drawBrush = new SolidBrush(Color.Black);
    x = 0F;
    y = 0F;
    drawFormat.FormatFlags = StringFormatFlags.NoWrap;

    //块数
    int num = 6;
    float weightAll = 12.600F;
    //标题
    //标题第一行
    string title = "钢板入库计量单";
    string BillCode = "吊号:" + "20080505170";
    string ClassType = "班别:" + "乙";
    string type1 = "类型:" + "定轧";
    string OrderNum = "记录单号:" + "QW-Y14-02-06";
    //标题第二行
    string ArriveStation = "到站:";
    string SpLine = "专用线:";
    string SaleNo = "销售订单号:";
    //标题第三行
    string AcceptName = "收货单位:" + "XXXX/12*2438*9144收二支";
    string time = DateTime.Now.Year.ToString() + "年" + DateTime.Now.Month.ToString().PadLeft(2,'0')+"月"+DateTime.Now.Day.ToString()+"日";
    //表格下数据
    string BoardNum = "合计:" + num.ToString() + "    块      " + weightAll.ToString()+"   吨   ";
    string CheckMan1 = "检查员:" + "";
    string MeName = "计量员:"+"XX";
    string CheckMan2 = "核对员:" + "";
    //线条长度
    Pen line = new Pen(drawBrush, 1);
    //绘图--字的位置
    //绘图--总标题
    e.Graphics.DrawString(title, drawFont, drawBrush, 400, 45, drawFormat);
    //绘图--标题--第一行
    //吊号
    e.Graphics.DrawString(BillCode, drawFont, drawBrush, 125,83, drawFormat);
    //班别
    e.Graphics.DrawString(ClassType, drawFont, drawBrush, 354, 83, drawFormat);
    //类型
    e.Graphics.DrawString(type1, drawFont, drawBrush, 500, 83, drawFormat);
    //记录单号
    e.Graphics.DrawString(OrderNum, drawFont, drawBrush, 685, 83, drawFormat);

    //绘图--标题--第二行
    //到站
    e.Graphics.DrawString(ArriveStation, drawFont, drawBrush, 125, 106, drawFormat);
    //专用线
    e.Graphics.DrawString(SpLine, drawFont, drawBrush, 354, 106, drawFormat);
    //销售订单号
    e.Graphics.DrawString(SaleNo, drawFont, drawBrush, 500, 106, drawFormat);

    //绘图--标题--第三行
    //收货单位
    e.Graphics.DrawString(AcceptName, drawFont, drawBrush, 125, 129, drawFormat);
    //时间
    e.Graphics.DrawString(time, drawFont, drawBrush, 685, 129, drawFormat);


    //绘图--表格下数据
    //合计
    e.Graphics.DrawString(BoardNum, drawFont, drawBrush, 125, 568, drawFormat);
    //检查员
    e.Graphics.DrawString(CheckMan1, drawFont, drawBrush, 400, 568, drawFormat);
    //计量员
    e.Graphics.DrawString(MeName, drawFont, drawBrush, 550, 568, drawFormat);
    //核对员
    e.Graphics.DrawString(CheckMan2, drawFont, drawBrush, 700, 568, drawFormat);

    //绘图 表格
    float leftbianJu = 120;
    float topbianJu = 152;
    float tableWidth = 770;
    float tableHeight = 393;

    float cellwidth = 55;
    float cellwidth1 = 110;
    float cellheigh = 0F;

    //绘图--线的位置 外边矩形
    //横
    e.Graphics.DrawLine(line, leftbianJu, topbianJu,leftbianJu+tableWidth,topbianJu);
    e.Graphics.DrawLine(line, leftbianJu, topbianJu + tableHeight, leftbianJu + tableWidth, topbianJu + tableHeight);
    //竖
    e.Graphics.DrawLine(line, leftbianJu, topbianJu, leftbianJu, topbianJu+tableHeight);
    e.Graphics.DrawLine(line, leftbianJu + tableWidth, topbianJu, leftbianJu + tableWidth, topbianJu + tableHeight);
    //e.Graphics.DrawLine(line, 15, 0, 15, 614);

    //绘图--内部竖线的位置
    y = topbianJu+tableHeight;
    x = leftbianJu + cellwidth;
    e.Graphics.DrawLine(line, x, topbianJu, x,y );
    x= leftbianJu + cellwidth + cellwidth1;
    e.Graphics.DrawLine(line,x , topbianJu, x, y);
    x = leftbianJu + cellwidth + cellwidth1 * 2;
    e.Graphics.DrawLine(line, x, topbianJu, x, y);
    x = leftbianJu + cellwidth + cellwidth1 * 3;
    e.Graphics.DrawLine(line, x, topbianJu, x, y);
    x = leftbianJu + cellwidth * 2 + cellwidth1 * 3;
    e.Graphics.DrawLine(line, x, topbianJu, x, y);
    x = leftbianJu + cellwidth * 3 + cellwidth1 * 3;
    e.Graphics.DrawLine(line, x, topbianJu, x, y);
    x = leftbianJu + cellwidth * 4 + cellwidth1 * 3;
    e.Graphics.DrawLine(line, x, topbianJu, x, y);
    x = leftbianJu + cellwidth * 5 + cellwidth1 * 3;
    e.Graphics.DrawLine(line, x, topbianJu, x, y);
    x = leftbianJu + cellwidth * 6 + cellwidth1 * 3;
    e.Graphics.DrawLine(line, x, topbianJu, x, y);
    x = leftbianJu + cellwidth * 7 + cellwidth1 * 3;
    e.Graphics.DrawLine(line, x, topbianJu, x, y);

    //绘图--内部横线的位置
    //根据块数计算表格高度
    if (num != 0)
 cellheigh = tableHeight / (float)num;
    else
 cellheigh = 0;
    //绘图--内部横线的位置
    for (int i = 1; i < num; i++)
    {
 if (i == 1)
 {
     //数据
     string str = "";
     str = "序号";
     x = leftbianJu + cellwidth / 5;
     y = topbianJu + cellheigh / 3;
     e.Graphics.DrawString(str, drawFont, drawBrush, x, y, drawFormat);
     str = "卡片编号";
     x = leftbianJu + cellwidth + cellwidth1 / 5;
     e.Graphics.DrawString(str, drawFont, drawBrush, x, y, drawFormat);

     str = "炉号";
     x = leftbianJu + cellwidth + cellwidth1 + cellwidth1 / 5;
     e.Graphics.DrawString(str, drawFont, drawBrush, x, y, drawFormat);

     str = "物料编码";
     x = leftbianJu + cellwidth + cellwidth1*2 + cellwidth1 / 5;
     e.Graphics.DrawString(str, drawFont, drawBrush, x, y, drawFormat);

     str = "牌号";
     x = leftbianJu + cellwidth + cellwidth1*3 + cellwidth / 5;
     e.Graphics.DrawString(str, drawFont, drawBrush, x, y, drawFormat);

     str = "厚度";
     x = leftbianJu + cellwidth * 2 + cellwidth1 * 3 + cellwidth / 5;
     e.Graphics.DrawString(str, drawFont, drawBrush, x, y, drawFormat);

     str = "宽度";
     x = leftbianJu + cellwidth * 3 + cellwidth1 * 3 + cellwidth / 5;
     e.Graphics.DrawString(str, drawFont, drawBrush, x, y, drawFormat);

     str = "长度";
     x = leftbianJu + cellwidth * 4 + cellwidth1 * 3 + cellwidth / 5;
     e.Graphics.DrawString(str, drawFont, drawBrush, x, y, drawFormat);

     str = "重量";
     x = leftbianJu + cellwidth * 5 + cellwidth1 * 3 + cellwidth / 5;
     e.Graphics.DrawString(str, drawFont, drawBrush, x, y, drawFormat);

     str = "级别";
     x = leftbianJu + cellwidth * 6 + cellwidth1 * 3 + cellwidth / 5;
     e.Graphics.DrawString(str, drawFont, drawBrush, x, y, drawFormat);

     str = "偏差";
     x = leftbianJu + cellwidth * 7 + cellwidth1 * 3 + cellwidth / 5;
     e.Graphics.DrawString(str, drawFont, drawBrush, x, y, drawFormat);
 }
 //横线
 y = topbianJu + cellheigh * i;
 e.Graphics.DrawLine(line, leftbianJu, y, leftbianJu + tableWidth, y);
    }
    e.HasMorePages = false;
}

运行效果如下图所示:

希望本文所述对大家的C#程序设计有所帮助。

更多精彩内容其他人还在看

winform用datagridview制作课程表实例

这篇文章主要介绍了winform用datagridview制作课程表的方法,实例分析了WinForm实现课程表的结构、数据库及调用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#中winform控制textbox输入只能为数字的方法

这篇文章主要介绍了C#中winform控制textbox输入只能为数字的方法,包括使用keyPress事件限制键盘输入以及TextChanged事件限制粘贴等情况,来实现控制输入为数字的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

C#省份城市下拉框联动简单实现方法

这篇文章主要介绍了C#省份城市下拉框联动简单实现方法,涉及字典的定义与索引的用法,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#处理MySql多个返回集的方法

这篇文章主要介绍了C#处理MySql多个返回集的方法,实现了对处理MySql多个返回集进行封装,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#无限参数的写法

这篇文章主要介绍了C#无限参数的写法,通过循环遍历再结合paras.Add方法实现无限参数的功能,是比较实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#反射应用实例

这篇文章主要介绍了C#反射应用,实例分析了通过反射实现多系统数据库的配置方法,是比较实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#窗体传值实例汇总

这篇文章主要介绍了C#窗体传值,实例形式汇总了静态变量传值、委托传值、对话框之间的传值等常见应用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#把数组中的某个元素取出来放到第一个位置的实现方法

这篇文章主要介绍了C#把数组中的某个元素取出来放到第一个位置的实现方法,涉及C#针对数组的常见操作技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#中Equality和Identity浅析

这篇文章主要介绍了C#中Equality和Identity浅析,本文先是讲解了Equality和Identity的定义,同时讲解了判断两个对象等价性的4种方法,需要的朋友可以参考下
收藏 0 赞 0 分享

在Linux上运行C#的方法

这篇文章主要介绍了在Linux上运行C#的方法,实例分析了Linux平台下Mono软件包的应用技巧,以及在此基础之上的C#运行方法,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多