📄 defprintform.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Drawing.Printing;
using System.Text;
namespace PM
{
/// <summary>
/// DefPrintForm 的摘要说明。
/// </summary>
public class DefPrintForm : System.Windows.Forms.Form
{
private System.Windows.Forms.CheckedListBox checkedListBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Drawing.Printing.PrintDocument printDocument1;
private System.Windows.Forms.PageSetupDialog pageSetupDialog1;
private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;
private System.Windows.Forms.PrintDialog printDialog1;
private System.Windows.Forms.TextBox Title_txt;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public DefPrintForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(DefPrintForm));
this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.printDocument1 = new System.Drawing.Printing.PrintDocument();
this.pageSetupDialog1 = new System.Windows.Forms.PageSetupDialog();
this.printPreviewDialog1 = new System.Windows.Forms.PrintPreviewDialog();
this.printDialog1 = new System.Windows.Forms.PrintDialog();
this.Title_txt = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// checkedListBox1
//
this.checkedListBox1.Location = new System.Drawing.Point(8, 8);
this.checkedListBox1.Name = "checkedListBox1";
this.checkedListBox1.Size = new System.Drawing.Size(120, 356);
this.checkedListBox1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(288, 280);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "页面设置";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(288, 312);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "打印预览 ";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(288, 344);
this.button3.Name = "button3";
this.button3.TabIndex = 3;
this.button3.Text = "打印";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(160, 344);
this.button4.Name = "button4";
this.button4.TabIndex = 4;
this.button4.Text = "取消";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(160, 312);
this.button5.Name = "button5";
this.button5.TabIndex = 5;
this.button5.Text = "全选";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button6
//
this.button6.Location = new System.Drawing.Point(160, 280);
this.button6.Name = "button6";
this.button6.TabIndex = 6;
this.button6.Text = "取消选定";
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// printDocument1
//
this.printDocument1.OriginAtMargins = true;
this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
//
// pageSetupDialog1
//
this.pageSetupDialog1.Document = this.printDocument1;
this.pageSetupDialog1.ShowHelp = true;
//
// printPreviewDialog1
//
this.printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.ClientSize = new System.Drawing.Size(400, 300);
this.printPreviewDialog1.Document = this.printDocument1;
this.printPreviewDialog1.Enabled = true;
this.printPreviewDialog1.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));
this.printPreviewDialog1.Location = new System.Drawing.Point(546, 8);
this.printPreviewDialog1.MinimumSize = new System.Drawing.Size(375, 250);
this.printPreviewDialog1.Name = "printPreviewDialog1";
this.printPreviewDialog1.TransparencyKey = System.Drawing.Color.Empty;
this.printPreviewDialog1.UseAntiAlias = true;
this.printPreviewDialog1.Visible = false;
//
// printDialog1
//
this.printDialog1.AllowSelection = true;
this.printDialog1.AllowSomePages = true;
this.printDialog1.Document = this.printDocument1;
this.printDialog1.ShowHelp = true;
//
// Title_txt
//
this.Title_txt.Location = new System.Drawing.Point(184, 96);
this.Title_txt.Name = "Title_txt";
this.Title_txt.Size = new System.Drawing.Size(168, 21);
this.Title_txt.TabIndex = 7;
this.Title_txt.Text = "员工基本情况表";
//
// label1
//
this.label1.Location = new System.Drawing.Point(136, 104);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(32, 16);
this.label1.TabIndex = 8;
this.label1.Text = "标题";
//
// label2
//
this.label2.ForeColor = System.Drawing.Color.Red;
this.label2.Location = new System.Drawing.Point(176, 24);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(176, 40);
this.label2.TabIndex = 9;
//
// label3
//
this.label3.Location = new System.Drawing.Point(152, 160);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(56, 16);
this.label3.TabIndex = 10;
this.label3.Text = "字段间距";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(224, 152);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 11;
this.textBox1.Text = "80";
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// DefPrintForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(400, 381);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.Title_txt);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.checkedListBox1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "DefPrintForm";
this.Text = "自定义打印";
this.Load += new System.EventHandler(this.DefPrintForm_Load);
this.ResumeLayout(false);
}
#endregion
private void DefPrintForm_Load(object sender, System.EventArgs e)
{
this.label2.Text = "请勾选需要打印的字段";
for(int i = 0 ; i < empInfoForm.eif.table.Columns.Count ; i ++)
{
this.checkedListBox1.Items.Add(empInfoForm.eif.table.Columns[i].ColumnName);
}
}
private void button4_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, System.EventArgs e)
{
this.pageSetupDialog1.ShowDialog();
}
private void button2_Click(object sender, System.EventArgs e)
{
this.printPreviewDialog1.ShowDialog();
}
private void button3_Click(object sender, System.EventArgs e)
{
if(this.printDialog1.ShowDialog() == DialogResult.OK)
{
try
{
this.printDocument1.Print();
}
catch(Exception ec)
{
MessageBox.Show(ec.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
printDocument1.PrintController.OnEndPrint(printDocument1,new PrintEventArgs());
}
}
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
StringBuilder sb = new StringBuilder();
for(int i = 0 ; i < this.checkedListBox1.CheckedItems.Count ; i ++)
{
sb.Append(this.checkedListBox1.CheckedItems[i].ToString() + "#");
}
string [] HeadText = sb.ToString().Split('#');
Graphics g = e.Graphics;
float leftMargion = e.MarginBounds.Left;
float left = e.MarginBounds.Left;
float top = e.MarginBounds.Top;
Font TitleFont = new Font("楷体_GB2312",12);//大标题字体
Font SubTileFont = new Font("楷体_GB2312",8);//小标题字体
Font font = new Font("宋体",9);
SolidBrush brush = new SolidBrush(Color.Black);//画刷
g.DrawString(this.Title_txt.Text,TitleFont,brush,e.MarginBounds.Width / 2 ,e.MarginBounds.Top);//打印主标题
g.DrawString("打印日期: " + DateTime.Now.ToShortDateString(),SubTileFont,brush,e.MarginBounds.Width / 4 * 3,e.MarginBounds.Top + font.GetHeight(g)*2);//打印字标题
for(int j = 0 ; j < HeadText.Length ; j ++)//画列名
{
g.DrawString(HeadText[j],font,brush,left,e.MarginBounds.Top + font.GetHeight(g)*4);
left += float.Parse(this.textBox1.Text.Trim());
}
float Top = e.MarginBounds.Top + font.GetHeight(g)*5;
for(int n = 0 ; n < empInfoForm.eif.table.Rows.Count ; n ++)
{
leftMargion = e.MarginBounds.Left;
for(int m = 0 ; m < HeadText.Length - 1 ; m ++)
{
g.DrawString(empInfoForm.eif.table.Rows[n][HeadText[m]].ToString(),font,brush,leftMargion,Top);
leftMargion += float.Parse(this.textBox1.Text.Trim());
}
Top += font.GetHeight(g)*2;
}
if(Top > e.MarginBounds.Bottom)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
}
//全选
private void button5_Click(object sender, System.EventArgs e)
{
for(int i = 0 ; i < this.checkedListBox1.Items.Count ; i ++)
{
this.checkedListBox1.SetItemCheckState(i,CheckState.Checked);
}
}
//取消选定
private void button6_Click(object sender, System.EventArgs e)
{
for(int i = 0 ; i < this.checkedListBox1.Items.Count ; i ++)
{
this.checkedListBox1.SetItemCheckState(i,CheckState.Unchecked);
}
}
//限制只能输入正整数
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
if(this.textBox1.Text != "")
{
if(!Regexlib.IsValidNumber(this.textBox1.Text.Trim()))
{
MessageBox.Show("字段间距格式错误,请输入正整数!");
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -