CPDFScalingUI.xaml.cs 4.2 KB

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