using STranslate.Helper;
using STranslate.ViewModel;
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
namespace STranslate
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
private MainVM vm;
public MainWindow()
{
InitializeComponent();
vm = (MainVM)DataContext;
InitView();
InitialTray();
//if (HotKeys.InputTranslate.Conflict || HotKeys.CrosswordTranslate.Conflict || HotKeys.ScreenShotTranslate.Conflict)
//{
// MessageBox.Show("全局快捷键有冲突,请您到设置中重新设置");
//}
}
private void InitialTray()
{
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
notifyIcon.Text = $"STranslate_{version}\n" +
$"划词翻译快捷键: ALT + D\n" +
$"输入翻译快捷键: ALT + A\n";
//notifyIcon.BalloonTipText = "STranslate 已启动";
notifyIcon.Icon = new System.Drawing.Icon(Application.GetResourceStream(new Uri("Images/translate.ico", UriKind.Relative)).Stream);
notifyIcon.Visible = true;
//notifyIcon.ShowBalloonTip(1000);
notifyIcon.MouseDoubleClick += NotifyIcon_MouseDoubleClick;
System.Windows.Forms.MenuItem CrossWordTranslateMenuItemBTN = new System.Windows.Forms.MenuItem("划词翻译");
CrossWordTranslateMenuItemBTN.Click += new EventHandler(CrossWordTranslateMenuItem_Click);
//当失去焦点后无法从托盘处获取选中文本
CrossWordTranslateMenuItemBTN.Visible = false;
System.Windows.Forms.MenuItem ScreenshotTranslateMenuItemBTN = new System.Windows.Forms.MenuItem("截图翻译");
ScreenshotTranslateMenuItemBTN.Click += new EventHandler(ScreenshotTranslateMenuItem_Click);
ScreenshotTranslateMenuItemBTN.Enabled = false;
System.Windows.Forms.MenuItem InputTranslateMenuItemBTN = new System.Windows.Forms.MenuItem("输入翻译");
InputTranslateMenuItemBTN.Click += new EventHandler(InputTranslateMenuItem_Click);
System.Windows.Forms.MenuItem OpenMainWinBTN = new System.Windows.Forms.MenuItem("显示主界面");
OpenMainWinBTN.Click += new EventHandler(OpenMainWin_Click);
System.Windows.Forms.MenuItem AutoStartBTN = new System.Windows.Forms.MenuItem("开机自启");
AutoStartBTN.Click += new EventHandler(AutoStart_Click);
AutoStartBTN.Checked = StartupHelper.IsStartup();
System.Windows.Forms.MenuItem ExitBTN = new System.Windows.Forms.MenuItem("退出");
ExitBTN.Click += new EventHandler(Exit_Click);
System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] {
CrossWordTranslateMenuItemBTN,
ScreenshotTranslateMenuItemBTN,
InputTranslateMenuItemBTN,
OpenMainWinBTN,
AutoStartBTN,
ExitBTN,
};
notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);
}
private void AutoStart_Click(object sender, EventArgs e)
{
if (StartupHelper.IsStartup()) StartupHelper.UnSetStartup();
else StartupHelper.SetStartup();
(sender as System.Windows.Forms.MenuItem).Checked = StartupHelper.IsStartup();
}
///
/// 移动
///
///
///
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
///
/// 软件运行时快捷键
///
///
///
private void Window_KeyDown(object sender, KeyEventArgs e)
{
//最小化 Esc
if (e.Key == Key.Escape)
{
this.Hide();
//取消置顶
if (_IsTopmost)
{
TopmostBtn.SetResourceReference(TemplateProperty, _UnTopmostTemplateName);
_IsTopmost = !_IsTopmost;
}
}
//置顶 Ctrl+Shift+T
if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control)
&& e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Shift)
&& e.Key == Key.T)
{
Top_Click(null, null);
}
//退出 Ctrl+Shift+Q
if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control)
&& e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Shift)
&& e.Key == Key.Q)
{
Exit_Click(null, null);
}
}
///
/// 监听全局快捷键
///
///
protected override void OnSourceInitialized(EventArgs e)
{
//base.OnSourceInitialized(e);
IntPtr handle = new WindowInteropHelper(this).Handle;
HotkeysHelper.RegisterHotKey(handle);
HwndSource source = HwndSource.FromHwnd(handle);
source.AddHook(WndProc);
}
///
/// 热键的功能
///
///
protected IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handle)
{
switch (msg)
{
case 0x0312: //这个是window消息定义的 注册的热键消息
//Console.WriteLine(wParam.ToString());
if (wParam.ToString().Equals(HotkeysHelper.InputTranslateId + ""))
{
this.InputTranslateMenuItem_Click(null, null);
}
else if (wParam.ToString().Equals(HotkeysHelper.CrosswordTranslateId + ""))
{
this.CrossWordTranslateMenuItem_Click(null, null);
}
#if false
else if (wParam.ToString().Equals(HotkeysHelper.ScreenShotTranslateId + ""))
{
this.ScreenshotTranslateMenuItem_Click(null, null);
}
#endif
else if (wParam.ToString().Equals(HotkeysHelper.OpenMainWindowId + ""))
{
this.OpenMainWin_Click(null, null);
}
break;
}
return IntPtr.Zero;
}
///
/// 非激活窗口则隐藏起来
///
///
///
private void Window_Deactivated(object sender, EventArgs e)
{
if (!_IsTopmost)
{
this.Hide();
}
}
///
/// 清空输入输出框
///
private void ClearTextBox()
{
vm.InputTxt = string.Empty;
vm.OutputTxt = string.Empty;
vm.SnakeRet = string.Empty;
vm.SmallHumpRet = string.Empty;
vm.LargeHumpRet = string.Empty;
vm.IdentifyLanguage = string.Empty;
}
///
/// 显示主窗口
///
///
///
private void OpenMainWin_Click(object sender, EventArgs e)
{
this.Show();
this.Activate();
}
///
/// 左键双击
///
///
///
private void NotifyIcon_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
InputTranslateMenuItem_Click(null, null);
}
///
/// 输入翻译
///
///
///
private void InputTranslateMenuItem_Click(object sender, EventArgs e)
{
ClearTextBox();
OpenMainWin_Click(null, null);
TextBoxInput.Focus();
}
///
/// 划词翻译
///
///
///
private void CrossWordTranslateMenuItem_Click(object sender, EventArgs e)
{
ClearTextBox();
var sentence = GetWordsHelper.Get();
this.Show();
this.Activate();
this.TextBoxInput.Text = sentence.Trim();
_ = vm.Translate();
}
///
/// 截图翻译
///
///
///
private void ScreenshotTranslateMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("开发中");
}
///
/// 是否置顶
///
///
///
private void Top_Click(object sender, RoutedEventArgs e)
{
if (_IsTopmost)
{
TopmostBtn.SetResourceReference(TemplateProperty, _UnTopmostTemplateName);
}
else
{
TopmostBtn.SetResourceReference(TemplateProperty, _TopmostTemplateName);
}
_IsTopmost = !_IsTopmost;
}
///
/// 退出
///
///
///
private void Exit_Click(object sender, EventArgs e)
{
notifyIcon.Dispose();
Environment.Exit(0);
}
private void InitView()
{
this.Activate();
this.TextBoxInput.Focus();
}
private System.Windows.Forms.NotifyIcon notifyIcon = new System.Windows.Forms.NotifyIcon();
private bool _IsTopmost { get; set; }
private readonly string _TopmostTemplateName = "ButtonTemplateTopmost";
private readonly string _UnTopmostTemplateName = "ButtonTemplateUnTopmost";
}
}