using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Linq; using System.Windows.Forms; using Uestc.Auto6.Dso.Core; using Uestc.Auto6.Dso.Core.Tools; using Uni_Trend.MSO7000X.Common.Default; using Uni_Trend.MSO7000X.Common.Helper; using Uni_Trend.MSO7000X.UserControls; using Uni_Trend.MSO7000X.UserControls.Style; namespace Uestc.Auto6.Dso.U2 { public partial class ScreenSavePage : UserControl, IFileView, IStylize { private Boolean _ArgToCtrl; public ScreenSavePage() { InitializeComponent(); } private void LoadPicFormatList(IEnumerable formats) { CbxFileType.DataSource = formats.Select(x => new KeyValuePair(x.ToString() + $"({FilePrsnt.GetPicFileExtName(x)})", x)).ToList(); CbxFileType.DisplayMember = "Key"; CbxFileType.ValueMember = "Value"; CbxFileType.SelectedIndexChanged += (_, _) => { if (!_ArgToCtrl) { Presenter.PicFormat = (PicFormat)CbxFileType.SelectedIndex; } }; } [Browsable(false)] public StyleFlag StyleFlags { get; set; } = StyleFlag.None; [Description("是否风格化"), Browsable(true), DefaultValue(typeof(Boolean)), Category(Const.Category)] public Boolean StylizeFlag { get; set; } = false; protected new Boolean DesignMode { get { Boolean rtnflag = false; #if DEBUG rtnflag = DesignTimeHelper.InDesignMode(this); #endif return rtnflag; } } public FilePrsnt Presenter { get => (FilePrsnt)(ParentForm as IFileView).Presenter; set => (ParentForm as IFileView).Presenter = value; } IFilePrsnt IView.Presenter { get => Presenter; set => Presenter = (FilePrsnt)value; } /// /// 生成文件名 /// /// //private String MakeDefaultFileName() //{ // if (Presenter.IfAppendDatetime) // return Presenter.DefaultPrefixName + DateTime.Now.ToString("yyyyMMddHHmmss"); // else // { // var ext = Presenter.PicFormat.GetFileTypeDescription().Extension[0]; // var result = new System.IO.DirectoryInfo(Presenter.PicPath) // .GetFiles($"*.{ext}", System.IO.SearchOption.TopDirectoryOnly) // .Where(x => Regex.IsMatch(x.Name, $"^{Presenter.DefaultPrefixName}[0-9]{"{3}"}.{ext}$", RegexOptions.IgnoreCase)); // return Presenter.DefaultPrefixName + String.Format("{0:D3}", result.Count()); // } //} public override void Refresh() { base.Refresh(); UpdateView(); } public void UpdateView(Object presenter, String propertyName) { if (String.IsNullOrEmpty(propertyName)) { UpdateView(); return; } _ArgToCtrl = true; switch (propertyName) { case nameof(Presenter.PicPath): CbxPath.Text = Presenter.PicPath; break; case nameof(Presenter.PicRegion): RdoRegion.ChoosedButtonIndex = Presenter.PicRegion == PicArea.Application ? 0 : 1; break; case nameof(Presenter.IfAppendDatetime): ChkSuffix.Checked = Presenter.IfAppendDatetime; TbxFileName.Text = Presenter.FileName; break; case nameof(Presenter.FileName): TbxFileName.Text = Presenter.FileName; break; case nameof(Presenter.PicFormat): CbxFileType.SelectedIndex = (Int32)Presenter.PicFormat; break; case nameof(Presenter.PicColor): RdoColor.ChoosedButtonIndex = (Int32)Presenter.PicColor; break; } _ArgToCtrl = false; } protected void UpdateView() { if (!DesignMode) { _ArgToCtrl = true; RdoRegion.ChoosedButtonIndex = Presenter.PicRegion == PicArea.Application ? 0 : 1; CbxPath.Text = Presenter.PicPath; ChkSuffix.Checked = Presenter.IfAppendDatetime; TbxFileName.Text = Presenter.FileName; CbxFileType.SelectedIndex = (Int32)Presenter.PicFormat; RdoColor.ChoosedButtonIndex = (Int32)Presenter.PicColor; _ArgToCtrl = false; } } protected override void OnLoad(EventArgs e) { base.OnLoad(e); Presenter.FileName = Presenter.MakeDefaultFileName(Presenter.PicPath, FilePrsnt.GetPicFileExtName(Presenter.PicFormat)); LoadPicFormatList(Enum.GetValues()); UpdateView(); } private void RdoRegion_IndexChanged(Object sender, EventArgs e) { if (!_ArgToCtrl) { Presenter.PicRegion = RdoRegion.ChoosedButtonIndex == 0 ? PicArea.Application : PicArea.Window; } } private void RdoColor_IndexChanged(Object sender, EventArgs e) { if (!_ArgToCtrl) { Presenter.PicColor = (PicColor)RdoColor.ChoosedButtonIndex; } } private void BtnSelectPath_Click(Object sender, EventArgs e) { if (!_ArgToCtrl) { using var fbd = new DirectoryBrowserForm(Presenter.PicPath); fbd.StartPositionEx = fbd.CalculateWindowPosition(); if (fbd.ShowDialogByEvent() == DialogResult.Yes) { Presenter.PicPath = fbd.ChoosedDirPath; } } } private void BtnSave_Click(Object sender, EventArgs e) { var picpath = Presenter.PicPath; var filename = Presenter.FileName; var picformat = Presenter.PicFormat; var picregion = Presenter.PicRegion; var appenddatetime = Presenter.IfAppendDatetime; var piccolor = Presenter.PicColor; ParentForm.Visible = false; if (FilePrsnt.SaveImage(picpath, filename, picformat, picregion, appenddatetime, piccolor)) { WeakTip.Default.Write("File", MsgTipId.SavingSuccess, false, picpath); } else { WeakTip.Default.Write("File", MsgTipId.SavingFailed); } } private void ChkSuffix_CheckedChangedEvent(Object sender, EventArgs e) { if (!_ArgToCtrl) { Presenter.IfAppendDatetime = ChkSuffix.Checked; } } private void TbxFileName_TextChanged(Object sender, EventArgs e) { if (!_ArgToCtrl) { Presenter.FileName = TbxFileName.Text; } } private void BtnOpen_Click(Object sender, EventArgs e) { var filename = ExplorerExtension.GetLatestFile(Presenter.PicPath, FilePrsnt.GetPicFileExtName(Presenter.PicFormat)); if (String.IsNullOrEmpty(filename)) { ExplorerExtension.ExplorerDic(Presenter.PicPath); } else { ExplorerExtension.ExplorerFile(filename); } } private void BtnSaveAndOpen_Click(Object sender, EventArgs e) { var picpath = Presenter.PicPath; var filename = Presenter.FileName; var picformat = Presenter.PicFormat; var picregion = Presenter.PicRegion; var appenddatetime = Presenter.IfAppendDatetime; var piccolor = Presenter.PicColor; ParentForm.Visible = false; if (FilePrsnt.SaveImage(picpath, filename, picformat, picregion, appenddatetime, piccolor)) { var latestfilename = ExplorerExtension.GetLatestFile(picpath, FilePrsnt.GetPicFileExtName(picformat)); if (String.IsNullOrEmpty(latestfilename)) { ExplorerExtension.ExplorerDic(picpath); } else { ExplorerExtension.ExplorerFile(latestfilename); } } else { WeakTip.Default.Write("File", MsgTipId.SavingFailed); } } } }