public FormMain(ExeModeManager exeMode) { InitializeComponent(); //获得执行模式 ExeMode = exeMode; supportedExtensions = ImageHelper.GetSupportedFormatExtension(); }
static int Main(string[] args) { //AppConfig.config.SetPrinterConfigName("default"); //AppConfig.config.SetLanguage("en"); string lang = AppConfig.config.GetLanguage(); //获得运行模式 ExeModeManager exeMode = ExeModeManager.AnaliseModeFromArgs(args); if (exeMode.mode == EXE_MODE.MODE_END)//发生错误,直接返回 { return(-1); } if (exeMode.mode == EXE_MODE.MODE_FORMAT_CONVERT) { if (exeMode.pathTo == null) { exeMode.returnCode = ImageHelper.ConvertImageByPath(exeMode.pathFrom, exeMode.formatFrom, exeMode.formatTo); } else { exeMode.returnCode = ImageHelper.ConvertImageByPath(exeMode.pathFrom, exeMode.pathTo, exeMode.formatFrom, exeMode.formatTo); } } else { if (!lang.Equals("") && !lang.Equals("zh")) { //设置全局运行的语言设置,初始化窗口时会根据设置选择界面翻译 System.Threading.Thread.CurrentThread.CurrentUICulture = AppConfig.config.GetCultrueInfo(); } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FormMain(exeMode)); exeMode.returnCode = 0; } return(exeMode.returnCode); }
//分析得到运行模式相关数据 static public ExeModeManager AnaliseModeFromArgs(string[] args) { ExeModeManager exeMode = new ExeModeManager(); if (args.Length > 0) { //读取模式 EXE_MODE mode = EXE_MODE.MODE_NORMAL; try { mode = (EXE_MODE)int.Parse(args[0]); } catch (Exception) { mode = EXE_MODE.MODE_NORMAL; } finally { if (mode < EXE_MODE.MODE_NORMAL || mode >= EXE_MODE.MODE_END) { mode = EXE_MODE.MODE_NORMAL; } } //根据模式,进一步读取相关参数 switch ((EXE_MODE)int.Parse(args[0])) { case EXE_MODE.MODE_NORMAL: break; case EXE_MODE.MODE_INIT_PATH: { if (args.Length == 2) { exeMode.initPath = args[1]; } else { mode = EXE_MODE.MODE_NORMAL; } } break; case EXE_MODE.MODE_SINGLE_IMAGE: { if (args.Length == 2) { exeMode.singleImagePath = args[1]; if (!File.Exists(exeMode.singleImagePath)) { MessageBox.Show($"Invalid image path: {exeMode.singleImagePath}", Trans.tr("Tip"), MessageBoxButtons.OK, MessageBoxIcon.Information); mode = EXE_MODE.MODE_NORMAL; } } else { mode = EXE_MODE.MODE_NORMAL; } } break; case EXE_MODE.MODE_FORMAT_CONVERT: { if (args.Length == 4 || args.Length == 5) { exeMode.formatFrom = args[1]; exeMode.formatTo = args[2]; exeMode.pathFrom = args[3]; if (args.Length == 5) { exeMode.pathTo = args[4]; } else { exeMode.pathTo = null; } if (exeMode.formatFrom == "svg" && exeMode.formatTo == "emf") { if (!(File.Exists(exeMode.pathFrom) || (Directory.Exists(exeMode.pathFrom) && (exeMode.pathTo == null || Directory.Exists(exeMode.pathTo))) ) ) { MessageBox.Show($"invalid path:\n\n path1 [{exeMode.pathFrom}]\n\n path2 [{exeMode.pathTo}]\n\n when from-path is file, to-path can be directory or file \n\n when from-path is directory, to-path must be directory too", Trans.tr("Tip"), MessageBoxButtons.OK, MessageBoxIcon.Information); mode = EXE_MODE.MODE_END; //格式转换时发生错误,不能继续执行,置为 MODE_END 表示错误 } } else { MessageBox.Show($"unsupported convert [{exeMode.formatFrom}]->[{exeMode.formatTo}]\n\nSupported convert [svg]->[emf]", Trans.tr("Tip"), MessageBoxButtons.OK, MessageBoxIcon.Information); mode = EXE_MODE.MODE_END; //格式转换时发生错误,不能继续执行,置为 MODE_END 表示错误 } } else { mode = EXE_MODE.MODE_END; //格式转换时发生错误,不能继续执行,置为 MODE_END 表示错误 } } break; default: mode = EXE_MODE.MODE_NORMAL; break; } //执行模式 exeMode.mode = mode; } return(exeMode); }