/******************************************************************************* * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential * 类的信息: * 1.程序名称:F_MST_0205.cs * 2.功能描述:打印工号条码 * 编辑履历: * 作者 日期 版本 修改内容 * 陈冰 2014/10/27 1.00 新建 *******************************************************************************/ using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Printing; using System.Windows.Forms; using Dongke.IBOSS.PRD.Basics.BaseControls; using Dongke.IBOSS.PRD.Basics.BaseResources; using Dongke.IBOSS.PRD.Basics.Library; using Dongke.IBOSS.PRD.Client.CommonModule; namespace Dongke.IBOSS.PRD.Client.SystemModule { public partial class F_MST_0205 : FormBase { #region 成员变量 // 文本张数 private string _txtTotal = "共{0}张"; // 图片集 List _imgList = new List(); #endregion #region 属性 /// /// 条码集 /// public List BarcodeList { get; set; } #endregion #region 构造 public F_MST_0205() { InitializeComponent(); this.Text = FormTitles.F_MST_0205; this.btnPreviousPage.Text = ButtonText.BTN_PREVIOUSPAGE; this.btnAfterPage.Text = ButtonText.BTN_AFTERPAGE; this.btnResetPage.Text = ButtonText.BTN_RESETPAGE; this.btnPrint.Text = ButtonText.BTN_PRINT; this.btnSaveImage.Text = ButtonText.BTN_SAVEIMAGE; this.btnCancel.Text = ButtonText.BTN_CANCEL; } #endregion #region 事件 /// /// 页面加载 /// /// /// private void F_MST_0208_Load(object sender, System.EventArgs e) { try { this.DrawImg(); } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 前页按钮事件 /// /// /// private void btnPreviousPage_Click(object sender, EventArgs e) { try { int currentPaper = int.Parse(this.lblCurrentPaper.Text); this.picboxBarcode.Image = _imgList[currentPaper - 2]; this.lblCurrentPaper.Text = (currentPaper - 1).ToString(); if (currentPaper - 1 == Constant.INT_IS_ONE) { this.btnPreviousPage.Enabled = false; } this.btnAfterPage.Enabled = true; } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 后页按钮事件 /// /// /// private void btnAfterPage_Click(object sender, EventArgs e) { try { int currentPaper = int.Parse(this.lblCurrentPaper.Text); this.picboxBarcode.Image = _imgList[currentPaper]; this.lblCurrentPaper.Text = (currentPaper + 1).ToString(); if (currentPaper + 1 == _imgList.Count) { this.btnAfterPage.Enabled = false; } this.btnPreviousPage.Enabled = true; } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 重新绘制图片 /// /// /// private void btnResetPage_Click(object sender, EventArgs e) { try { this.DrawImg(); } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 保存图片 /// /// /// private void btnSaveImage_Click(object sender, EventArgs e) { try { this.saveFileDialog.Filter = "所有文件(*.*)|*.*"; this.saveFileDialog.RestoreDirectory = true; this.saveFileDialog.FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff"); DialogResult dialogResult = this.saveFileDialog.ShowDialog(); if (dialogResult == DialogResult.OK) { string localFilePath = this.saveFileDialog.FileName.ToString(); //获得文件路径 //获取文件路径,不带文件名 string path = localFilePath.Substring(0, localFilePath.LastIndexOf("\\")); foreach (Image img in _imgList) { img.Save(path + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg", ImageFormat.Jpeg); } MessageBox.Show(string.Format(Messages.MSG_CMN_I001, "图片", "保存"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 打印图片 /// /// /// private void btnPrint_Click(object sender, EventArgs e) { try { // 实现一次打印多页详见 http://blog.csdn.net/tsapi/article/details/6237695 foreach (Image img in _imgList) { //打印事件设置 this.printDocument.PrintPage += new PrintPageEventHandler((obj, printPageEventArgs) => { printPageEventArgs.Graphics.DrawImage(_imgList[0], 0, 0); }); this.printDocument.Print(); } } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 关闭 /// /// /// private void btnClose_Click(object sender, EventArgs e) { this.Close(); } #endregion #region 私有方法 /// /// 绘制图片 /// private void DrawImg() { this._imgList = BarcodePrintUtility.BarcodeDrawPaper(BarcodeList, int.Parse(this.txtWidth.Text), int.Parse(this.txtHeight.Text), BarcodePrintUtility.PaperType.A4); this.picboxBarcode.Image = _imgList[0]; this.picboxBarcode.SizeMode = PictureBoxSizeMode.CenterImage; this.lblTotal.Text = string.Format(_txtTotal, _imgList.Count); this.lblCurrentPaper.Text = "1"; this.btnPreviousPage.Enabled = false; if (_imgList.Count == Constant.INT_IS_ONE) { this.btnAfterPage.Enabled = false; } } #endregion } }