FrmShelfPrinterMain.cs 8.7 KB
using OnlineStore.Common;
using OnlineStore.DeviceLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ShelfPrinter
{
    public partial class FrmShelfPrinterMain : Form
    {
        private Asa.PrintLabel print;
        string labelName;
        private ContextMenuStrip notifyMenu;
        public FrmShelfPrinterMain()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            PrintLabel.Common.MULTIPLE = 100f;
            print = new Asa.PrintLabel(Application.StartupPath + "\\Label");
            print.PrintStatusChanged += Print_PrintStatusChanged;
            labelName = ConfigAppSettings.GetValue("LabelName", "lizhen");
            string printerName = ConfigAppSettings.GetValue("PrinterName", "Xprinter XP-236B");
            string[] printerNames = print.GetPrinterName();
            if (printerNames == null || printerNames.Length == 0)
            {
                MessageBox.Show("未检测到打印机,请检查打印机是否连接");
                return;
            }
            notifyMenu = new ContextMenuStrip();
            ToolStripMenuItem itemShow = new ToolStripMenuItem("显示(&S)");
            itemShow.Click += ItemShow_Click;
            ToolStripMenuItem itemExit = new ToolStripMenuItem("退出(&X)");
            itemExit.Click += ItemExit_Click;
            notifyMenu.Items.Add(itemShow);
            notifyMenu.Items.Add(itemExit);
            notifyIcon1.ContextMenuStrip = notifyMenu;
            lblLabelName.Text = labelName;
            txtIp.Text = printerName; //printerNames[0];
            print.Printer(printerName, false);
            
            timer1.Enabled = true;
        }
        Asa.PrintLabel.PrinterStatus printerStatus = Asa.PrintLabel.PrinterStatus.Idle;
        private void Print_PrintStatusChanged(Asa.PrintLabel.PrinterStatus sta, string msg)
        {
            this.Invoke(new Action(() =>
            {
                printerStatus = sta;
                lblState.Text = $"打印机状态:{msg}";
            }));
        }
        private void ItemExit_Click(object sender, EventArgs e)
        {
            //notify.Dispose();
            //exit = true;
            //Close();
            ExitApp();
        }

        private void ItemShow_Click(object sender, EventArgs e)
        {
            this.Opacity = 100;
            this.Visible = true;
            //this.WindowState = FormWindowState.Maximized;
            this.notifyIcon1.Visible = false;
            this.ShowInTaskbar = true;
        }
        void printLabel(Label_LZ label_LZ)
        {
            try
            {
                print.LoadLabel(labelName);
                Dictionary<string, string> text = new Dictionary<string, string>();
                text.Add("lh", label_LZ.pn);
                text.Add("jt", label_LZ.station);
                text.Add("xb", label_LZ.line);
                text.Add("sj", label_LZ.shijian);
                text.Add("zb", label_LZ.slot);
                pictureBox1.Image = print.Print(text);
            }catch(Exception ex)
            {
                LogUtil.error("printLabel", ex);
            }

        }
        Dictionary<string, string> printview(Label_LZ label_LZ)
        {
            print.LoadLabel(labelName);
            Dictionary<string, string> text = new Dictionary<string, string>();
            text.Add("lh", label_LZ.pn);
            text.Add("jt", label_LZ.station);
            text.Add("xb", label_LZ.line);
            text.Add("sj", label_LZ.shijian);
            text.Add("zb", label_LZ.slot);
            if (pictureBox1.Image != null)
            {
                pictureBox1.Image.Dispose();
            }
            pictureBox1.Image = print.PrintPreview(text);
            return text;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            print.EditLabel();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            groupBox1.Enabled = checkBox1.Checked;
            timer1.Enabled = !checkBox1.Checked;
        }

        private void lblLabelName_Click(object sender, EventArgs e)
        {
            ConfigHelper.AdvanceConfigForm.ShowEditDialog(this);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            printLabel(new Label_LZ(textBox1.Text, textBox3.Text, textBox4.Text, textBox2.Text,textBox5.Text,textBox6.Text));
        }
        string[] cids = ConfigAppSettings.GetValue("cid", "01#02").Split('#');
        object handleObj = new object();
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Monitor.TryEnter(handleObj))
            {
                try
                {
                    foreach (var item in cids)
                    {
                        Label_LZ labels = SServerManager.GetPrintInfo(item);
                        if (labels != null)
                        {
                            // if (printerStatus.Equals(Asa.PrintLabel.PrinterStatus.Idle))
                            {
                                timer1.Enabled = false;
                                //foreach (var item in labels)
                                {
                                    lblLabelInfo.Text = $"打印任务[{item}]:{labels.ToStr()}";
                                    //printLabel(new Label_LZ(labels.pn, labels.side, labels.station, labels.line));
                                    //while (printerStatus.Equals(Asa.PrintLabel.PrinterStatus.Printing)
                                    //    || printerStatus.Equals(Asa.PrintLabel.PrinterStatus.Print))
                                    //{
                                    //    Thread.Sleep(300);
                                    //}
                                    LogUtil.info($"自动打印[{printerStatus.ToString()}]:{labels.ToStr()}");
                                    while (true)
                                    {
                                        if (SServerManager.FinishPrint(item))
                                            break;
                                        Thread.Sleep(1000);
                                    }
                                }
                                timer1.Enabled = true;
                            }
                            //else
                            //{
                            //    lblLabelInfo.Text = "无法打印标签,因打印机不空闲";
                            //}
                        }
                        lblLabelInfo.Text = "";
                    }

                }
                catch (Exception ex)
                {
                    LogUtil.error($"处理打印异常:", ex);
                }
                finally
                {
                    Monitor.Exit(handleObj);
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                print.PrintPreview(printview(new Label_LZ(textBox1.Text, textBox3.Text, textBox4.Text, textBox2.Text)));
            }
            catch
            {

            }
        }

        private void FrmShelfPrinterMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)//当界面点击窗体右上角X按钮或(Alt + F4)时 发生           
            {
                e.Cancel = true;
                HideForm();
            }
        }
        private void HideForm()
        {
            this.Opacity = 0;
            this.ShowInTaskbar = false;
            this.notifyIcon1.Visible = true;

            this.Hide();
            GC.Collect();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Opacity = 100;
            this.Visible = true;
            //this.WindowState = FormWindowState.Maximized;
            this.notifyIcon1.Visible = false;
            this.ShowInTaskbar = true;
        }
        private void ExitApp()
        {
            DialogResult result = MessageBox.Show("是否确定退出客户端?", "提示", MessageBoxButtons.YesNo);
            if (result.Equals(DialogResult.Yes))
            {
                try
                {
                    System.Environment.Exit(System.Environment.ExitCode);
                }
                catch (Exception ex)
                {
                    LogUtil.error("退出出错:", ex);
                }
                //this.Close();
            }
        }
    }
}