using CaeGlobals; using CommandLine; using System; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; // ReSharper disable ArrangeTypeMemberModifiers // ReSharper disable UnusedMember.Local namespace CPSO { static class Program { [DllImport("kernel32.dll")] private static extern IntPtr GetConsoleWindow(); // [DllImport("kernel32.dll")] static extern bool AttachConsole(int dwProcessId); private const int ATTACH_PARENT_PROCESS = -1; // [DllImport("kernel32.dll")] static extern bool FreeConsole(); [DllImport("user32.dll")] private static extern bool SetProcessDPIAware(); // /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { // DPI // if (Environment.OSVersion.Version.Major >= 6) SetProcessDPIAware(); Console.WriteLine(""); // 设置语言相关 SetCultureAndLanguage(); // Parse if (args != null && args.Length == 1 && File.Exists(args[0])) { args = new[] { "-f", args[0] }; } Debug.Assert(args != null, nameof(args) + " != null"); for (int i = 0; i < args.Length; i++) { if (args[i] == "-r0" || args[i] == "-r1" || args[i] == "-r2" || args[i] == "-r3") { args[i] = "-" + args[i]; } } var parserResult = Parser.Default.ParseArguments(args); // 主循环 if (parserResult.Value != null) { Run(parserResult.Value); } // a process remains running afer application exits Process.GetCurrentProcess().Kill(); } private static void SetCultureAndLanguage() { System.Globalization.CultureInfo ci = (System.Globalization.CultureInfo)System.Globalization.CultureInfo.InvariantCulture.Clone(); ci.NumberFormat.NumberGroupSeparator = ""; // 设置Culture Thread.CurrentThread.CurrentCulture = ci; // This thread System.Globalization.CultureInfo.DefaultThreadCurrentCulture = ci; // All feature threads // 设置UI Culture Thread.CurrentThread.CurrentUICulture = ci; System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = ci; // Set MessageBoxButtons to English defaults /* MessageBoxManager.OK = "OK"; MessageBoxManager.Cancel = "Cancel"; MessageBoxManager.Abort = "Abort"; MessageBoxManager.Retry = "Retry"; MessageBoxManager.Ignore = "Ignore"; MessageBoxManager.Yes = "Yes"; MessageBoxManager.No = "No"; */ // 本地化消息框 MessageBoxManager.OK = "确定"; MessageBoxManager.Cancel = "取消"; MessageBoxManager.Abort = "中止"; MessageBoxManager.Retry = "重试"; MessageBoxManager.Ignore = "忽略"; MessageBoxManager.Yes = "是"; MessageBoxManager.No = "否"; MessageBoxManager.Register(); } private static void Run(CommandLineOptions cmdOptions) { try { // Show values string values = CommandLineOptions.GetValuesAsString(cmdOptions); if (values != null) { Console.WriteLine(values); } // Check for errors var cmdError = CommandLineOptions.CheckForErrors(cmdOptions); if (cmdError != null) { throw new CaeException(cmdError); } // Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); // 主窗口 using (FrmMain mainForm = new FrmMain(cmdOptions)) { if (cmdOptions.ShowGui == "No") // must be here { mainForm.WindowState = FormWindowState.Minimized; mainForm.ShowInTaskbar = false; } Application.ThreadException += Application_ThreadException; Application.Run(mainForm); Console.WriteLine(@"----------Finished------------"); Console.WriteLine(@"Process finished successfully."); Console.WriteLine(""); } } catch (Exception ex) { FinishedWithException(ex); } } private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) { FinishedWithException(e.Exception); } private static void FinishedWithException(Exception ex) { Console.WriteLine(@"----------Error---------------"); Console.WriteLine(ex.Message); Console.WriteLine(@"----------Finished------------"); Console.WriteLine(@"Process finished with errors."); Console.WriteLine(""); // a process remains running afer application exits Process.GetCurrentProcess().Kill(); } private static bool IsWindowsApplication() { return GetConsoleWindow() == IntPtr.Zero; } } }