Program.cs 5.8 KB
using ConfigHelper;
using log4net.Config;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TheMachine
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //var x = new byte[] { 0x02, 0x41, 0x62, 0x2B, 0x30, 0x30, 0x31, 0x2E, 0x30, 0x30, 0x30, 0x32, 0x37, 0x03 };
            //01仪表发送的毛重消息为02 41 62 2B 30 30 31 2E 30 30 30 32 37 03。
            //2B 30 30 31 2E 30 30 30位具体的毛重数据,转换为ASCII码为 +001.000;
            //32H为校验码高位,37H为校验码低位,03H为结束符;

            //var datatxt = System.Text.Encoding.ASCII.GetString(x);
            //datatxt = datatxt.Substring(3, 8);
            //double.TryParse(datatxt, out double value);
            //Console.WriteLine(value);
            /*
            int ff = 0;
            var f = @"D:\rick\vs\SO1069MIMO_PLUS\Image_20220511153145272.bmp";
            var bmp = new Bitmap(f);
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            var b = new Bitmap(bmp.Width/2, bmp.Height/2,bmp.PixelFormat);
            using (var g = Graphics.FromImage(b))
            {
                g.InterpolationMode = InterpolationMode.Low;
                g.DrawImage(bmp, 0, 0, b.Width, b.Height);
            }

            var bd = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, b.PixelFormat);
            var Pixels = new byte[b.Width * b.Height * 3];

            // Copy data from pointer to array
            Marshal.Copy(bd.Scan0, Pixels, 0, Pixels.Length);

            for (int x = 0; x < b.Width * b.Height * 3; x += 3)
            {
                //var cr = (int)Marshal.ReadByte(bd.Scan0, x+2);
                //var cg = (int)Marshal.ReadByte(bd.Scan0, x + 1);
                //var cb = (int)Marshal.ReadByte(bd.Scan0, x);
                var cr = (int)Pixels[x + 2];
                var cg = (int)Pixels[x + 1];
                var cb = (int)Pixels[x];
                var h = ColorHelper.RgbToHsv(new ColorRGB(cr, cg, cb));
                if (h.H >= 59 && h.H <= 61 && h.V >= 90 && h.S >= 90)
                {
                    ff++;
                    //Marshal.WriteByte(bd.Scan0, x,0);
                    //Marshal.WriteByte(bd.Scan0, x+1, 0);
                    //Marshal.WriteByte(bd.Scan0, x+2, 255);
                }
            }
            b.UnlockBits(bd);
            stopwatch.Stop();
            var t = stopwatch.ElapsedMilliseconds / 1000d;
            b.Save(@"D:\rick\vs\SO1069MIMO_PLUS\11-57-09-156_11.bmp");
            */
            if (ConfigHelper.Config.Get("Func_CheckReelLocInFix", false))
                DeviceLibrary.VisionHelper.CheckAndRunServer();

            _ = 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;
            }
            Application.ThreadException += Application_ThreadException;
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            Environment.CurrentDirectory = Application.StartupPath;
            XmlConfigurator.Configure();
            Config.LoadMyConfig(new Setting_Init().GetType());
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            LogUtil.error(e.ToString());
        }

        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            LogUtil.error(e.ToString());
        }



        #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

    }
}