1.添加Windows服务项目
2.添加安装程序
在右键点击主界面,添加安装程序
3.添加组件
在工具箱拖动服务一个组件到主界面
4.主要实现代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
public bool SendTextFileToPrinter(string szFileName, string m_PrinterName, Log log, string sourcePath) { log.WriteLog("[" + m_PrinterName + "] 准备打印: " + szFileName); using (PdfDocument doc = new PdfDocument()) { doc.LoadFromFile(szFileName); log.WriteLog(doc.Pages.Count.ToString()); //doc.PrintFromPage = 1; //doc.PrintToPage = doc.Pages.Count; doc.PrintSettings.PrinterName = m_PrinterName; //此行代码为选择打印机名称来打印 //毫米转百英寸 int printWidth = (int)(Math.Round(101 * 0.03937 * 100, 0));//394 int printHeight = (int)(Math.Round(152 * 0.03937 * 100, 0));//197 PaperSize paper = new PaperSize("123", printWidth, printHeight); paper.RawKind = (int)PaperKind.Custom; //设置打印的纸张大小 doc.PrintSettings.PaperSize = paper; //需要选择FitSize打印模式 false为纵向打印 doc.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode.FitSize, false); doc.Print(); } log.WriteLog("[" + m_PrinterName + "] 打印成功!: "+ szFileName); //删除打印成功的PDF //if (File.Exists(szFileName)) // File.Delete(szFileName); return true; } |
5.服务管理界面主要代码
1)初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
private void FormMain_Load(object sender, EventArgs e) { try { m_Data = new DataSet(); m_SettingFile = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "TaskSettings.xml"; if (!File.Exists(m_SettingFile)) MakeTaskSettings(m_SettingFile); m_Data.ReadXml(m_SettingFile); if (m_Data == null || m_Data.Tables.Count < 1) { throw new Exception("No task settings found."); } this.dataGridView1.DataSource = m_Data; this.dataGridView1.DataMember = m_Data.Tables[0].TableName; if (m_Data.Tables[0].Rows.Count > 0) dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0]; } catch (Exception ex) { MessageBox.Show(ex.Message); } try { m_Service = new ServiceController("DamsonwarePrintServiceZpl"); if (m_Service.Status.Equals(ServiceControllerStatus.Running)) { buttonStop.Enabled = true; buttonStart.Enabled = false; } else { buttonStop.Enabled = false; buttonStart.Enabled = true; } } catch(Exception ee) { buttonStart.Enabled = false; buttonStop.Enabled = false; MessageBox.Show("Checking Service status have error: " + ee.Message); } } |
2)保存代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
private void buttonSave_Click(object sender, EventArgs e) { try { DataRow row; if (m_New) { row = m_Data.Tables[0].NewRow(); m_Data.Tables[0].Rows.Add(row); } else row = m_Data.Tables[0].Rows[this.dataGridView1.CurrentRow.Index]; string DataPath = textBoxDataPath.Text; if (!System.IO.Directory.Exists(DataPath)) System.IO.Directory.CreateDirectory(DataPath); //row["TaskName"] = textBoxJobName.Text; row["LogFileName"] = textBoxLogFileName.Text; //row["RunCycle"] = comboBoxRunCycle.Text; //row["RepeatCycle"] = textBoxRepeatCycle.Text; //row["JobStartTime"] = textBoxJobStartTime.Text; //row["ErrorMailTo"] = textBoxErrorMailTo.Text; //row["JobEnable"] = comboBoxJobEnable.Text; row["PrinterName"] = textBoxPrinterName.Text; row["DataPath"] = DataPath; //row["ArchPath"] = textBoxArchPath.Text; //row["FileFilter"] = textBoxFileFilter.Text; m_Data.WriteXml(m_SettingFile); this.groupBox1.Enabled = false; this.buttonDelete.Enabled = true; //this.buttonNew.Enabled = true; this.buttonEdit.Enabled = true; this.buttonSave.Enabled = false; if (m_New) { dataGridView1.CurrentCell = dataGridView1.Rows[m_Data.Tables[0].Rows.Count - 1].Cells[0]; } MessageBox.Show("Successfully saved!"); } catch (Exception ex) { MessageBox.Show("Saving have error: " + ex.Message); } } |
3)启动服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
private void buttonStart_Click(object sender, EventArgs e) { try { //m_Service.Start(); //ServiceController serviceController = new ServiceController("DamsonwarePrintServiceZpl"); //serviceController.Start(); //if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName); //bat文件路径 string path = "启动服务并设置自动DamsonwarePrintServicePDF.bat"; Process pro = new Process(); FileInfo file = new FileInfo(path); pro.StartInfo.WorkingDirectory = file.Directory.FullName; pro.StartInfo.FileName = path; pro.StartInfo.CreateNoWindow = false; pro.Start(); pro.WaitForExit(); MessageBox.Show("PDF自动打印服务运行成功!"); buttonStop.Enabled = true; buttonStart.Enabled = false; } catch (Exception ex) { MessageBox.Show("Trying to start service have error: " + ex.Message); } m_Service.Refresh(); } |
效果图:
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。