📄 formprint.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace PrintSystem
{
public partial class FormPrint : Form
{
public FormPrint()
{
InitializeComponent();
pDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(pDoc_PrintPage);
}
DataPrint m_Data;
InfoPrint info;
#region 打印的姓名
/// <summary>
/// 打印的姓名
/// </summary>
#endregion
string strName = string.Empty; //姓名
#region 打印的证号
/// <summary>
/// 打印的证号
/// </summary>
#endregion
string strCode = string.Empty; //证号
string strOrder = string.Empty;
float fMarginNameTop = 0;
float fMarginNameLeft = 0;
float fMarginCodeTop = 0;
float fMarginCodeLeft = 0;
Font fPrint;
private void FormPrint_Load(object sender, EventArgs e)
{
m_Data = new DataPrint(Config.connStr);
info = new InfoPrint();
LoadCofig();
DoLoad(string.Empty,false );
}
//加载系统配置
private void LoadCofig()
{
try
{
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(Config.strXmlPath);
System.Xml.XmlNode xNode = xmlDoc.DocumentElement.SelectSingleNode("Item[@Name='Name']");
if (xNode != null)
{
fMarginNameTop = Convert.ToSingle(xNode.Attributes["Top"].Value);
fMarginNameLeft = Convert.ToSingle(xNode.Attributes["Left"].Value);
}
xNode = xmlDoc.DocumentElement.SelectSingleNode("Item[@Name='Code']");
if (xNode != null)
{
fMarginCodeTop = Convert.ToSingle(xNode.Attributes["Top"].Value);
fMarginCodeLeft = Convert.ToSingle(xNode.Attributes["Left"].Value);
}
xNode = xmlDoc.DocumentElement.SelectSingleNode("Item[@Name='Font']");
if (xNode != null)
{
float fSize = Convert.ToSingle(xNode.Attributes["Size"].Value);
string strFontFimily = xNode.Attributes["Value"].Value;
fPrint = new Font(strFontFimily, fSize);
}
//纸张
xNode = xmlDoc.DocumentElement.SelectSingleNode("Item[@Name='Paper']");
if (xNode != null)
{
if (xNode.Attributes["Checked"].Value == "1")
{
int nHeight = Convert.ToInt32(xNode.Attributes["Height"].Value);
int nWith = Convert.ToInt32(xNode.Attributes["With"].Value);
System.Drawing.Printing.PageSettings pageSet = new System.Drawing.Printing.PageSettings();
pageSet.PaperSize = new System.Drawing.Printing.PaperSize("A4", nHeight,nWith );
// System.Drawing .Printing .Margins mm= new System.Drawing.Printing.Margins (
pDoc.DefaultPageSettings = pageSet;
}
}
xNode = xmlDoc.DocumentElement.SelectSingleNode("Item[@Name='Order']");
if (xNode != null)
{
string strO = xNode.Attributes["Checked"].Value;
switch (strO)
{
case "0":
strOrder = " Order By PrintTime ";
break;
case "1":
strOrder = " Order by PrintTime Desc ";
break;
case "2":
strOrder = " Order by Code ";
break;
case "3" :
strOrder = " Order by Code desc ";
break;
}
}
xmlDoc = null;
}
catch (Exception E)
{
MessageBox.Show(E.Message);
}
}
//加载数据
private void DoLoad(string strWhere,bool bChange)
{
string strSQL = "SELECT * FROM PrintSystem WHERE 66=66 " + strWhere + strOrder ;
DataTable dt = m_Data.ExecSQLTableDirect(strSQL);
Share.ListViewDataFill(dt, this.lvRecord);
this.tssRecordNum.Text = "当前记录共 :" + this.lvRecord.Items.Count.ToString() + " 条";
}
//打印预
private void btnPrintPre_Click(object sender, EventArgs e)
{
//设置打印值
this.strName = this.txbName.Text.Trim();
this.strCode = this.txbCode .Text.Trim();
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = pDoc;
ppd.ShowDialog();
}
//打印内容
private void pDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawString(strName, fPrint , Brushes.Black, fMarginNameLeft, fMarginNameTop);
e.Graphics.DrawString(strCode, fPrint, Brushes.Black, fMarginCodeLeft, fMarginCodeTop);
}
//开始打印
private void btnPrint_Click(object sender, EventArgs e)
{
btnAdd_Click(sender,e);
//设置打印值
this.strName = this.txbName.Text.Trim ();
this.strCode = this.txbCode .Text.Trim();
//开始打印
pDoc.Print();
this.txbName.Text = "";
this.txbCode.Text = "";
}
private void Modify(string strID)
{
try
{
FormInfoModify frm = new FormInfoModify();
frm.Tag = strID;
frm.ShowDialog();
if (frm.DialogResult == DialogResult.OK)
{
m_Data.UpdatePrint(frm.PrintInfo);
DoLoad(string.Empty, false);
}
}
catch (Exception E)
{
MessageBox.Show(E.Message);
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
//
if (this.txbName.Text.Trim() == string.Empty)
throw new Exception("姓名不能为空!");
if (this.txbCode.Text.Trim() == string.Empty)
throw new Exception("证号不能为空!");
//
info.strName = this.txbName.Text.Trim();
info.strCode = this.txbCode.Text.Trim();
m_Data.InsertPrint(info);
DoLoad(string.Empty, false);
this.txbName.Text = "";
this.txbCode.Text = "";
}
catch (Exception E)
{
MessageBox.Show(E.Message);
}
finally
{
this.txbName.Text = "";
this.txbCode.Text = "";
}
}
private void btnAlter_Click(object sender, EventArgs e)
{
try
{
if (this.lvRecord.Items.Count == 0 || this.lvRecord.SelectedItems.Count == 0) return;
Modify(this.lvRecord .SelectedItems [0].Tag .ToString ());
}
catch (Exception E)
{
MessageBox.Show(E.Message);
}
}
private void btnDel_Click(object sender, EventArgs e)
{
try
{
if (Share.QuestionYesNo("删除后将不能恢复,确定要删除吗?") == DialogResult.No) return;
foreach (ListViewItem lvi in this.lvRecord.Items)
{
if (lvi.Selected)
{
m_Data.DeletePrint(lvi.Tag.ToString());
this.lvRecord.Items.Remove(lvi);
}
}
}
catch (Exception E)
{
MessageBox.Show(E.Message);
}
}
private void btnSel_Click(object sender, EventArgs e)
{
try
{
string strSQL = string.Empty;
//姓名查询
if (this.txbSelName.Text.Trim() != string.Empty)
strSQL += "AND Name like '%" + this.txbSelName.Text.Trim() + "%' ";
//证号
if (this.txbSelCode.Text.Trim() != string.Empty)
strSQL += "AND Code like '%" + this.txbSelCode.Text.Trim() + "%' ";
//时间
if (this.checkBox1.Checked)
{
if (this.dtSelStart.Value >= this.dtSelEnd.Value)
throw new Exception("请保证开始时间小于结束时间!");
strSQL += " AND PrintTime > #"
+ string.Format("{0}-{1}-{2} 00:00", this.dtSelStart.Value.Year, this.dtSelStart.Value.Month, this.dtSelStart.Value.Day) + "# "
+" AND PrintTime < #"
+ string.Format("{0}-{1}-{2} 23:59", this.dtSelEnd.Value.Year, this.dtSelEnd.Value.Month, this.dtSelEnd.Value.Day) + "# ";
}
DoLoad(strSQL, true);
}
catch (Exception E)
{
MessageBox.Show(E.Message);
}
}
private void btnConfig_Click(object sender, EventArgs e)
{
FormConfig frm = new FormConfig();
frm.ShowDialog();
if (frm.DialogResult == DialogResult.OK)
LoadCofig();
}
//打印选择项
private void btnPrintSelected_Click(object sender, EventArgs e)
{
try
{
if (this.lvRecord.Items.Count == 0 || this.lvRecord.SelectedItems.Count == 0) return;
//设置打印值
this.strName = this.lvRecord.FocusedItem.SubItems[1].Text;
this.strCode = this.lvRecord.FocusedItem.SubItems[2].Text;
//开始打印
pDoc.Print();
}
catch (Exception E)
{
MessageBox.Show(E.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("软件开发人员联系电话:柳先生:13944902227 何先生:13634838877");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -