LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C# WinForm实现WebApi打印服务

admin
2025年8月12日 7:31 本文热度 54

用WinForm实现一款打印服务,使用WebApi通讯,无数据库1.0版,支持各种终端调用。

一、效果展示
打印服务
模板设计
模板预览
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类实例,并返回被调用方使用获取里面的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数据:" + 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>        /// 设计器支持所需的方法 - 不要修改        /// 使用代码编辑器修改此方法的内容。        /// </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是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved