Program.cs 7.3 KB
using BLL;
using CodeLibrary;
using ConfigHelper;
using DeviceLibrary;
using log4net.Config;
using Newtonsoft.Json;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoScanAndLabel
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);


                SolidBrush blue = new SolidBrush(Color.BlueViolet);
                SolidBrush red = new SolidBrush(Color.Red);
                SolidBrush red2 = new SolidBrush(Color.MediumVioletRed);
                SolidBrush yellow = new SolidBrush(Color.Yellow);
                SolidBrush AliceBlue = new SolidBrush(Color.AliceBlue);
                SolidBrush Green = new SolidBrush(Color.Green);
                /*
                Font font = new Font(FontFamily.GenericSansSerif, 60);
                Pen Greenpen = new Pen(Color.Green, 7);
                //Size pointsize = new Size(15, 15);
                int pointsize = 16;

                var codetxt = @"[{""CodeStr"":""1T0009938251"",""X"":1605,""Y"":2047,""CodeType"":""Code 128"",""Orientation"":35.0,""LabelCenter"":""0, 0"",""LabelDistance"":0},{""CodeStr"":""10D2547"",""X"":1652,""Y"":1973,""CodeType"":""Code 128"",""Orientation"":34.0,""LabelCenter"":""0, 0"",""LabelDistance"":0},{""CodeStr"":""1PCRCW0805249KFHEAP"",""X"":1852,""Y"":1909,""CodeType"":""Code 128"",""Orientation"":34.0,""LabelCenter"":""0, 0"",""LabelDistance"":0},{""CodeStr"":""XA7IF 249KF     "",""X"":2301,""Y"":1460,""CodeType"":""Code 39"",""Orientation"":43.0,""LabelCenter"":""0, 0"",""LabelDistance"":0},{""CodeStr"":""Q5000"",""X"":1654,""Y"":1876,""CodeType"":""Code 128"",""Orientation"":39.0,""LabelCenter"":""0, 0"",""LabelDistance"":0},{""CodeStr"":""4LIL"",""X"":1512,""Y"":2060,""CodeType"":""Code 128"",""Orientation"":38.0,""LabelCenter"":""0, 0"",""LabelDistance"":0},{""CodeStr"":""701011124;0040;5000;CRCW0805249KFHEAP;BC COMP VISHAY;2547;1010025249;20260204;;0009938251"",""X"":1607,""Y"":1583,""CodeType"":""Data Matrix ECC 200"",""Orientation"":36.0,""LabelCenter"":""0, 0"",""LabelDistance"":0},{""CodeStr"":""CRCW0805249KFHEAP000993825111/21/25                                                       202547IL     5000.00000329245390001               2547    61410010  50                                                     9938251  5000      00041569040001        CRCW0805249KFHEAPCRCW0805-P 50 249K 1% ET1                B0R013674260"",""X"":2437,""Y"":1305,""CodeType"":""PDF417"",""Orientation"":0.0,""LabelCenter"":""0, 0"",""LabelDistance"":0}]";
                var codelists= JsonConvert.DeserializeObject<List<CodeInfo>>(codetxt);
                var bmp = new Bitmap(@"D:\logs\123\2026-02-09-16-24-3891.bmp");
                var bmp2 = (Bitmap)bmp.Clone();
                var result = RemoteDecodeHelper_mod.NeoSacnRequest(RemoteDecodeHelper_mod.BitmapToBase64(bmp), codelists, 7, 2059, 1755);

                var labelPoint = new Point(result.positionAngle.X, result.positionAngle.Y);

                Graphics graphics = Graphics.FromImage(bmp2);
                graphics.FillEllipse(red, Common.EllipseCenter(new Point(2059, 1755), pointsize));
                graphics.FillEllipse(red, Common.EllipseCenter(labelPoint, pointsize));

                var l2 = Common.PointWithAngle(labelPoint, result.positionAngle.Angle, 200);
                graphics.DrawLine(Greenpen, labelPoint, l2);

                graphics.Save();
                bmp2.Save(@"D:\logs\123\2026-02-09-16-24-3891_result.bmp");
                return;
                */
                _ = new Mutex(true, Application.ProductName, out bool ret);
                if (!ret)
                {
                    IntPtr formhwnd = FindWindow(null, Config.Get(Setting_Init.App_Title));
                    ShowWindow(formhwnd, SW_RESTORE);
                    SwitchToThisWindow(formhwnd, true);
                    //MessageBox.Show("该程序已经启动", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                Environment.CurrentDirectory = Application.StartupPath;
                XmlConfigurator.Configure();
                Config.LoadMyConfig(typeof(Setting_Init));
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            catch (Exception ex)
            {
                LogUtil.error(ex.ToString());
                throw new Exception(ex.ToString());
            }         
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            LogUnhandledException("CurrentDomain_UnhandledException", e.ToString() + "" + e.ExceptionObject.ToString() + " ");
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            LogUnhandledException("Application_ThreadException", e.ToString() + "" + e.Exception.ToString() + " ");
        }
        static void LogUnhandledException(string type, string exceptionobj)
        {
            //这里可以进一步地写日志
            LogUtil.error("【" + type + "】" + exceptionobj);
            MessageBox.Show(exceptionobj, type);

            LogUtil.error("【" + type + "】" + exceptionobj);
        }
        #region Win32函数的声明

        /// <summary>
        /// 找到某个窗口与给出的类别名和窗口名相同窗口 
        /// </summary>
        /// <param name="lpClassName">类别名</param>
        /// <param name="lpWindowName">窗口名</param>
        /// <returns>成功找到返回窗口句柄,否则返回null</returns>
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        /// <summary>
        /// 切换到窗口并把窗口设入前台,类似 SetForegroundWindow方法的功能
        /// </summary>
        /// <param name="hWnd">窗口句柄</param>
        /// <param name="fAltTab">True代表窗口正在通过Alt/Ctrl +Tab被切换</param>
        [DllImport("user32.dll ", SetLastError = true)]
        static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

        /// <summary>
        ///  设置窗口的显示状态 
        /// </summary>
        /// <param name="hWnd">窗口句柄</param>
        /// <param name="cmdShow">指示窗口如何被显示</param>
        /// <returns>如果窗体之前是可见,返回值为非零;如果窗体之前被隐藏,返回值为零</returns>
        [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
        public const int SW_RESTORE = 9;

        #endregion

    }
}