MeasureSettingPanel.xaml.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using ComPDFKitViewer.PdfViewer;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
  17. namespace Compdfkit_Tools.Measure
  18. {
  19. /// <summary>
  20. /// MeasureSettingPanel.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class MeasureSettingPanel : UserControl
  23. {
  24. public event EventHandler CancelEvent;
  25. public event EventHandler DoneEvent;
  26. public bool ReturnToInfoPanel { get; set; }
  27. public MeasureSettingPanel()
  28. {
  29. InitializeComponent();
  30. }
  31. private void ButtonCancel_Click(object sender, RoutedEventArgs e)
  32. {
  33. CancelEvent?.Invoke(this, e);
  34. ReturnToInfoPanel = false;
  35. }
  36. private void ButtonDone_Click(object sender, RoutedEventArgs e)
  37. {
  38. SaveMeasureSetting();
  39. DoneEvent?.Invoke(this, e);
  40. ReturnToInfoPanel = false;
  41. }
  42. private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  43. {
  44. List<Key> allowKeys = new List<Key>()
  45. {
  46. Key.Delete, Key.Back, Key.Enter, Key.NumPad0, Key.NumPad1, Key.NumPad2, Key.NumPad3,
  47. Key.NumPad4, Key.NumPad5, Key.NumPad6, Key.NumPad7, Key.NumPad8, Key.NumPad9, Key.D0,
  48. Key.D1, Key.D2, Key.D3, Key.D4, Key.D5, Key.D6, Key.D7, Key.D8, Key.D9, Key.Left, Key.Right,
  49. Key.OemPeriod,Key.Decimal
  50. };
  51. if (allowKeys.Contains(e.Key) == false)
  52. {
  53. e.Handled = true;
  54. }
  55. }
  56. private void TextBox_CanExecute(object sender, CanExecuteRoutedEventArgs e)
  57. {
  58. try
  59. {
  60. if (e.Command == ApplicationCommands.Paste && Clipboard.ContainsText())
  61. {
  62. string checkText = Clipboard.GetText();
  63. if (int.TryParse(checkText, out int value))
  64. {
  65. e.CanExecute = true;
  66. }
  67. e.Handled = true;
  68. }
  69. }
  70. catch (Exception ex)
  71. {
  72. }
  73. }
  74. public void BindMeasureSetting()
  75. {
  76. RulerBaseText.Text=MeasureSetting.RulerBase.ToString();
  77. RulerTranslateText.Text=MeasureSetting.RulerTranslate.ToString();
  78. RulerTranslateCombo.SelectedIndex = -1;
  79. RulerBaseUnitCombo.SelectedIndex = -1;
  80. PrecisionBox.SelectedIndex = -1;
  81. if (MeasureSetting.RulerBaseUnit=="in")
  82. {
  83. RulerBaseUnitCombo.SelectedIndex = 0;
  84. }
  85. if (MeasureSetting.RulerBaseUnit == "cm")
  86. {
  87. RulerBaseUnitCombo.SelectedIndex = 1;
  88. }
  89. if (MeasureSetting.RulerBaseUnit == "mm")
  90. {
  91. RulerBaseUnitCombo.SelectedIndex = 2;
  92. }
  93. for(int i=0;i< RulerTranslateCombo.Items.Count;i++)
  94. {
  95. ComboBoxItem checkItem= RulerTranslateCombo.Items[i] as ComboBoxItem;
  96. if(checkItem!=null && checkItem.Content.ToString()==MeasureSetting.RulerTranslateUnit.ToString())
  97. {
  98. RulerTranslateCombo.SelectedIndex = i;
  99. }
  100. }
  101. for (int i = 0; i < PrecisionBox.Items.Count; i++)
  102. {
  103. ComboBoxItem checkItem = PrecisionBox.Items[i] as ComboBoxItem;
  104. if (checkItem != null && checkItem.Content.ToString() == MeasureSetting.Precision.ToString())
  105. {
  106. PrecisionBox.SelectedIndex = i;
  107. }
  108. }
  109. }
  110. private void SaveMeasureSetting()
  111. {
  112. if(double.TryParse(RulerBaseText.Text,out double ruleBase))
  113. {
  114. MeasureSetting.RulerBase = ruleBase;
  115. }
  116. if(RulerBaseUnitCombo.SelectedItem!=null)
  117. {
  118. ComboBoxItem checkItem = RulerBaseUnitCombo.SelectedItem as ComboBoxItem;
  119. MeasureSetting.RulerBaseUnit= checkItem.Content.ToString();
  120. }
  121. if (double.TryParse(RulerTranslateText.Text, out double ruletranBase))
  122. {
  123. MeasureSetting.RulerTranslate = ruletranBase;
  124. }
  125. if (RulerTranslateCombo.SelectedItem != null)
  126. {
  127. ComboBoxItem checkItem = RulerTranslateCombo.SelectedItem as ComboBoxItem;
  128. MeasureSetting.RulerTranslateUnit = checkItem.Content.ToString();
  129. }
  130. if (PrecisionBox.SelectedValue!=null)
  131. {
  132. ComboBoxItem checkItem = PrecisionBox.SelectedValue as ComboBoxItem;
  133. if (double.TryParse(checkItem.Content.ToString(), out double precision))
  134. {
  135. MeasureSetting.Precision = precision;
  136. }
  137. }
  138. }
  139. }
  140. }