You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
STranslate/STranslate/MainWindow.xaml.cs

198 lines
5.9 KiB

using STranslate.Utils;
using STranslate.ViewModel;
using System;
2 years ago
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
2 years ago
namespace STranslate
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
2 years ago
private MainVM vm;
2 years ago
public MainWindow()
{
InitializeComponent();
vm = (MainVM)DataContext;
}
/// <summary>
/// 显示主窗口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OpenMainWin_Click(object sender, EventArgs e)
2 years ago
{
this.Show();
this.Activate();
2 years ago
}
/// <summary>
/// 移动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
2 years ago
2 years ago
/// <summary>
/// 软件运行时快捷键
2 years ago
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_KeyDown(object sender, KeyEventArgs e)
{
//最小化 Esc
2 years ago
if (e.Key == Key.Escape)
{
this.Hide();
2 years ago
vm.InputTxt = string.Empty;
vm.OutputTxt = string.Empty;
2 years ago
}
//退出 Ctrl+Shift+Q
if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control)
&& e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Shift)
&& e.Key == Key.Q)
2 years ago
{
2 years ago
Environment.Exit(0);
2 years ago
}
#if false
//置顶/取消置顶 Ctrl+T
if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control) && e.Key == Key.T)
{
Topmost = Topmost != true;
Opacity = Topmost ? 1 : 0.9;
}
2 years ago
//缩小 Ctrl+[
if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control) && e.Key == Key.OemOpenBrackets)
{
if (Width < 245)
{
return;
}
Width /= 1.2;
Height /= 1.2;
}
//放大 Ctrl+]
if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control) && e.Key == Key.OemCloseBrackets)
{
if (Width > 600)
{
return;
}
Width *= 1.2;
Height *= 1.2;
}
//恢复界面大小 Ctrl+P
if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control) && e.Key == Key.P)
{
Width = 400;
Height = 450;
}
#endif
2 years ago
}
2 years ago
/// <summary>
/// 监听全局快捷键
/// </summary>
/// <param name="e"></param>
protected override void OnSourceInitialized(EventArgs e)
{
IntPtr handle = new WindowInteropHelper(this).Handle;
2 years ago
HotKeysUtil.RegisterHotKey(handle);
HwndSource source = HwndSource.FromHwnd(handle);
source.AddHook(WndProc);
}
2 years ago
/// <summary>
/// 热键的功能
/// </summary>
/// <param name="m"></param>
protected IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handle)
{
switch (msg)
{
case 0x0312: //这个是window消息定义的 注册的热键消息
//Console.WriteLine(wParam.ToString());
2 years ago
if (wParam.ToString().Equals(HotKeysUtil.InputTranslateId + ""))
{
2 years ago
this.InputTranslateMenuItem_Click(null, null);
}
2 years ago
else if (wParam.ToString().Equals(HotKeysUtil.CrosswordTranslateId + ""))
{
2 years ago
this.CrossWordTranslateMenuItem_Click(null, null);
}
2 years ago
else if (wParam.ToString().Equals(HotKeysUtil.ScreenShotTranslateId + ""))
{
2 years ago
this.ScreenshotTranslateMenuItem_Click(null, null);
}
break;
}
return IntPtr.Zero;
}
/// <summary>
/// 非激活窗口则隐藏起来
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_Deactivated(object sender, EventArgs e)
{
this.Hide();
}
/// <summary>
2 years ago
/// 清空输入输出框
/// </summary>
2 years ago
private void ClearTextBox()
{
2 years ago
vm.InputTxt = string.Empty;
vm.OutputTxt = string.Empty;
}
/// <summary>
/// 输入翻译
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2 years ago
private void InputTranslateMenuItem_Click(object sender, RoutedEventArgs e)
{
ClearTextBox();
2 years ago
OpenMainWin_Click(null, null);
TextBoxInput.Focus();
}
/// <summary>
2 years ago
/// 划词翻译
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2 years ago
private void CrossWordTranslateMenuItem_Click(object sender, RoutedEventArgs e)
{
2 years ago
ClearTextBox();
var sentence = GetWords.Get();
this.Show();
this.Activate();
this.TextBoxInput.Text = sentence.Trim();
_ = vm.Translate();
}
/// <summary>
2 years ago
/// 截图翻译
/// </summary>
2 years ago
/// <param name="sender"></param>
/// <param name="e"></param>
private void ScreenshotTranslateMenuItem_Click(object sender, RoutedEventArgs e)
{
2 years ago
MessageBox.Show("开发中");
}
2 years ago
}
}