CPDFScalingUI.xaml.cs 4.3 KB

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