123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- using Microsoft.Xaml.Behaviors;
- using PDF_Master.Model;
- using PDF_Master.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_Master.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter
- {
- public class HomePagePrinterPaperSettingsDialogViewModel : BindableBase, IDialogAware
- {
- public string Title => "PaperSettings";
- public event Action<IDialogResult> RequestClose;
- private PrintQueue printQueue;
- string Unicode = null;
- private Visibility _isPageSizeStackPanelsVisible = Visibility.Hidden;
- public Visibility IsPageSizeStackPanelsVisible
- {
- get { return _isPageSizeStackPanelsVisible; }
- set { SetProperty(ref _isPageSizeStackPanelsVisible, value); }
- }
- /// <summary>
- /// 是否自定义纸张
- /// </summary>
- private bool _isCustomSize;
- public bool IsCustomSize
- {
- get => _isCustomSize;
- set
- {
- if (value)
- {
- IsPageSizeStackPanelsVisible = Visibility.Visible;
- }
- else
- {
- IsPageSizeStackPanelsVisible = Visibility.Hidden;
- }
- _isCustomSize = value;
- }
- }
- private int? _customPaperSizeWidth;
- /// <summary>
- /// 自定义页面尺寸:宽度
- /// </summary>
- public int? CustomPaperSizeWidth
- {
- get => _customPaperSizeWidth;
- set => _customPaperSizeWidth = value;
- }
- private int? _customPaperSizeHeight;
- /// <summary>
- /// 自定义页面尺寸:高度
- /// </summary>
- public int? CustomPaperSizeHeight
- {
- get => _customPaperSizeHeight;
- set => _customPaperSizeHeight = value;
- }
- /// <summary>
- /// 当前选中的纸张尺寸
- /// </summary>
- public PageMediaSize currentPageMediaSize;
- /// <summary>
- /// 打印机支持的纸张
- /// </summary>
- public List<PageMediaSize> PageMediaSizeList = new List<PageMediaSize>();
- /// <summary>
- /// 纸张大小列表
- /// </summary>
- private List<string> _pageMediaSizeNameList = new List<string>();
- public List<string> PageMediaSizeNameList
- {
- get => _pageMediaSizeNameList;
- set => _pageMediaSizeNameList = value;
- }
- private List<int> _margin = new List<int>();
- public List<int> Margin
- {
- get => _margin;
- set => _margin = value;
- }
- private int _pageMediaSizeIndex = 0;
- public int PageMediaSizeIndex
- {
- get=> _pageMediaSizeIndex;
- set => SetProperty(ref _pageMediaSizeIndex, value);
- }
- /// <summary>
- /// 页边距
- /// </summary>
- 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<object> SetPaperSizeCommand { get; set; }
- public DelegateCommand ConfirmPaperSettingsCommand { get; set; }
- HomePagePrinterPaperSettingsDialogViewModel()
- {
- CancelCommand = new DelegateCommand(Cancel);
- SetPaperSizeCommand = new DelegateCommand<object>(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<PrintQueue>(ParameterNames.PrintQueue, out printQueue);
- parameters.TryGetValue<string>("Unicode", out Unicode);
- InitPaperSizeList();
- }
- }
- }
|