perf: replace hardcodet by forms notifyicon(win11 will have a better experience)

dev_self
SONGE9B3\song_pd_win11 2 years ago
parent 741d9a2efd
commit 579d967003

@ -53,6 +53,7 @@
2. 软件内快捷键
- `ESC` 隐藏界面
- `Ctrl+Shift+Q` 退出程序
- `Ctrl+Shift+R` 切换主题
- `Ctrl+Shift+T` 置顶/取消置顶
点击软件外部界面任意处或点击软件则会自动隐藏到后台,即用即走。

@ -53,9 +53,6 @@
<ApplicationIcon>Images\translate.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="Hardcodet.NotifyIcon.Wpf, Version=1.1.0.0, Culture=neutral, PublicKeyToken=682384a853a08aad, processorArchitecture=MSIL">
<HintPath>..\packages\Hardcodet.NotifyIcon.Wpf.1.1.0\lib\net472\Hardcodet.NotifyIcon.Wpf.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Xaml.Behaviors, Version=1.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Xaml.Behaviors.Wpf.1.1.39\lib\net45\Microsoft.Xaml.Behaviors.dll</HintPath>
</Reference>

@ -20,29 +20,6 @@
Width="400"
Icon="/Images/translate.ico">
<Window.Resources>
<ContextMenu x:Key="TrayMenu">
<MenuItem Header="输入翻译"
InputGestureText="Alt+A"
Command="{Binding InputTranslateCmd}"/>
<MenuItem Header="截图翻译"
InputGestureText="Alt+S"
Command="{Binding ScreenShotTranslateCmd}"/>
<MenuItem Header="划词翻译"
InputGestureText="Alt+D"
IsEnabled="False"/>
<MenuItem Header="显示主界面"
InputGestureText="Alt+G"
Command="{Binding ShowMainWinCmd}"/>
<MenuItem Header="开机自启"
IsChecked="{Binding IsStartup}"
Command="{Binding StartupCmd}"/>
<Separator />
<MenuItem Header="退出"
Command="{Binding ExitCmd}"/>
</ContextMenu>
</Window.Resources>
<Window.InputBindings>
<KeyBinding Modifiers="Ctrl+Shift"
Key="Q"
@ -51,6 +28,9 @@
Key="T"
Command="{Binding TopmostCmd}"
CommandParameter="{Binding ElementName=TopmostBtn, Mode=OneWay}"/>
<KeyBinding Modifiers="Ctrl+Shift"
Key="R"
Command="{Binding ThemeConvertCmd}"/>
<KeyBinding Key="Esc"
Command="{Binding EscCmd}"/>
</Window.InputBindings>
@ -74,14 +54,6 @@
<RowDefinition Height="7*"/>
</Grid.RowDefinitions>
<!--tray-->
<tb:TaskbarIcon x:Name="myNotifyIcon"
Visibility="{Binding IsVisibility, Converter={StaticResource BooleanToVisibilityConverter}}"
ToolTipText="{Binding TrayToolTip}"
ContextMenu="{StaticResource TrayMenu}"
DoubleClickCommand="{Binding InputTranslateCmd}"
IconSource="/Images/translate.ico"/>
<!--header-->
<Grid x:Name="Header">
<Button x:Name="TopmostBtn"

@ -2,7 +2,6 @@
using STranslate.ViewModel;
using System;
using System.Windows;
using System.Windows.Input;
namespace STranslate.View
{
@ -11,14 +10,13 @@ namespace STranslate.View
/// </summary>
public partial class MainWindow : Window
{
private MainVM vm = MainVM.Instance;
public MainWindow()
{
//InitializeComponent();
DataContext = vm;
vm.Mainwin = this;
InitialTray();
//if (HotKeys.InputTranslate.Conflict || HotKeys.CrosswordTranslate.Conflict || HotKeys.ScreenShotTranslate.Conflict)
//{
// MessageBox.Show("全局快捷键有冲突...");
@ -56,5 +54,84 @@ namespace STranslate.View
vm.OpenMainWin();
});
}
private MainVM vm = MainVM.Instance;
public System.Windows.Forms.NotifyIcon NotifyIcon = new System.Windows.Forms.NotifyIcon();
#region TrayIcon
private void InitialTray()
{
var app = System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location);
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
NotifyIcon.Text = $"{app} {version.Substring(0, version.Length - 2)}";
NotifyIcon.Icon = new System.Drawing.Icon(Application.GetResourceStream(new Uri("Images/translate.ico", UriKind.Relative)).Stream);
NotifyIcon.Visible = true;
NotifyIcon.BalloonTipText = $"{app} already started...";
NotifyIcon.ShowBalloonTip(500);
NotifyIcon.MouseDoubleClick += InputTranslateMenuItem_Click;
System.Windows.Forms.MenuItem InputTranslateMenuItemBTN = new System.Windows.Forms.MenuItem("输入翻译");
InputTranslateMenuItemBTN.Click += new EventHandler(InputTranslateMenuItem_Click);
System.Windows.Forms.MenuItem ScreenshotTranslateMenuItemBTN = new System.Windows.Forms.MenuItem("截图翻译");
ScreenshotTranslateMenuItemBTN.Click += new EventHandler(ScreenshotTranslateMenuItem_Click);
System.Windows.Forms.MenuItem CrossWordTranslateMenuItemBTN = new System.Windows.Forms.MenuItem("划词翻译");
//CrossWordTranslateMenuItemBTN.Click += new EventHandler(CrossWordTranslateMenuItem_Click);
//当失去焦点后无法从托盘处获取选中文本
CrossWordTranslateMenuItemBTN.Enabled = false;
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[] {
InputTranslateMenuItemBTN,
ScreenshotTranslateMenuItemBTN,
CrossWordTranslateMenuItemBTN,
OpenMainWinBTN,
AutoStartBTN,
ExitBTN,
};
NotifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);
}
private void ScreenshotTranslateMenuItem_Click(object sender, EventArgs e)
{
vm.ScreenShotTranslate();
}
private void OpenMainWin_Click(object sender, EventArgs e)
{
vm.OpenMainWin();
}
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 Exit_Click(object sender, EventArgs e)
{
vm.ExitApp(0);
}
private void InputTranslateMenuItem_Click(object sender, EventArgs e)
{
vm.InputTranslate();
}
#endregion
}
}

