using ComPDFKit.PDFAnnotation; using ComPDFKitViewer; using ComPDFKitViewer.AnnotEvent; using ComPDFKitViewer.PdfViewer; using PDF_Master.CustomControl.CompositeControl; using PDF_Master.Helper; using PDF_Master.Model; using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; namespace PDF_Master.ViewModels.Form { public class ButtonPropertyViewModel : FormBaseVM, INavigationAware { #region Command变量 public DelegateCommand FieldNameTextChangedCommand { get; set; } public DelegateCommand ToolTipTextChangedCommand { get; set; } //外观 public DelegateCommand ResetColorCommand { get; set; } public DelegateCommand ResetColorCheckedBtnCommand { get; set; } public DelegateCommand FormContentTextChangedCommand { get; set; } public DelegateCommand LineStyleCommand { get; set; } #endregion #region 变量 private CPDFViewer PDFViewer; private WidgetPushButtonArgs pushButtonArgs; private IDialogService dialogs; public event EventHandler SelectResetColorBtnHandler; public List FontFamilyItems { get; private set; } public List FontStyleItems { get; private set; } public List AglinmentItems { get; private set; } #endregion #region 初始化 public ButtonPropertyViewModel(IDialogService dialogService) { dialogs = dialogService; InitVariable(); InitCommand(); } private void InitVariable() { InitAllResetColor(); InitFontFamilyComboBox(); InitFontStyleComboBox(); InitAglinmentItemsComboBox(); } private void InitAllResetColor() { ResetColorOne = InitResetColor(Color.FromArgb(0xFF, 0x18, 0xA0, 0xFB), Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Color.FromArgb(0xFF, 0xff, 0xff, 0xff)); ResetColorTwo = InitResetColor(Colors.Transparent, Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Color.FromArgb(0xFF, 0xB9, 0xB9, 0xB9)); ResetColorThree = InitResetColor(Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF), Color.FromArgb(0xFF, 0x00, 0x00, 0x00)); ResetColorForth = InitResetColor(Colors.Transparent, Color.FromArgb(0xFF, 0x00, 0x00, 0x00), Color.FromArgb(0xFF, 0x38, 0xAC, 0xFF)); } private void InitFontFamilyComboBox() { FontFamilyItems = new List(); ComboDataItem item = new ComboDataItem("Courier", "Courier New"); FontFamilyItems.Add(item); item = new ComboDataItem("Helvetica", "Helvetica"); FontFamilyItems.Add(item); item = new ComboDataItem("Times-Roman", "Times New Roman"); FontFamilyItems.Add(item); } private void InitFontStyleComboBox() { FontStyleItems = new List(); ComboDataItem item = new ComboDataItem("Regular", "Regular"); FontStyleItems.Add(item); item = new ComboDataItem("Bold", "Bold"); FontStyleItems.Add(item); item = new ComboDataItem("Italic", "Italic"); FontStyleItems.Add(item); item = new ComboDataItem("Bold Italic", "Bold Italic"); FontStyleItems.Add(item); } private void InitAglinmentItemsComboBox() { AglinmentItems = new List(); ComboDataItem item = new ComboDataItem("Left", "Left"); AglinmentItems.Add(item); item = new ComboDataItem("Center", "Center"); AglinmentItems.Add(item); item = new ComboDataItem("Right", "Right"); AglinmentItems.Add(item); } private void InitCommand() { //一般 FieldNameTextChangedCommand = new DelegateCommand(FieldNameTextChanged); ToolTipTextChangedCommand = new DelegateCommand(ToolTipTextChanged); //外观 ResetColorCheckedBtnCommand = new DelegateCommand(ResetColorCheckedBtn); ResetColorCommand = new DelegateCommand(ResetColorEvent); LineStyleCommand = new DelegateCommand(LineStyleBtnEvent); //选项 FormContentTextChangedCommand = new DelegateCommand(FormContentTextChanged); } #endregion #region 事件 private void FieldNameTextChanged(string obj) { if (string.IsNullOrEmpty(obj) == false && IsCurrentWidget == true) { FieldName = obj; } } private void ToolTipTextChanged(string obj) { if (string.IsNullOrEmpty(obj) == false && IsCurrentWidget == true) { ToolTipStr = obj; } } private void LineStyleBtnEvent(object obj) { if (obj != null) { switch ((string)obj) { case "Solid": BorderStyle = ComPDFKit.PDFAnnotation.C_BORDER_STYLE.BS_SOLID; break; case "Dotted": BorderStyle = ComPDFKit.PDFAnnotation.C_BORDER_STYLE.BS_DASHDED; break; } } } private void FormContentTextChanged(string obj) { if (obj != null && IsCurrentWidget == true) { FormContent = obj; } } private void ResetColorCheckedBtn(object obj) { if (obj != null) { var str = obj as string; if (str != null) { switch (str) { case "One": BorderColor = ResetColorOne.BorderColor.Color; ContentColor = ResetColorOne.FontColor.Color; FillColor = ResetColorOne.FillColor.Color; break; case "Two": BorderColor = ResetColorTwo.BorderColor.Color; ContentColor = ResetColorTwo.FontColor.Color; FillColor = ResetColorTwo.FillColor.Color; break; case "Three": BorderColor = ResetColorThree.BorderColor.Color; ContentColor = ResetColorThree.FontColor.Color; FillColor = ResetColorThree.FillColor.Color; break; case "Forth": BorderColor = ResetColorForth.BorderColor.Color; ContentColor = ResetColorForth.FontColor.Color; FillColor = ResetColorForth.FillColor.Color; break; } } } } private void ResetColorEvent(object obj) { bool result = true; DialogParameters value = new DialogParameters(); value.Add(ParameterNames.PDFViewer, PDFViewer); dialogs.ShowDialog(DialogNames.EditPresetColorsDialog, value, e => { if (e.Result != ButtonResult.OK) { result = false; } EditPresetColorsDialogViewModel DialogVM = e.Parameters.GetValue(ParameterNames.DataModel); if (DialogVM != null) { } }); if (!result) { return; } } #endregion #region 外部XAML触发事件 private void UpdataSelectResetColorBtn() { int result = 0; if (UpdataSelectResetColor(ResetColorOne)) { result = 1; } else if (UpdataSelectResetColor(ResetColorTwo)) { result = 2; } else if (UpdataSelectResetColor(ResetColorThree)) { result = 3; } else if (UpdataSelectResetColor(ResetColorForth)) { result = 4; } SelectResetColorBtnHandler?.Invoke(null, result); } private bool UpdataSelectResetColor(ResetColor reset) { if (reset.FillColor.Color == FillColor && reset.FontColor.Color == ContentColor && reset.BorderColor.Color == BorderColor ) { return true; } return false; } #endregion #region Navegation public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { pushButtonArgs = null; isCreateWidget = false; IsCurrentWidget = false; PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler; } public void OnNavigatedTo(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue(ParameterNames.PDFViewer, out PDFViewer); navigationContext.Parameters.TryGetValue("WidgetArgs", out pushButtonArgs); navigationContext.Parameters.TryGetValue(ParameterNames.AnnotEvent, out AttribEvent); PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler; PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler; GetWidgeText(); UpdataSelectResetColorBtn(); } private void PDFViewer_AnnotEditHandler(object sender, List e) { if (e != null && e.Count > 0) { var widgeArgs = e[e.Count - 1].EditAnnotArgs as WidgetArgs; if (widgeArgs != null) { AnnotEditEvent editEvent = e[e.Count - 1]; if (editEvent.EditAction == ActionType.Modify) { SetSizeNoUpdateValue(widgeArgs.Width, widgeArgs.Height); } } } } private void GetWidgeText() { if (pushButtonArgs == null) { PDFViewer.SetMouseMode(MouseModes.FormEditTool); WidgetPushButtonArgs pushButtonArgs = new WidgetPushButtonArgs(); pushButtonArgs.BgColor = Colors.White; pushButtonArgs.FontName = "Courier New"; pushButtonArgs.FontSize = 12; pushButtonArgs.FontColor = Colors.Black; pushButtonArgs.LineColor = Colors.Black; pushButtonArgs.LineWidth = 2; pushButtonArgs.Width = 72; pushButtonArgs.Height = 20; var action = new Dictionary(); action.Add(ComPDFKit.PDFDocument.Action.C_ACTION_TYPE.ACTION_TYPE_URI, ""); pushButtonArgs.ActionDict = action; pushButtonArgs.FieldName = "PushButton"; pushButtonArgs.Text = "PushButton"; this.pushButtonArgs = pushButtonArgs; PDFViewer.SetToolParam(pushButtonArgs); isCreateWidget = true; } else { PDFViewer.SetToolParam(new AnnotHandlerEventArgs()); isCreateWidget = false; } GetProperty(); IsCurrentWidget = true; } private void GetProperty() { if (pushButtonArgs != null) { IsLocked = pushButtonArgs.Locked; FieldName = pushButtonArgs.FieldName; ToolTipStr = pushButtonArgs.Tooltip; IsReadOnly = pushButtonArgs.ReadOnly; FillColor = pushButtonArgs.BgColor; ContentColor = pushButtonArgs.FontColor; BorderColor = pushButtonArgs.LineColor; BorderThiness = pushButtonArgs.LineWidth; BorderStyle = pushButtonArgs.BorderStyle; string fontWeightStyleStr = ""; if (pushButtonArgs.IsItalic == false) { if (pushButtonArgs.IsBold == false) { fontWeightStyleStr = "Regular"; } else { fontWeightStyleStr = "Bold"; } } else { if (pushButtonArgs.IsBold == false) { fontWeightStyleStr = "Italic"; } else { fontWeightStyleStr = "Bold Italic"; } } FontWeightStyleItem = new ComboDataItem(fontWeightStyleStr); FontFamilyData = new ComboDataItem(pushButtonArgs.FontName); //避免BorderStyle跟上一个值相同,而没触发更改IsSolid属性 if (BorderStyle == C_BORDER_STYLE.BS_SOLID) IsSolid = true; else IsSolid = false; FontSizeData = new ComboDataItem(pushButtonArgs.FontSize); if (isCreateWidget == false) { HeightSize = pushButtonArgs.Height; WidthSize = pushButtonArgs.Width; } //FormContent = listBoxArgs.Text; //IsMultiLine = listBoxArgs.IsMultiLine; //IsScrollText = listBoxArgs.ScrollFlag; } } #endregion } }