CPDFScalingUI.xaml.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using ComPDFKitViewer.PdfViewer;
  2. using System;
  3. using System.ComponentModel;
  4. using System.Text.RegularExpressions;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Input;
  8. using Compdfkit_Tools.Helper;
  9. namespace Compdfkit_Tools.PDFControlUI
  10. {
  11. public partial class CPDFScalingUI : UserControl, INotifyPropertyChanged
  12. {
  13. public CPDFViewer PDFView { get; set; }
  14. private string regixString = "[^0-9]+";
  15. private int _scale = 100;
  16. public int Scale
  17. {
  18. get { return _scale; }
  19. set
  20. {
  21. if (_scale != value)
  22. {
  23. _scale = value;
  24. OnPropertyChanged(nameof(Scale));
  25. }
  26. }
  27. }
  28. private int[] zoomLevelList = { 10, 25, 50, 100, 150, 200, 300, 400, 500, 1000 };
  29. public event EventHandler<string> SetScaleEvent;
  30. public event EventHandler<string> SetPresetScaleEvent;
  31. public event EventHandler ScaleIncreaseEvent;
  32. public event EventHandler ScaleDecreaseEvent;
  33. public CPDFScalingUI()
  34. {
  35. InitializeComponent();
  36. DataContext = this;
  37. BindZoomLevel();
  38. }
  39. private void DropDownNumberBoxControl_SetPresetEvent(object sender, string e)
  40. {
  41. SetPresetScaleEvent?.Invoke(this, e);
  42. }
  43. private void BindZoomLevel()
  44. {
  45. ZoomComboBox.Items.Add(new ComboBoxItem() { Content = LanguageHelper.CommonManager.GetString("Zoom_Real") });
  46. ZoomComboBox.Items.Add(new ComboBoxItem() { Content = LanguageHelper.CommonManager.GetString("Zoom_FitWidth") });
  47. ZoomComboBox.Items.Add(new ComboBoxItem() { Content = LanguageHelper.CommonManager.GetString("Zoom_FitPage")});
  48. foreach (double zoomLevel in zoomLevelList)
  49. {
  50. ComboBoxItem zoomItem = new ComboBoxItem();
  51. zoomItem.Content = zoomLevel;
  52. ZoomComboBox.Items.Add(zoomItem);
  53. }
  54. }
  55. private void ZoomComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  56. {
  57. ComboBoxItem selectItem = ZoomComboBox.SelectedItem as ComboBoxItem;
  58. SetPresetScaleEvent?.Invoke(this, selectItem.Content.ToString());
  59. }
  60. public void SetZoomTextBoxText(string value)
  61. {
  62. ZoomTextBox.Text = value;
  63. }
  64. private void DropDownNumberBoxControl_InputEnterEvent(object sender, string e)
  65. {
  66. SetScaleEvent?.Invoke(this,e);
  67. }
  68. private void ScaleDecreaseButton_Click(object sender, RoutedEventArgs e)
  69. {
  70. ScaleDecreaseEvent?.Invoke(this,e);
  71. }
  72. private void ScaleIncreaseButton_Click(object sender, RoutedEventArgs e)
  73. {
  74. ScaleIncreaseEvent?.Invoke(this,e);
  75. }
  76. public event PropertyChangedEventHandler PropertyChanged;
  77. protected void OnPropertyChanged(string propertyName)
  78. {
  79. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  80. }
  81. private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  82. {
  83. e.Handled = new Regex(regixString).IsMatch(e.Text);
  84. }
  85. private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  86. {
  87. if (e.Key == Key.Enter)
  88. {
  89. SetScaleEvent?.Invoke(sender, ZoomTextBox.Text);
  90. }
  91. if ((e.KeyStates == Keyboard.GetKeyStates(Key.LeftCtrl) || e.KeyStates == Keyboard.GetKeyStates(Key.RightCtrl)) && e.KeyStates == Keyboard.GetKeyStates(Key.V))
  92. e.Handled = true;
  93. else
  94. e.Handled = false;
  95. }
  96. private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  97. {
  98. if (!string.IsNullOrEmpty(ZoomTextBox.Text))
  99. {
  100. int num;
  101. int.TryParse(ZoomTextBox.Text, out num);
  102. if (num > 1000)
  103. {
  104. ZoomTextBox.Text = 1000.ToString();
  105. }
  106. if (num < 1)
  107. {
  108. ZoomTextBox.Text = 1.ToString();
  109. }
  110. }
  111. }
  112. private void ZoomTextBox_LostFocus(object sender, RoutedEventArgs e)
  113. {
  114. SetScaleEvent?.Invoke(sender, ZoomTextBox.Text);
  115. }
  116. }
  117. }