using Newtonsoft.Json; using Newtonsoft.Json.Linq; using PipeGallery.Manage; using PipeGallery.Model; using PipeGallery.ViewModel; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace PipeGallery.View { /// <summary> /// SoundSpeedEditView.xaml 的交互逻辑 /// </summary> public partial class SoundSpeedEditView : UserControl { private int pipeMaterialId = 0; private bool btnFlag = false; private int selectedIndex = 0; public SoundSpeedEditView(int pipeMaterialId, bool btnFlag) { InitializeComponent(); this.btnClose.Click += btnReturn_Click; this.btnOK.Click += btnOK_Click; this.btnNum.Click += BtnNum_Click; this.btnCloseKb.Click += BtnCloseKb_Click; this.tbMin.GotFocus += TbMin_GotFocus; this.tbMax.GotFocus += TbMin_GotFocus; this.tbSpeed.GotFocus += TbMin_GotFocus; this.tbMin.LostFocus += TbMin_LostFocus; this.tbMax.LostFocus += TbMin_LostFocus; this.tbSpeed.LostFocus += TbMin_LostFocus; this.tbMin.PreviewTouchUp += TbMin_PreviewTouchUp; this.tbMin.PreviewMouseUp += TbMin_PreviewMouseUp; this.tbMax.PreviewTouchUp += TbMin_PreviewTouchUp; this.tbMax.PreviewMouseUp += TbMin_PreviewMouseUp; this.tbSpeed.PreviewTouchUp += TbMin_PreviewTouchUp; this.tbSpeed.PreviewMouseUp += TbMin_PreviewMouseUp; this.pipeMaterialId = pipeMaterialId; this.btnFlag = btnFlag; if (!btnFlag) { foreach (var soundspeedinfo in SoundSpeedEditViewModel.Instance.SoundSpeedInfos) { if (soundspeedinfo.IsSelected) { this.tbMin.Text = soundspeedinfo.MinRange; this.tbMax.Text = soundspeedinfo.MaxRange; this.tbSpeed.Text = soundspeedinfo.Speed; this.selectedIndex = soundspeedinfo.Order - 1; } } } } private void TbMin_PreviewMouseUp(object sender, MouseButtonEventArgs e) { HideInputPanel(); } private void TbMin_PreviewTouchUp(object sender, TouchEventArgs e) { HideInputPanel(); } private void TbMin_LostFocus(object sender, RoutedEventArgs e) { BtnCloseKb_Click(null, null); } private void TbMin_GotFocus(object sender, RoutedEventArgs e) { bdrContent.Margin = new Thickness(0, 0, 0, 200); gridNumKeyboard.Visibility = Visibility.Visible; HideInputPanel(); } private void BtnCloseKb_Click(object sender, RoutedEventArgs e) { bdrContent.Margin = new Thickness(0, 0, 0, 0); gridNumKeyboard.Visibility = Visibility.Collapsed; } private const Int32 WM_SYSCOMMAND = 274; private const UInt32 SC_CLOSE = 61536; [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] private static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] private static extern bool PostMessage(IntPtr hWnd, int Msg, uint wParam, uint lParam); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); //隐藏屏幕键盘 public static void HideInputPanel() { /* 线程优化1 System.Threading.ThreadStart start = delegate () { Thread.Sleep(500); IntPtr TouchhWnd = new IntPtr(0); TouchhWnd = FindWindow("IPTip_Main_Window", null); if (TouchhWnd == IntPtr.Zero) return; PostMessage(TouchhWnd, WM_SYSCOMMAND, SC_CLOSE, 0); }; Thread t = new System.Threading.Thread(start); t.IsBackground = true; t.Start(); */ Task t = new Task(() => { Thread.Sleep(500); IntPtr TouchhWnd = new IntPtr(0); TouchhWnd = FindWindow("IPTip_Main_Window", null); if (TouchhWnd == IntPtr.Zero) return; PostMessage(TouchhWnd, WM_SYSCOMMAND, SC_CLOSE, 0); }); t.Start(); } [DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)] public static extern void keybd_event(System.Windows.Forms.Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo); private void BtnNum_Click(object sender, RoutedEventArgs e) { Button btn = sender as Button; if (btn != null) { System.Windows.Forms.Keys key = System.Windows.Forms.Keys.Back; switch (btn.Content.ToString()) { case "0": key = System.Windows.Forms.Keys.NumPad0; break; case "1": key = System.Windows.Forms.Keys.NumPad1; break; case "2": key = System.Windows.Forms.Keys.NumPad2; break; case "3": key = System.Windows.Forms.Keys.NumPad3; break; case "4": key = System.Windows.Forms.Keys.NumPad4; break; case "5": key = System.Windows.Forms.Keys.NumPad5; break; case "6": key = System.Windows.Forms.Keys.NumPad6; break; case "7": key = System.Windows.Forms.Keys.NumPad7; break; case "8": key = System.Windows.Forms.Keys.NumPad8; break; case "9": key = System.Windows.Forms.Keys.NumPad9; break; case ".": key = System.Windows.Forms.Keys.Decimal; break; } keybd_event(key, 0, 0, 0); } } void btnReturn_Click(object sender, RoutedEventArgs e) { Border bdr = this.Parent as Border; if (bdr != null) { bdr.Child = null; } } void btnOK_Click(object sender, RoutedEventArgs e) { bool flag = true; if (tbMin.Text == "" || tbMax.Text == "" || tbSpeed.Text == "") { new PopupWindow("不允许为空!", "提示").ShowDialog(); return; } if (!Regex.IsMatch(tbMin.Text, "^(([1-9]\\d{0,3})|10000|0)$")) { tbMin.Text = ""; flag = false; } if (!Regex.IsMatch(tbMax.Text, "^(([1-9]\\d{0,3})|10000|0)$")) { tbMax.Text = ""; flag = false; } if (!Regex.IsMatch(tbSpeed.Text, "^(([1-9]\\d{0,3})|10000|0)$")) { tbSpeed.Text = ""; flag = false; } if (flag && int.Parse(tbMin.Text) > int.Parse(tbMax.Text)) { flag = false; } if (!flag) { new PopupWindow("输入格式错误,请重新输入!").ShowDialog(); return; } string range = tbMin.Text + "-" + tbMax.Text; string speed = tbSpeed.Text; string josnString = File.ReadAllText(System.AppDomain.CurrentDomain.BaseDirectory + "config.json", Encoding.UTF8); JObject jo = JObject.Parse(josnString); JArray jsonRange = JArray.Parse(jo["CustomMaterial"][pipeMaterialId]["range"].ToString()); JArray jsonSpeed = JArray.Parse(jo["CustomMaterial"][pipeMaterialId]["speed"].ToString()); if (btnFlag) { jsonRange.Add(range); jo["CustomMaterial"][pipeMaterialId]["range"] = jsonRange; jsonSpeed.Add(speed); jo["CustomMaterial"][pipeMaterialId]["speed"] = jsonSpeed; StreamWriter sw = new StreamWriter(System.AppDomain.CurrentDomain.BaseDirectory + "config.json"); sw.Write(JsonConvert.SerializeObject(jo)); sw.Flush(); sw.Close(); SoundSpeedInfo soundSpeedInfo = new SoundSpeedInfo(); soundSpeedInfo.ID = Guid.NewGuid().ToString(); soundSpeedInfo.Order = jsonRange.Count; soundSpeedInfo.MinRange = tbMin.Text; soundSpeedInfo.MaxRange = tbMax.Text; soundSpeedInfo.Speed = tbSpeed.Text; //绑定一个菜单项 SoundSpeedEditViewModel.Instance.SoundSpeedInfos.Add(soundSpeedInfo); } else { jsonRange[selectedIndex] = range; jo["CustomMaterial"][pipeMaterialId]["range"] = jsonRange; jsonSpeed[selectedIndex] = speed; jo["CustomMaterial"][pipeMaterialId]["speed"] = jsonSpeed; StreamWriter sw = new StreamWriter(System.AppDomain.CurrentDomain.BaseDirectory + "config.json"); sw.Write(JsonConvert.SerializeObject(jo)); sw.Flush(); sw.Close(); DataFactory.InitalSoundSpeedData(pipeMaterialId); } Border bdr = this.Parent as Border; if (bdr != null) { bdr.Child = null; } } private void Grid_PreviewMouseDown(object sender, MouseButtonEventArgs e) { btnClose.Focusable = true; btnClose.Focus(); btnClose.Focusable = false; } } }