using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using DevExpress.XtraEditors; using ICSSoft.Base.Config.AppConfig; using ICSSoft.Base.Config.DBHelper; using System.IO; namespace ICSSoft.Base.Lable.PrintTool { public partial class FormPrintDialog : DevExpress.XtraEditors.XtraForm { string _LableType = ""; List _PrintParas = new List(); bool _RptFlag = false; DataSet _PrintDataSet = null; string _PrintTableName = ""; public FormPrintDialog(string lableType, string printTableName, List printParas, bool rptFlag, DataSet printDataSet) { InitializeComponent(); if (rptFlag) { chkFPrint.Enabled = false; txtPointName.Enabled = false; } if (chkShowDialogFlag.Checked) { txtPrintNum.Enabled = false; txtPrintNum.Text = "1"; } _LableType = lableType; _PrintParas = printParas; _PrintTableName = printTableName; _RptFlag = rptFlag; _PrintDataSet = printDataSet; BindPrinters(); BindLables(); BindPrintPoint(); GetDefaultLastPrinter(); } private void BindPrinters() { try { List list = GetPrinterInfo.GetPrinters(); foreach (string s in list) { txtPrint.Properties.Items.Add(s); } } catch (Exception ex) { } } private void BindLables() { string sql = "SELECT ID as [编号],LableName as [名称] FROM dbo.Sys_Lables a WHERE (LableType='{0}' AND WorkPointCode='{1}') "; sql = string.Format(sql, new object[] { _LableType, AppConfig.WorkPointCode }); DataTable data = DBHelper.ExecuteDataset(AppConfig.FrameConnectString, CommandType.Text, sql).Tables[0]; txtTempName.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("编号", "编号")); txtTempName.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("名称", "名称")); txtTempName.Properties.ValueMember = "编号"; txtTempName.Properties.DisplayMember = "名称"; txtTempName.Properties.Columns[0].Visible = false; txtTempName.Properties.DataSource = data; if (data.Rows.Count > 0) { txtTempName.EditValue = data.Rows[0]["编号"]; } } private void BindPrintPoint() { string sql = "SELECT ID AS [编号],PointName AS [名称] FROM dbo.Sys_PrintPoint"; DataTable data = DBHelper.ExecuteDataset(AppConfig.FrameConnectString, CommandType.Text, sql).Tables[0]; txtPointName.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("编号", "编号")); txtPointName.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("名称", "名称")); txtPointName.Properties.ValueMember = "编号"; txtPointName.Properties.DisplayMember = "名称"; txtPointName.Properties.Columns[0].Visible = false; txtPointName.Properties.DataSource = data; if (data.Rows.Count > 0) { txtPointName.ItemIndex = 0; } } private void btnOk_Click(object sender, EventArgs e) { if (txtTempName.EditValue == null) { MessageBox.Show("请选择打印模板!"); return; } if (chkLPrint.Checked == false && chkFPrint.Checked == false) { MessageBox.Show("请选择本地打印或者远程打印当中的一种打印方式!"); return; } if (chkLPrint.Checked == true && chkFPrint.Checked == true) { MessageBox.Show("请选择本地打印或者远程打印当中的一种打印方式!"); return; } if (chkLPrint.Checked == true) { if (txtPrint.Text.Trim() == "") { MessageBox.Show("请选择本地打印机!"); return; } } if (chkFPrint.Checked == true) { if (txtPointName.EditValue == null) { MessageBox.Show("请选择打印地点"); return; } } if (txtPrintNum.Text.Trim() == "") { MessageBox.Show("请输入打印张数!"); return; } if (_RptFlag == true) { if (chkShowDialogFlag.Checked) { if (chkLPrint.Checked) { FormPrint pr = new FormPrint(txtTempName.EditValue.ToString(), txtPrint.Text, true, _PrintTableName, Convert.ToInt32(txtPrintNum.Text), "", "本地打印", _PrintDataSet); pr.ShowDialog(); } else { FormPrint pr = new FormPrint(txtTempName.EditValue.ToString(), txtPrint.Text, true, _PrintTableName, Convert.ToInt32(txtPrintNum.Text), txtPointName.EditValue.ToString(), txtPointName.Text, _PrintDataSet); pr.ShowDialog(); } } else { if (chkLPrint.Checked) { PrintReport pr = new PrintReport(txtTempName.EditValue.ToString(), txtPrint.Text, false, _PrintTableName, Convert.ToInt32(txtPrintNum.Text), "", "本地打印", _PrintDataSet); } else { PrintReport pr = new PrintReport(txtTempName.EditValue.ToString(), txtPrint.Text, false, _PrintTableName, Convert.ToInt32(txtPrintNum.Text), txtPointName.EditValue.ToString(), txtPointName.Text, _PrintDataSet); } } } else { if (chkShowDialogFlag.Checked) { if (chkLPrint.Checked) { FormPrint pr = new FormPrint(txtTempName.EditValue.ToString(), txtPrint.Text, true, true, _PrintTableName, Convert.ToInt32(txtPrintNum.Text), "", "本地打印", _PrintParas); pr.ShowDialog(); } else { FormPrint pr = new FormPrint(txtTempName.EditValue.ToString(), txtPrint.Text, false, true, _PrintTableName, Convert.ToInt32(txtPrintNum.Text), txtPointName.EditValue.ToString(), txtPointName.Text, _PrintParas); pr.ShowDialog(); } } else { if (chkLPrint.Checked) { PrintReport pr = new PrintReport(txtTempName.EditValue.ToString(), txtPrint.Text, true, false, _PrintTableName, Convert.ToInt32(txtPrintNum.Text), "", "本地打印", _PrintParas); } else { PrintReport pr = new PrintReport(txtTempName.EditValue.ToString(), txtPrint.Text, false, false, _PrintTableName, Convert.ToInt32(txtPrintNum.Text), txtPointName.EditValue.ToString(), txtPointName.Text, _PrintParas); } } } SetDefaultLastPrinter(); } private void chkShowDialogFlag_CheckedChanged(object sender, EventArgs e) { if (chkShowDialogFlag.Checked) { txtPrintNum.Enabled = false; txtPrintNum.Text = "1"; } else { txtPrintNum.Enabled = true; txtPrintNum.Text = "1"; } } private void SetDefaultLastPrinter() { try { string PrinterPath = System.AppDomain.CurrentDomain.BaseDirectory + "\\temp\\"; string PrinterFileName = PrinterPath + _LableType + _PrintTableName +"printerName.ini"; if (!Directory.Exists(PrinterPath)) { Directory.CreateDirectory(PrinterPath); } if (!File.Exists(PrinterFileName)) { using (File.Create(PrinterFileName)) { } } using (StreamWriter sw = new StreamWriter(PrinterFileName)) { sw.WriteLine(txtTempName.EditValue == null ? "" : txtTempName.EditValue.ToString()); sw.WriteLine(chkLPrint.Checked.ToString()); sw.WriteLine(txtPrint.Text); sw.WriteLine(chkFPrint.Checked.ToString()); sw.WriteLine(txtPointName.EditValue == null ? "" : txtPointName.EditValue.ToString()); sw.WriteLine(chkShowDialogFlag.Checked.ToString()); } } catch { } } private void GetDefaultLastPrinter() { try { string PrinterPath = System.AppDomain.CurrentDomain.BaseDirectory + "\\temp\\"; string PrinterFileName = PrinterPath + _LableType + _PrintTableName + "printerName.ini"; if (!Directory.Exists(PrinterPath)) { Directory.CreateDirectory(PrinterPath); } if (!File.Exists(PrinterFileName)) { using (File.Create(PrinterFileName)) { } } Dictionary infoDic = new Dictionary(); using (StreamReader fReader = new StreamReader(PrinterFileName)) { int key = 0; while (fReader.Peek() > -1) { string value = fReader.ReadLine(); if (value != null) { infoDic.Add(key, value); key++; } } } if (infoDic.Count == 6) { // if (infoDic[0] != "") { if (txtTempName.Properties.DataSource != null) { DataTable dt = txtTempName.Properties.DataSource as DataTable; if (dt.Select("编号 = '" + infoDic[0] + "'").Length > 0) { txtTempName.EditValue = infoDic[0]; } } } if (infoDic[1] != "") { bool boolvalue = false; if (bool.TryParse(infoDic[1], out boolvalue)) { chkLPrint.Checked = boolvalue; } } if (infoDic[2] != "") { if (txtPrint.Properties.Items.Contains(infoDic[2])) { txtPrint.Text = infoDic[2]; } } if (infoDic[3] != "") { bool boolvalue = false; if (bool.TryParse(infoDic[3], out boolvalue)) { chkFPrint.Checked = boolvalue; } } if (infoDic[4] != "") { if (txtPointName.Properties.DataSource != null) { DataTable dt = txtPointName.Properties.DataSource as DataTable; if (dt.Select("编号 = '" + infoDic[4] + "'").Length > 0) { txtPointName.EditValue = infoDic[4]; } } } if (infoDic[5] != "") { bool boolvalue = false; if (bool.TryParse(infoDic[5], out boolvalue)) { chkShowDialogFlag.Checked = boolvalue; } } } } catch { } } } }