using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 面试成绩汇总表打印
{
    public partial class Form1 : Form
    {
        VBprinter.VB2008Print myp = new VBprinter.VB2008Print();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PrintSub(false);
        }
        private void PrintSub(bool isimmediateprint)
        {
            myp.PaperKind = System.Drawing.Printing.PaperKind.A4;
            myp.PaperLandscape = true;
            myp.IsImmediatePrintNotPreview = isimmediateprint;
            myp.NewDoc();
            myp.NewPage();

            float space1;
            space1 = 20;
            float space2;
            space2 = 15;
            float space3;
            space3=20;
            float dgv1height = 0;
            float dgv2height = 0;
            //计算表格1需要打印的高度（按DGV1表格自身的行高计算）
            dgv1height=myp.ConvFromDisplay (DGV1 .ColumnHeadersHeight,false);
            for (int i=0;i<=DGV1.RowCount -1;i++)
            {
                dgv1height =dgv1height +myp.ConvFromDisplay (DGV1.Rows[i].Height ,false);
            }
            //计算表格2需要打印的高度
             dgv2height=0;
            for (int i=0;i<=DGV2.RowCount -1;i++)
            {
                dgv2height =dgv2height +myp.ConvFromDisplay (DGV2.Rows[i].Height ,false);
            }
            //两个表格加间距的总高度
            float dgvheight;
            dgvheight =space1+dgv1height +space2+dgv2height +space3 ;
            string ss;
            ss = "面试成绩汇总表";
            Font titlefont = new Font("黑体", 18, FontStyle.Bold);
            Font txtfont = new Font("宋体", 12);
            Font font2 = new Font("华文行楷", 12, FontStyle.Bold);
            myp.DrawTitle(ss, titlefont, Color.Black, 0);
            myp.NewRow(myp.LastDrawTextHeight/2);
            float rowheight =35;
            //下面简单计算每列宽度，如果需要，可以分每列定义宽度
            float w1 = myp.PaperPrintWidth /12;
            float w2 = myp.PaperPrintWidth / 12*2;
                       
            ss = "岗位代码：";
            myp.DrawCell(ss,w1,rowheight,txtfont,Color.Black ,"1111",Color.Black,StringAlignment .Center ,StringAlignment .Center ,false,false,false,false,0,0);
            ss = "高级程序员";
            myp.DrawCell(ss, w2, rowheight,font2,Color.Black, "1111", Color.Black, StringAlignment.Center, StringAlignment.Center, false, false, false, false, 0, 0);

            ss = "准考证号：";
            myp.DrawCell(ss, w1, rowheight, txtfont, Color.Black, "1111", Color.Black, StringAlignment.Center, StringAlignment.Center, false, false, false, false, 0, 0);
            ss = "ad32323553555";
            myp.DrawCell(ss, w2, rowheight, font2, Color.Black, "1111", Color.Black, StringAlignment.Center, StringAlignment.Center, false, false, false, false, 0, 0);

            ss = "考生姓名：";
            myp.DrawCell(ss, w1, rowheight, txtfont, Color.Black, "1111", Color.Black, StringAlignment.Center, StringAlignment.Center, false, false, false, false, 0, 0);
            ss = "张三";
            myp.DrawCell(ss, w2, rowheight, font2, Color.Black, "1111", Color.Black, StringAlignment.Center, StringAlignment.Center, false, false, false, false, 0, 0);

            ss = "总分";
            myp.DrawCell(ss, w1, rowheight, txtfont, Color.Black, "1111", Color.Black, StringAlignment.Center, StringAlignment.Center, false, false, false, false, 0, 0);
            ss = "99";
            myp.DrawCell(ss, w2, rowheight, font2, Color.Black, "1111", Color.Black, StringAlignment.Center, StringAlignment.Center, false, false, false, false, 0, 0);

            myp.NewRow(rowheight);
            float tempy = myp.Currenty;
            myp.DrawCell("", myp.PaperPrintWidth, dgvheight, "1111", Color.Black, StringAlignment.Center, StringAlignment.Center, false, false, false, false, 0, 0);


            myp.NewRow(dgvheight);
            rowheight = 70;
            ss = "考生" + "\n" + "姓名";
            myp.DrawCell(ss, w1, rowheight, txtfont, Color.Black, "1111", Color.Black, StringAlignment.Center, StringAlignment.Center, true, false, false, false, 0, 0);
            ss = "张三";
            myp.DrawCell(ss, w2, rowheight, font2, Color.Black, "1111", Color.Black, StringAlignment.Center, StringAlignment.Center, false, false, false, false, 0, 0);
           ss = "考官评语：" + "\n" + "\n" + "                                                         主考官签字:";
            myp.DrawCell(ss, myp.PaperPrintWidth -w1-w2, rowheight, font2, Color.Black, "1111", Color.Black, StringAlignment.Near , StringAlignment.Center,true, false, false, false, 0, 0);

            myp.NewRow(rowheight);
            myp.NewRow(10);
            ss = "监督（签字）：                         工作人员（签字）：                                    年  月  日";
            myp.DrawText(ss, txtfont, 0);
            //再打印DGV1与DGV2
            myp.Currentx=0;
            myp.Currenty = tempy;
            myp.Currenty = myp.Currenty + space1;
            myp.PrintDGV(DGV1, "", titlefont , StringAlignment.Center, null);
            myp.Currentx = 0;
            myp.Currenty = myp.Currenty + space2;
            myp.PrintDGV(DGV2, "", titlefont, StringAlignment.Center, null);

            if (isimmediateprint == true)
            {
                myp.EndDoc();
            }
            else
            {
                myp.EndDoc("打印预览结果");
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            DGV1.AllowUserToAddRows = false;
            DGV1.RowCount = 8;
            DGV2.AllowUserToAddRows = false;
            DGV2.RowCount = 4;
             DGV1.Rows[0].Cells[1].Value ="权重";
            for (int i = 1; i <= DGV1.RowCount - 1; i++)
            {
                DGV1.Rows[i].Cells[1].Value = "张" + i.ToString();
            }
            Random rn = new Random();
            for (int j = 2; j <= DGV1.ColumnCount - 1; j++)
            {
                for (int i = 0; i <= DGV1.RowCount - 1; i++)
                {
                    DGV1.Rows[i].Cells[j].Value = rn.Next(100);
                }
            }
            DGV2.Rows[0].Cells[1].Value = "扣最高分";
            for (int j = 2; j <= DGV2.ColumnCount - 1; j++)
            {
                for (int i = 0; i <= DGV2.RowCount - 1; i++)
                {
                    DGV2.Rows[i].Cells[j].Value = rn.Next(100);
                }
            }
            DGV1.AutoResizeColumns ();
            for (int i=0; i <= DGV1.ColumnCount - 1; i++)
            {
                DGV2.Columns[i].Width = DGV1.Columns[i].Width;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            PrintSub(true);
        }
    }
}