@ -21,49 +21,14 @@ namespace STranslate.ViewModel
{
ExitApp(-1);
}
TrayToolTip = $"{System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)} " +
$"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Version}";
InputCombo = LanguageEnumDict.Keys.ToList();
OutputCombo = LanguageEnumDict.Keys.ToList();
#endregion
#region 托盘程序
//运行前检查是否开机自启
IsStartup = StartupHelper.IsStartup();
//输入翻译
InputTranslateCmd = new RelayCommand((_) => true, (_) =>
{
InputTranslate();
});
//截图翻译
ScreenShotTranslateCmd = new RelayCommand((_) => true, (_) =>
{
ScreenShotTranslate();
});
//显示主界面
ShowMainWinCmd = new RelayCommand((_) => true, (_) =>
{
OpenMainWin();
});
//开机自启
StartupCmd = new RelayCommand((_) => true, (_) =>
{
if (StartupHelper.IsStartup()) StartupHelper.UnSetStartup();
else StartupHelper.SetStartup();
IsStartup = StartupHelper.IsStartup();
});
//退出App
ExitCmd = new RelayCommand((_) => true, (_) =>
{
ExitApp(0);
});
#region Common
//退出
ExitCmd = new RelayCommand((_) => true, (_) => ExitApp(0));
//置顶
TopmostCmd = new RelayCommand((_) => true, (o) =>
{
@ -101,10 +66,6 @@ namespace STranslate.ViewModel
_ = Translate();
}
});
#endregion
#region Common
//移动
MouseLeftDownCmd = new RelayCommand((_) => true, (_) =>
{
@ -242,8 +203,7 @@ namespace STranslate.ViewModel
/// </summary>
public void ExitApp(int id)
{
//隐藏icon
IsVisibility = false;
Mainwin.NotifyIcon.Dispose();
Mainwin.Close();
//语音合成销毁
Speech.Dispose();
@ -429,33 +389,11 @@ namespace STranslate.ViewModel
public ICommand CopySmallHumpResultCmd { get; private set; }
public ICommand CopyLargeHumpResultCmd { get; private set; }
public ICommand ThemeConvertCmd { get; private set; }
//托盘程序
public ICommand InputTranslateCmd { get; private set; }
public ICommand ScreenShotTranslateCmd { get; private set; }
public ICommand ShowMainWinCmd { get; private set; }
public ICommand StartupCmd { get; private set; }
public ICommand ExitCmd { get; private set; }
public ICommand TopmostCmd { get; private set; }
public ICommand EscCmd { get; private set; }
public ICommand ExitCmd { get; private set; }
public ICommand SelectLangChangedCmd { get; private set; }
/// <summary>
/// 托盘程序ToolTip
/// </summary>
private string _TrayToolTip;
public string TrayToolTip { get => _TrayToolTip; set => UpdateProperty(ref _TrayToolTip, value); }
/// <summary>
/// 是否开机自启
/// </summary>
private bool _IsStartup;
public bool IsStartup { get => _IsStartup; set => UpdateProperty(ref _IsStartup, value); }
/// <summary>
/// 托盘图标可见
/// </summary>
private bool _IsVisibility = true;
public bool IsVisibility { get => _IsVisibility; set => UpdateProperty(ref _IsVisibility, value); }
/// <summary>
/// view传递至viewmodel
/// </summary>

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Hardcodet.NotifyIcon.Wpf" version="1.1.0" targetFramework="net48" />
<package id="Microsoft.Xaml.Behaviors.Wpf" version="1.1.39" targetFramework="net48" />
<package id="Newtonsoft.Json" version="13.0.2" targetFramework="net48" />
<package id="Tesseract" version="5.2.0" targetFramework="net48" />

Loading…
Cancel
Save