用WinForm實(shí)現(xiàn)一款打印服務(wù),使用WebApi通訊,無數(shù)據(jù)庫1.0版,支持各種終端調(diào)用。







#region WebApi接口服務(wù)
public static OWINTriggerEvent TriggerEvent { get; set; }
private IDisposable _webApp; private string BaseUrl = "http://192.168.100.84:2000";
private void StartServer() { string serverIp = ConfigurationManager.AppSettings["ServerIP"]; string serverPort = ConfigurationManager.AppSettings["ServerPort"]; BaseUrl = $"http://{serverIp}:{serverPort}";
_webApp = WebApp.Start<Startup>(BaseUrl); rtbLog.Clear(); AppendLog($"服務(wù)已啟動(dòng): {BaseUrl}");
TriggerEvent = new OWINTriggerEvent(); TriggerEvent.infoEvent += TriggerEvent_infoEvent; }
private async void TriggerEvent_infoEvent(object sender, EventArgs e) { try { if (rtbLog.InvokeRequired) { rtbLog.BeginInvoke(new Action(() => rtbLog.Clear())); } else rtbLog.Clear();
AppendLog($"服務(wù)已啟動(dòng): {BaseUrl}");
string jsonStr = TriggerEvent.Message; AppendLog("接收json數(shù)據(jù):" + JsonConvert.SerializeObject(jsonStr)); AppendLog("開始打印");
string outputDirectory = AppDomain.CurrentDomain.BaseDirectory; DataTable dtinfo = JsonConverter.JsonToDataTable(jsonStr);
if (this.InvokeRequired) { this.Invoke(new MethodInvoker(delegate { report.Load($"{outputDirectory}\\Report.mrt"); report.RegData("TableA", dtinfo);
report.Compile(); report.Print(); AppendLog("打印完成"); })); } } catch (Exception ex) { AppendLog("接收到信息異常:" + ex.Message); } }
private void StopServer() { _webApp?.Dispose(); AppendLog("服務(wù)已停止"); }
private void AppendLog(string message) { DateTime d = DateTime.Now; string dateStr = d.ToString("yyyy-MM-dd HH:mm:ss:fff");
if (rtbLog.InvokeRequired) rtbLog.BeginInvoke(new Action(() => rtbLog.AppendText(dateStr + " " + message + "\r\n"))); else rtbLog.AppendText(dateStr + " " + message + "\r\n"); } #endregion
namespace StuimusoftReport{ partial class PrintService { private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); }
#region Windows 窗體設(shè)計(jì)器生成的代碼
private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.panel1 = new System.Windows.Forms.Panel(); this.rtbLog = new System.Windows.Forms.RichTextBox(); this.panel2 = new System.Windows.Forms.Panel(); this.btStart = new System.Windows.Forms.Button(); this.btStop = new System.Windows.Forms.Button(); this.panel1.SuspendLayout(); this.panel2.SuspendLayout(); this.SuspendLayout(); this.button1.Font = new System.Drawing.Font("宋體", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.button1.Location = new System.Drawing.Point(650, 42); this.button1.Margin = new System.Windows.Forms.Padding(4); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(160, 50); this.button1.TabIndex = 0; this.button1.Text = "設(shè)計(jì)模板"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); this.button2.Font = new System.Drawing.Font("宋體", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.button2.Location = new System.Drawing.Point(868, 42); this.button2.Margin = new System.Windows.Forms.Padding(4); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(160, 50); this.button2.TabIndex = 1; this.button2.Text = "預(yù)覽模板"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); this.panel1.Controls.Add(this.rtbLog); this.panel1.Controls.Add(this.panel2); this.panel1.Location = new System.Drawing.Point(0, 15); this.panel1.Margin = new System.Windows.Forms.Padding(4); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(1203, 616); this.panel1.TabIndex = 4; this.rtbLog.Dock = System.Windows.Forms.DockStyle.Fill; this.rtbLog.Location = new System.Drawing.Point(0, 125); this.rtbLog.Margin = new System.Windows.Forms.Padding(4); this.rtbLog.Name = "rtbLog"; this.rtbLog.Size = new System.Drawing.Size(1203, 491); this.rtbLog.TabIndex = 3; this.rtbLog.Text = ""; this.panel2.Controls.Add(this.button1); this.panel2.Controls.Add(this.button2); this.panel2.Controls.Add(this.btStart); this.panel2.Controls.Add(this.btStop); this.panel2.Dock = System.Windows.Forms.DockStyle.Top; this.panel2.Location = new System.Drawing.Point(0, 0); this.panel2.Margin = new System.Windows.Forms.Padding(4); this.panel2.Name = "panel2"; this.panel2.Size = new System.Drawing.Size(1203, 125); this.panel2.TabIndex = 4; this.btStart.Location = new System.Drawing.Point(112, 42); this.btStart.Margin = new System.Windows.Forms.Padding(4); this.btStart.Name = "btStart"; this.btStart.Size = new System.Drawing.Size(160, 50); this.btStart.TabIndex = 0; this.btStart.Text = "開啟服務(wù)"; this.btStart.UseVisualStyleBackColor = true; this.btStart.Click += new System.EventHandler(this.btStart_Click); this.btStop.Location = new System.Drawing.Point(332, 42); this.btStop.Margin = new System.Windows.Forms.Padding(4); this.btStop.Name = "btStop"; this.btStop.Size = new System.Drawing.Size(160, 50); this.btStop.TabIndex = 1; this.btStop.Text = "停止服務(wù)"; this.btStop.UseVisualStyleBackColor = true; this.btStop.Click += new System.EventHandler(this.btStop_Click); this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1219, 646); this.Controls.Add(this.panel1); this.Margin = new System.Windows.Forms.Padding(4); this.Name = "PrintService"; this.Text = "打印服務(wù)1.0"; this.panel1.ResumeLayout(false); this.panel2.ResumeLayout(false); this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Panel panel1; private System.Windows.Forms.RichTextBox rtbLog; private System.Windows.Forms.Panel panel2; private System.Windows.Forms.Button btStart; private System.Windows.Forms.Button btStop; }}
閱讀原文:原文鏈接
該文章在 2025/8/12 11:05:19 編輯過