华恒Mes鼎捷代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

371 lines
13 KiB

5 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using DevExpress.XtraEditors;
  9. using ICSSoft.Base.Config.AppConfig;
  10. using ICSSoft.Base.Config.DBHelper;
  11. using System.IO;
  12. namespace ICSSoft.Base.Lable.PrintTool
  13. {
  14. public partial class FormPrintDialog : DevExpress.XtraEditors.XtraForm
  15. {
  16. string _LableType = "";
  17. List<PrintPara> _PrintParas = new List<PrintPara>();
  18. bool _RptFlag = false;
  19. DataSet _PrintDataSet = null;
  20. string _PrintTableName = "";
  21. public FormPrintDialog(string lableType, string printTableName, List<PrintPara> printParas, bool rptFlag, DataSet printDataSet)
  22. {
  23. InitializeComponent();
  24. if (rptFlag)
  25. {
  26. chkFPrint.Enabled = false;
  27. txtPointName.Enabled = false;
  28. }
  29. if (chkShowDialogFlag.Checked)
  30. {
  31. txtPrintNum.Enabled = false;
  32. txtPrintNum.Text = "1";
  33. }
  34. _LableType = lableType;
  35. _PrintParas = printParas;
  36. _PrintTableName = printTableName;
  37. _RptFlag = rptFlag;
  38. _PrintDataSet = printDataSet;
  39. BindPrinters();
  40. BindLables();
  41. BindPrintPoint();
  42. GetDefaultLastPrinter();
  43. }
  44. private void BindPrinters()
  45. {
  46. try
  47. {
  48. List<string> list = GetPrinterInfo.GetPrinters();
  49. foreach (string s in list)
  50. {
  51. txtPrint.Properties.Items.Add(s);
  52. }
  53. }
  54. catch (Exception ex)
  55. {
  56. }
  57. }
  58. private void BindLables()
  59. {
  60. string sql = "SELECT ID as [编号],LableName as [名称] FROM dbo.Sys_Lables a WHERE (LableType='{0}' AND WorkPointCode='{1}') ";
  61. sql = string.Format(sql, new object[] { _LableType, AppConfig.WorkPointCode });
  62. DataTable data = DBHelper.ExecuteDataset(AppConfig.FrameConnectString, CommandType.Text, sql).Tables[0];
  63. txtTempName.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("编号", "编号"));
  64. txtTempName.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("名称", "名称"));
  65. txtTempName.Properties.ValueMember = "编号";
  66. txtTempName.Properties.DisplayMember = "名称";
  67. txtTempName.Properties.Columns[0].Visible = false;
  68. txtTempName.Properties.DataSource = data;
  69. if (data.Rows.Count > 0)
  70. {
  71. txtTempName.EditValue = data.Rows[0]["编号"];
  72. }
  73. }
  74. private void BindPrintPoint()
  75. {
  76. string sql = "SELECT ID AS [编号],PointName AS [名称] FROM dbo.Sys_PrintPoint";
  77. DataTable data = DBHelper.ExecuteDataset(AppConfig.FrameConnectString, CommandType.Text, sql).Tables[0];
  78. txtPointName.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("编号", "编号"));
  79. txtPointName.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("名称", "名称"));
  80. txtPointName.Properties.ValueMember = "编号";
  81. txtPointName.Properties.DisplayMember = "名称";
  82. txtPointName.Properties.Columns[0].Visible = false;
  83. txtPointName.Properties.DataSource = data;
  84. if (data.Rows.Count > 0)
  85. {
  86. txtPointName.ItemIndex = 0;
  87. }
  88. }
  89. private void btnOk_Click(object sender, EventArgs e)
  90. {
  91. if (txtTempName.EditValue == null)
  92. {
  93. MessageBox.Show("请选择打印模板!");
  94. return;
  95. }
  96. if (chkLPrint.Checked == false && chkFPrint.Checked == false)
  97. {
  98. MessageBox.Show("请选择本地打印或者远程打印当中的一种打印方式!");
  99. return;
  100. }
  101. if (chkLPrint.Checked == true && chkFPrint.Checked == true)
  102. {
  103. MessageBox.Show("请选择本地打印或者远程打印当中的一种打印方式!");
  104. return;
  105. }
  106. if (chkLPrint.Checked == true)
  107. {
  108. if (txtPrint.Text.Trim() == "")
  109. {
  110. MessageBox.Show("请选择本地打印机!");
  111. return;
  112. }
  113. }
  114. if (chkFPrint.Checked == true)
  115. {
  116. if (txtPointName.EditValue == null)
  117. {
  118. MessageBox.Show("请选择打印地点");
  119. return;
  120. }
  121. }
  122. if (txtPrintNum.Text.Trim() == "")
  123. {
  124. MessageBox.Show("请输入打印张数!");
  125. return;
  126. }
  127. if (_RptFlag == true)
  128. {
  129. if (chkShowDialogFlag.Checked)
  130. {
  131. if (chkLPrint.Checked)
  132. {
  133. FormPrint pr = new FormPrint(txtTempName.EditValue.ToString(), txtPrint.Text, true, _PrintTableName, Convert.ToInt32(txtPrintNum.Text), "", "本地打印", _PrintDataSet);
  134. pr.ShowDialog();
  135. }
  136. else
  137. {
  138. FormPrint pr = new FormPrint(txtTempName.EditValue.ToString(), txtPrint.Text, true, _PrintTableName, Convert.ToInt32(txtPrintNum.Text), txtPointName.EditValue.ToString(), txtPointName.Text, _PrintDataSet);
  139. pr.ShowDialog();
  140. }
  141. }
  142. else
  143. {
  144. if (chkLPrint.Checked)
  145. {
  146. PrintReport pr = new PrintReport(txtTempName.EditValue.ToString(), txtPrint.Text, false, _PrintTableName, Convert.ToInt32(txtPrintNum.Text), "", "本地打印", _PrintDataSet);
  147. }
  148. else
  149. {
  150. PrintReport pr = new PrintReport(txtTempName.EditValue.ToString(), txtPrint.Text, false, _PrintTableName, Convert.ToInt32(txtPrintNum.Text), txtPointName.EditValue.ToString(), txtPointName.Text, _PrintDataSet);
  151. }
  152. }
  153. }
  154. else
  155. {
  156. if (chkShowDialogFlag.Checked)
  157. {
  158. if (chkLPrint.Checked)
  159. {
  160. FormPrint pr = new FormPrint(txtTempName.EditValue.ToString(), txtPrint.Text, true, true, _PrintTableName, Convert.ToInt32(txtPrintNum.Text), "", "本地打印", _PrintParas);
  161. pr.ShowDialog();
  162. }
  163. else
  164. {
  165. FormPrint pr = new FormPrint(txtTempName.EditValue.ToString(), txtPrint.Text, false, true, _PrintTableName, Convert.ToInt32(txtPrintNum.Text), txtPointName.EditValue.ToString(), txtPointName.Text, _PrintParas);
  166. pr.ShowDialog();
  167. }
  168. }
  169. else
  170. {
  171. if (chkLPrint.Checked)
  172. {
  173. PrintReport pr = new PrintReport(txtTempName.EditValue.ToString(), txtPrint.Text, true, false, _PrintTableName, Convert.ToInt32(txtPrintNum.Text), "", "本地打印", _PrintParas);
  174. }
  175. else
  176. {
  177. PrintReport pr = new PrintReport(txtTempName.EditValue.ToString(), txtPrint.Text, false, false, _PrintTableName, Convert.ToInt32(txtPrintNum.Text), txtPointName.EditValue.ToString(), txtPointName.Text, _PrintParas);
  178. }
  179. }
  180. }
  181. SetDefaultLastPrinter();
  182. }
  183. private void chkShowDialogFlag_CheckedChanged(object sender, EventArgs e)
  184. {
  185. if (chkShowDialogFlag.Checked)
  186. {
  187. txtPrintNum.Enabled = false;
  188. txtPrintNum.Text = "1";
  189. }
  190. else
  191. {
  192. txtPrintNum.Enabled = true;
  193. txtPrintNum.Text = "1";
  194. }
  195. }
  196. private void SetDefaultLastPrinter()
  197. {
  198. try
  199. {
  200. string PrinterPath = System.AppDomain.CurrentDomain.BaseDirectory + "\\temp\\";
  201. string PrinterFileName = PrinterPath + _LableType + _PrintTableName +"printerName.ini";
  202. if (!Directory.Exists(PrinterPath))
  203. {
  204. Directory.CreateDirectory(PrinterPath);
  205. }
  206. if (!File.Exists(PrinterFileName))
  207. {
  208. using (File.Create(PrinterFileName))
  209. {
  210. }
  211. }
  212. using (StreamWriter sw = new StreamWriter(PrinterFileName))
  213. {
  214. sw.WriteLine(txtTempName.EditValue == null ? "" : txtTempName.EditValue.ToString());
  215. sw.WriteLine(chkLPrint.Checked.ToString());
  216. sw.WriteLine(txtPrint.Text);
  217. sw.WriteLine(chkFPrint.Checked.ToString());
  218. sw.WriteLine(txtPointName.EditValue == null ? "" : txtPointName.EditValue.ToString());
  219. sw.WriteLine(chkShowDialogFlag.Checked.ToString());
  220. }
  221. }
  222. catch
  223. {
  224. }
  225. }
  226. private void GetDefaultLastPrinter()
  227. {
  228. try
  229. {
  230. string PrinterPath = System.AppDomain.CurrentDomain.BaseDirectory + "\\temp\\";
  231. string PrinterFileName = PrinterPath + _LableType + _PrintTableName + "printerName.ini";
  232. if (!Directory.Exists(PrinterPath))
  233. {
  234. Directory.CreateDirectory(PrinterPath);
  235. }
  236. if (!File.Exists(PrinterFileName))
  237. {
  238. using (File.Create(PrinterFileName))
  239. {
  240. }
  241. }
  242. Dictionary<int, string> infoDic = new Dictionary<int, string>();
  243. using (StreamReader fReader = new StreamReader(PrinterFileName))
  244. {
  245. int key = 0;
  246. while (fReader.Peek() > -1)
  247. {
  248. string value = fReader.ReadLine();
  249. if (value != null)
  250. {
  251. infoDic.Add(key, value);
  252. key++;
  253. }
  254. }
  255. }
  256. if (infoDic.Count == 6)
  257. { //
  258. if (infoDic[0] != "")
  259. {
  260. if (txtTempName.Properties.DataSource != null)
  261. {
  262. DataTable dt = txtTempName.Properties.DataSource as DataTable;
  263. if (dt.Select("编号 = '" + infoDic[0] + "'").Length > 0)
  264. {
  265. txtTempName.EditValue = infoDic[0];
  266. }
  267. }
  268. }
  269. if (infoDic[1] != "")
  270. {
  271. bool boolvalue = false;
  272. if (bool.TryParse(infoDic[1], out boolvalue))
  273. {
  274. chkLPrint.Checked = boolvalue;
  275. }
  276. }
  277. if (infoDic[2] != "")
  278. {
  279. if (txtPrint.Properties.Items.Contains(infoDic[2]))
  280. {
  281. txtPrint.Text = infoDic[2];
  282. }
  283. }
  284. if (infoDic[3] != "")
  285. {
  286. bool boolvalue = false;
  287. if (bool.TryParse(infoDic[3], out boolvalue))
  288. {
  289. chkFPrint.Checked = boolvalue;
  290. }
  291. }
  292. if (infoDic[4] != "")
  293. {
  294. if (txtPointName.Properties.DataSource != null)
  295. {
  296. DataTable dt = txtPointName.Properties.DataSource as DataTable;
  297. if (dt.Select("编号 = '" + infoDic[4] + "'").Length > 0)
  298. {
  299. txtPointName.EditValue = infoDic[4];
  300. }
  301. }
  302. }
  303. if (infoDic[5] != "")
  304. {
  305. bool boolvalue = false;
  306. if (bool.TryParse(infoDic[5], out boolvalue))
  307. {
  308. chkShowDialogFlag.Checked = boolvalue;
  309. }
  310. }
  311. }
  312. }
  313. catch
  314. {
  315. }
  316. }
  317. }
  318. }