LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C# WinForm實現(xiàn)WebApi打印服務

admin
2025年8月12日 7:31 本文熱度 308

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

一、效果展示
打印服務
模板設計
模板預覽
Postman測試
接收到打印指令
打印完成
打印效果
二、程序設計
界面設計
核心代碼
        #region WebApi接口服務
        public static OWINTriggerEvent TriggerEvent { getset; }
        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($"服務已啟動: {BaseUrl}");
            //新建OWINTriggerEvent類實例,并返回被調(diào)用方使用獲取里面的message和infoEvent事件            //var triggerEvent = new OWINTriggerEvent();            TriggerEvent = new OWINTriggerEvent();            TriggerEvent.infoEvent += TriggerEvent_infoEvent;        }
        /// <summary>        ///接收事件         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private async void TriggerEvent_infoEvent(object sender, EventArgs e)        {            try            {                if (rtbLog.InvokeRequired)                {                    rtbLog.BeginInvoke(new Action(() => rtbLog.Clear()));                }                else                    rtbLog.Clear();
                AppendLog($"服務已啟動: {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("服務已停止");        }
        // 跨線程安全更新UI        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    {        /// <summary>        /// 必需的設計器變量。        /// </summary>        private System.ComponentModel.IContainer components = null;
        /// <summary>        /// 清理所有正在使用的資源。        /// </summary>        /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>        protected override void Dispose(bool disposing)        {            if (disposing && (components != null))            {                components.Dispose();            }            base.Dispose(disposing);        }
        #region Windows 窗體設計器生成的代碼
        /// <summary>        /// 設計器支持所需的方法 - 不要修改        /// 使用代碼編輯器修改此方法的內(nèi)容。        /// </summary>        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();            //             // button1            //             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(65042);            this.button1.Margin = new System.Windows.Forms.Padding(4);            this.button1.Name = "button1";            this.button1.Size = new System.Drawing.Size(16050);            this.button1.TabIndex = 0;            this.button1.Text = "設計模板";            this.button1.UseVisualStyleBackColor = true;            this.button1.Click += new System.EventHandler(this.button1_Click);            //             // button2            //             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(86842);            this.button2.Margin = new System.Windows.Forms.Padding(4);            this.button2.Name = "button2";            this.button2.Size = new System.Drawing.Size(16050);            this.button2.TabIndex = 1;            this.button2.Text = "預覽模板";            this.button2.UseVisualStyleBackColor = true;            this.button2.Click += new System.EventHandler(this.button2_Click);            //             // panel1            //             this.panel1.Controls.Add(this.rtbLog);            this.panel1.Controls.Add(this.panel2);            this.panel1.Location = new System.Drawing.Point(015);            this.panel1.Margin = new System.Windows.Forms.Padding(4);            this.panel1.Name = "panel1";            this.panel1.Size = new System.Drawing.Size(1203616);            this.panel1.TabIndex = 4;            //             // rtbLog            //             this.rtbLog.Dock = System.Windows.Forms.DockStyle.Fill;            this.rtbLog.Location = new System.Drawing.Point(0125);            this.rtbLog.Margin = new System.Windows.Forms.Padding(4);            this.rtbLog.Name = "rtbLog";            this.rtbLog.Size = new System.Drawing.Size(1203491);            this.rtbLog.TabIndex = 3;            this.rtbLog.Text = "";            //             // panel2            //             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(00);            this.panel2.Margin = new System.Windows.Forms.Padding(4);            this.panel2.Name = "panel2";            this.panel2.Size = new System.Drawing.Size(1203125);            this.panel2.TabIndex = 4;            //             // btStart            //             this.btStart.Location = new System.Drawing.Point(11242);            this.btStart.Margin = new System.Windows.Forms.Padding(4);            this.btStart.Name = "btStart";            this.btStart.Size = new System.Drawing.Size(16050);            this.btStart.TabIndex = 0;            this.btStart.Text = "開啟服務";            this.btStart.UseVisualStyleBackColor = true;            this.btStart.Click += new System.EventHandler(this.btStart_Click);            //             // btStop            //             this.btStop.Location = new System.Drawing.Point(33242);            this.btStop.Margin = new System.Windows.Forms.Padding(4);            this.btStop.Name = "btStop";            this.btStop.Size = new System.Drawing.Size(16050);            this.btStop.TabIndex = 1;            this.btStop.Text = "停止服務";            this.btStop.UseVisualStyleBackColor = true;            this.btStop.Click += new System.EventHandler(this.btStop_Click);            //             // PrintService            //             this.AutoScaleDimensions = new System.Drawing.SizeF(8F15F);            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;            this.ClientSize = new System.Drawing.Size(1219646);            this.Controls.Add(this.panel1);            this.Margin = new System.Windows.Forms.Padding(4);            this.Name = "PrintService";            this.Text = "打印服務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 編輯過
關鍵字查詢
相關文章
正在查詢...
點晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運作、調(diào)度、堆場、車隊、財務費用、相關報表等業(yè)務管理,結合碼頭的業(yè)務特點,圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
亚洲日韩在线精品视频第二页 | 野狼AV午夜福利在线观看 | 在线免费人成观看视频 | 亚州精品在线视频 | 日本精品激情乱一区二区 | 天堂v亚洲国产v第一次 |