|
@@ -3,6 +3,7 @@ using System.Windows.Input;
|
|
|
using System.Windows.Controls;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.Windows.Interop;
|
|
|
|
|
|
namespace FaceChange
|
|
|
{
|
|
@@ -11,10 +12,11 @@ namespace FaceChange
|
|
|
/// </summary>
|
|
|
public partial class MainWindow : Window
|
|
|
{
|
|
|
- AppContext appContext = AppContext.GetAppContext();
|
|
|
- SerialTool serialTool = SerialTool.GetSerialTool();
|
|
|
+ private readonly AppContext _appContext = AppContext.GetAppContext();
|
|
|
+ private readonly SerialTool _serialTool = SerialTool.GetSerialTool();
|
|
|
|
|
|
- private List<FaceInfo> faces;
|
|
|
+ private readonly List<FaceInfo> _faces;
|
|
|
+ private bool _noDevice;
|
|
|
|
|
|
public static Label Status;
|
|
|
|
|
@@ -22,12 +24,16 @@ namespace FaceChange
|
|
|
{
|
|
|
InitializeComponent();
|
|
|
|
|
|
- faces = appContext.FaceInfos;
|
|
|
+ _faces = _appContext.FaceInfos;
|
|
|
+ Status = PortStatus;
|
|
|
+ LoadDevice();
|
|
|
}
|
|
|
|
|
|
private void Window_Loaded(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
- Status = PortStatus;
|
|
|
+ // 监听 Windows 消息,获取窗口句柄一定要写在窗口 loaded 事件里,才能获取到窗口句柄,否则为空
|
|
|
+ // 窗口过程 - 挂钩
|
|
|
+ (PresentationSource.FromVisual(this) as HwndSource)?.AddHook(DeviceChanged);
|
|
|
}
|
|
|
|
|
|
private void Window_KeyDown(object sender, KeyEventArgs e)
|
|
@@ -41,15 +47,15 @@ namespace FaceChange
|
|
|
}
|
|
|
if (e.Key == Key.Space)
|
|
|
{
|
|
|
- if (faces.Count == 0)
|
|
|
+ if (_faces.Count == 0)
|
|
|
{
|
|
|
MessageBox.Show("请先在设置页添加资源", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- var index = GetNoRepeatRandom(0, faces.Count);
|
|
|
- Name.Content = faces[index].Name;
|
|
|
- Face.Source = faces[index].Face;
|
|
|
+ var index = GetNoRepeatRandom(0, _faces.Count);
|
|
|
+ Name.Content = _faces[index].Name;
|
|
|
+ Face.Source = _faces[index].Face;
|
|
|
Console.WriteLine(index);
|
|
|
}
|
|
|
}
|
|
@@ -74,6 +80,7 @@ namespace FaceChange
|
|
|
|
|
|
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
|
|
|
{
|
|
|
+ if (_noDevice) return;
|
|
|
var messageBoxResult = MessageBox.Show("确定退出吗?", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Question);
|
|
|
if (messageBoxResult == MessageBoxResult.Cancel)
|
|
|
{
|
|
@@ -98,5 +105,62 @@ namespace FaceChange
|
|
|
this.WindowStyle = WindowStyle.None;
|
|
|
this.WindowState = WindowState.Maximized;
|
|
|
}
|
|
|
+
|
|
|
+ private void LoadDevice()
|
|
|
+ {
|
|
|
+ var portList = _serialTool.FindPort();
|
|
|
+ MessageBoxResult mbr;
|
|
|
+ switch (portList.Count)
|
|
|
+ {
|
|
|
+ case 0:
|
|
|
+ mbr = MessageBox.Show("没有找到可用硬件!\n请确保连接硬件后再打开本软件。", "错误提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
|
|
+ if (mbr == MessageBoxResult.OK)
|
|
|
+ {
|
|
|
+ _noDevice = true;
|
|
|
+ Close();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 1:
|
|
|
+ var serialPort = _serialTool.OpenPort(portList[0], "9600", "8", "1", "无", _appContext.DataReceived);
|
|
|
+ if (serialPort != null)
|
|
|
+ {
|
|
|
+ _appContext.SerialPort = serialPort;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ mbr = MessageBox.Show("请到设置页面配置正确串口!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
+ if (mbr == MessageBoxResult.OK)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Windows 消息编号,采用 Windows 的消息机制来捕获插入的 USB 状态
|
|
|
+ public const int WmDeviceChange = 0x219;
|
|
|
+ public const int DbtDeviceArrival = 0x8000;
|
|
|
+ public const int DbtDeviceRemoveComplete = 0x8004;
|
|
|
+
|
|
|
+ // 设备插拔改变函数
|
|
|
+ private IntPtr DeviceChanged(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
|
|
|
+ {
|
|
|
+ if (msg == WmDeviceChange)
|
|
|
+ {
|
|
|
+ switch (wParam.ToInt32())
|
|
|
+ {
|
|
|
+ case DbtDeviceArrival:
|
|
|
+ // 设备插入
|
|
|
+ LoadDevice();
|
|
|
+ Console.WriteLine("设备插入");
|
|
|
+ break;
|
|
|
+ case DbtDeviceRemoveComplete:
|
|
|
+ // 设备卸载
|
|
|
+ Console.WriteLine("设备卸载");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return IntPtr.Zero;
|
|
|
+ }
|
|
|
}
|
|
|
}
|