using Microsoft.Xaml.Behaviors; using PDF_Office.Model; using PDF_Office.CustomControl; using Prism.Commands; using Prism.Mvvm; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Linq; using System.Printing; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter { public class HomePagePrinterPaperSettingsDialogViewModel : BindableBase, IDialogAware { public string Title => "PaperSettings"; public event Action RequestClose; private PrintQueue printQueue; string Unicode = null; private Visibility _isPageSizeStackPanelsVisible = Visibility.Hidden; public Visibility IsPageSizeStackPanelsVisible { get { return _isPageSizeStackPanelsVisible; } set { SetProperty(ref _isPageSizeStackPanelsVisible, value); } } /// /// 是否自定义纸张 /// private bool _isCustomSize; public bool IsCustomSize { get => _isCustomSize; set { if (value) { IsPageSizeStackPanelsVisible = Visibility.Visible; } else { IsPageSizeStackPanelsVisible = Visibility.Hidden; } _isCustomSize = value; } } private int? _customPaperSizeWidth; /// /// 自定义页面尺寸:宽度 /// public int? CustomPaperSizeWidth { get => _customPaperSizeWidth; set => _customPaperSizeWidth = value; } private int? _customPaperSizeHeight; /// /// 自定义页面尺寸:高度 /// public int? CustomPaperSizeHeight { get => _customPaperSizeHeight; set => _customPaperSizeHeight = value; } /// /// 当前选中的纸张尺寸 /// public PageMediaSize currentPageMediaSize; /// /// 打印机支持的纸张 /// public List PageMediaSizeList = new List(); /// /// 纸张大小列表 /// private List _pageMediaSizeNameList = new List(); public List PageMediaSizeNameList { get => _pageMediaSizeNameList; set => _pageMediaSizeNameList = value; } private List _margin = new List(); public List Margin { get => _margin; set => _margin = value; } private int _pageMediaSizeIndex = 0; public int PageMediaSizeIndex { get=> _pageMediaSizeIndex; set => SetProperty(ref _pageMediaSizeIndex, value); } /// /// 页边距 /// private int _marginTop; public int MarginTop { get => _marginTop; set => SetProperty(ref _marginTop, value); } private int _marginBottom; public int MarginBottom { get => _marginBottom; set => SetProperty(ref _marginBottom, value); } private int _marginLeft; public int MarginLeft { get => _marginLeft; set => SetProperty(ref _marginLeft, value); } private int _marginRight; public int MarginRight { get => _marginRight; set => SetProperty(ref _marginRight, value); } public DelegateCommand CancelCommand { get; set; } public DelegateCommand SetPaperSizeCommand { get; set; } public DelegateCommand ConfirmPaperSettingsCommand { get; set; } HomePagePrinterPaperSettingsDialogViewModel() { CancelCommand = new DelegateCommand(Cancel); SetPaperSizeCommand = new DelegateCommand(SetPaperSize); ConfirmPaperSettingsCommand = new DelegateCommand(ConfirmPaperSettings); } private void InitPaperSizeList() { PageMediaSizeList.Clear(); PageMediaSizeNameList.Clear(); foreach (PageMediaSize size in printQueue.GetPrintCapabilities().PageMediaSizeCapability) { if (size == null || size.PageMediaSizeName == null) { continue; } PageMediaSizeList.Add(size); PageMediaSizeNameList.Add(size.PageMediaSizeName.ToString()); } PageMediaSizeNameList.Add("自定义"); if (PageMediaSizeNameList.Contains("ISOA4")) { PageMediaSizeIndex = PageMediaSizeNameList.IndexOf("ISOA4"); } else { PageMediaSizeIndex = 0; } currentPageMediaSize = PageMediaSizeList[PageMediaSizeIndex]; } public void SetPaperSize(object e) { var cmb = e as ComboBox; if (cmb.SelectedIndex < PageMediaSizeNameList.Count - 1) { IsCustomSize = false; currentPageMediaSize = PageMediaSizeList[PageMediaSizeNameList.IndexOf(cmb.SelectedItem.ToString())]; } else { IsCustomSize = true; } } public void ConfirmPaperSettings () { if (IsCustomSize) { if (CustomPaperSizeHeight!=null&&CustomPaperSizeWidth!=null) { currentPageMediaSize = new PageMediaSize((double)CustomPaperSizeWidth/25.4*96, (double)CustomPaperSizeHeight / 25.4 * 96); } else { MessageBoxEx.Show("纸张尺寸缺失"); return; } } Margin.Add(MarginLeft); Margin.Add(MarginTop); Margin.Add(MarginRight); Margin.Add(MarginBottom); var dialogResult = new DialogResult(ButtonResult.OK); dialogResult.Parameters.Add("PageMediaSize", currentPageMediaSize); dialogResult.Parameters.Add("MarginLeft", MarginLeft); dialogResult.Parameters.Add("MarginTop", MarginTop); dialogResult.Parameters.Add("MarginRight", MarginRight); dialogResult.Parameters.Add("MarginBottom", MarginBottom); RequestClose.Invoke(dialogResult); } public void Cancel() { var dialogResult = new DialogResult(ButtonResult.Cancel); RequestClose.Invoke(dialogResult); } public bool CanCloseDialog() { return true; } public void OnDialogClosed() { } public void OnDialogOpened(IDialogParameters parameters) { parameters.TryGetValue(ParameterNames.PrintQueue, out printQueue); parameters.TryGetValue("Unicode", out Unicode); InitPaperSizeList(); } } }