Program.cs 5.7 KB
using ConfigHelper;
using log4net.Config;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
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);
                //DeviceLibrary.LabelParam labelParam = new DeviceLibrary.LabelParam();
                //labelParam.codeInfos = new List<CodeLibrary.CodeInfo>();
                ////##340093060037##01410000441770##012100000224##Q3000
                ////labelParam.codeInfos.Add(new CodeLibrary.CodeInfo("Y38072732TLP785(D4GHTR6,F         (C                                                   2000A0132G0 2000ZUY0HG1U8501F2A Y0HG1U 2124   2124TWCN          69                                          T2", 0, 0));
                ////labelParam.codeInfos.Add(new CodeLibrary.CodeInfo("[)>P1PTLV7022DGKR6P2PAQ25000V00333171T0557736ZEW4WTKYD210631T1032393ASH20LRFB21LUSA22LASH23LCHNEG43Z1/260C/UNLIM;//;022121L18087KN01", 0, 0));
                //labelParam.codeInfos.Add(new CodeLibrary.CodeInfo("99700736000619EM6H38.125008625", 0, 0));
                ////labelParam.codeInfos.Add(new CodeLibrary.CodeInfo("CK7T0H.125002115", 0, 0));
                ////labelParam.codeInfos.Add(new CodeLibrary.CodeInfo("Y38073304TLP385(D4GREETRE         (T                                               00003000A0149T 03000WT0HBGCC8210B3F-5595   2009   2009JPTH  00      67                         91432487           02Q", 0, 0));
                ////labelParam.codeInfos.Add(new CodeLibrary.CodeInfo("01410000441770", 0, 0));
                ////labelParam.codeInfos.Add(new CodeLibrary.CodeInfo("012100000224", 0, 0));
                ////labelParam.codeInfos.Add(new CodeLibrary.CodeInfo("Q3000", 0, 0));
                //labelParam.codeInfos.Add(new CodeLibrary.CodeInfo("340093060037", 0, 0, "Code 128"));

                //DeviceLibrary.Common.codeProcess(labelParam, out string debugmsg);
                //Console.WriteLine(debugmsg);
                _ = 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();
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            catch (Exception ex)
            {
                LogUtil.error(ex.Message);
                throw new Exception(ex.Message); ;
            }         
        }

        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

    }
}