EditStandValueDlg.xaml.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using MeterVision.Config;
  2. using MeterVision.model;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Shapes;
  17. namespace MeterVision.Dlg
  18. {
  19. /// <summary>
  20. /// EditStandValueDlg.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class EditStandValueDlg : Window
  23. {
  24. public StandValueModel mStandValueModel { get; private set; }
  25. private string CurImagePath = string.Empty;
  26. public event Action<StandValueModel> OnEditStandValue;
  27. public EditStandValueDlg(StandValueModel standValueModel)
  28. {
  29. InitializeComponent();
  30. int width = CfginiItem.GetConfigItem().DialogStandSize_Width;
  31. int height = CfginiItem.GetConfigItem().DialogStandSize_Height;
  32. if (width != 0 && height != 0)
  33. {
  34. this.Width = width;
  35. this.Height = height;
  36. }
  37. mStandValueModel = standValueModel;
  38. InitUIValue(standValueModel);
  39. CurImagePath = "";
  40. if (standValueModel.ModelType == 2)
  41. {
  42. CurImagePath = mStandValueModel.SrcImage; //让界面线显示结果图片
  43. }
  44. LoadImage();
  45. }
  46. private void InitUIValue(StandValueModel standValueModel)
  47. {
  48. //ModelType = 1 模板标准值修改, = 2任务标准值修改;
  49. pnlLeft.Visibility = pnlRight.Visibility = pnlFinalValue.Visibility =
  50. (standValueModel.ModelType == 1 ? Visibility.Collapsed : Visibility.Visible);
  51. txtFinalValue.Text = standValueModel.FinalValue;
  52. txtStandValue.Text = standValueModel.StandValue;
  53. }
  54. private void LoadImage()
  55. {
  56. if (CurImagePath.Equals(mStandValueModel.SrcImage))
  57. {
  58. ucImage.ImageSource = mStandValueModel.DstImage;
  59. ucImage.ImageName = "结果图片";
  60. CurImagePath = mStandValueModel.DstImage;
  61. }
  62. else
  63. {
  64. ucImage.ImageSource = mStandValueModel.SrcImage;
  65. ucImage.ImageName = "原始图片";
  66. CurImagePath = mStandValueModel.SrcImage;
  67. }
  68. }
  69. private void BtnRight_Click(object sender, RoutedEventArgs e)
  70. {
  71. LoadImage();
  72. }
  73. private void BtnLeft_Click(object sender, RoutedEventArgs e)
  74. {
  75. LoadImage();
  76. }
  77. private void BtnCopy_Click(object sender, RoutedEventArgs e)
  78. {
  79. txtStandValue.Text = txtFinalValue.Text;
  80. }
  81. private void BtnOk_Click(object sender, RoutedEventArgs e)
  82. {
  83. string standValue = txtStandValue.Text.Trim();
  84. if (string.IsNullOrWhiteSpace(standValue))
  85. {
  86. MessageBox.Show("请输入标准值。", "提示",
  87. MessageBoxButton.OK, MessageBoxImage.Information);
  88. return;
  89. }
  90. if (!IsPositiveNumber(standValue))
  91. {
  92. MessageBox.Show("请输入正确的值。", "提示",
  93. MessageBoxButton.OK, MessageBoxImage.Information);
  94. return;
  95. }
  96. mStandValueModel.StandValue = standValue;
  97. //DialogResult = true;
  98. this.Close();
  99. OnEditStandValue?.Invoke(mStandValueModel);
  100. }
  101. private void BtnClose_Click(object sender, RoutedEventArgs e)
  102. {
  103. //DialogResult = false;
  104. this.Close();
  105. }
  106. private void TxtStandValue_PreviewTextInput(object sender, TextCompositionEventArgs e)
  107. {
  108. //string input = e.Text;
  109. //// 检查是否输入的是有效的数字或小数点
  110. //// 1. 只允许数字、英文小数点
  111. //// 2. 禁止输入任何中文字符
  112. //if (Regex.IsMatch(input, @"[^0-9.]") || (input == "." && txtStandValue.Text.Contains(".")))
  113. //{
  114. // e.Handled = true; // 阻止非法字符
  115. //}
  116. }
  117. private void TxtStandValue_TextChanged(object sender, TextChangedEventArgs e)
  118. {
  119. //string text = txtStandValue.Text;
  120. //// 如果输入为空或者不是合法的正数或小数
  121. //if (!string.IsNullOrEmpty(text) && !IsValidNumber(text))
  122. //{
  123. // // 可以根据需求重置文本或者给出提示
  124. // txtStandValue.Text = "0"; // 默认值为 0
  125. // txtStandValue.SelectionStart = txtStandValue.Text.Length; // 保证光标位置在文本末尾
  126. //}
  127. }
  128. private bool IsValidNumber(string text)
  129. {
  130. // 正则表达式:仅允许正整数或一个小数点的数字
  131. Regex regex = new Regex(@"^\d+(\.\d+)?$");
  132. return regex.IsMatch(text);
  133. }
  134. public bool IsPositiveNumber(string input)
  135. {
  136. // 使用 double.TryParse 判断是否为数字
  137. if (double.TryParse(input, out double result))
  138. {
  139. // 判断是否为正数
  140. return result >= 0;
  141. }
  142. return false;
  143. }
  144. private void Window_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
  145. {
  146. if (Keyboard.Modifiers == ModifierKeys.Control)
  147. {
  148. //if (e.Delta > 0)
  149. //{
  150. // ZoomFactor *= 1.1; // Zoom in
  151. //}
  152. //else
  153. //{
  154. // ZoomFactor /= 1.1; // Zoom out
  155. //}
  156. //// Optionally, center the zoom around the mouse cursor
  157. //Point mousePosition = e.GetPosition(imgViewer);
  158. //double prevZoomFactor = 1 / ZoomFactor;
  159. //double offsetX = (mousePosition.X * ZoomFactor - mousePosition.X * prevZoomFactor) / prevZoomFactor;
  160. //double offsetY = (mousePosition.Y * ZoomFactor - mousePosition.Y * prevZoomFactor) / prevZoomFactor;
  161. //scrollViewer.ScrollToHorizontalOffset(scrollViewer.HorizontalOffset + offsetX);
  162. //scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + offsetY);
  163. ucImage.ImageZoom(e.Delta > 0,e);
  164. }
  165. }
  166. private void Window_Closed(object sender, EventArgs e)
  167. {
  168. if (WindowState != WindowState.Maximized)
  169. {
  170. int width = (int)this.Width;
  171. int height = (int)this.Height;
  172. string dialogSize = $"{width},{height}";
  173. CfginiItem.GetConfigItem().DialogStandSize = dialogSize;
  174. }
  175. }
  176. //----------------------------------------------------------------
  177. }
  178. }