增加托盘

This commit is contained in:
陈国伟 2021-10-29 13:25:52 +08:00
parent 810aab081f
commit 06345a0caf
3 changed files with 111 additions and 3 deletions

View File

@ -1,5 +1,7 @@
using Microsoft.Win32;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows;
namespace FastGithub.UI
@ -9,12 +11,57 @@ namespace FastGithub.UI
/// </summary>
public partial class App : Application
{
private Mutex mutex;
private Process fastGithub;
protected override void OnStartup(StartupEventArgs e)
{
this.mutex = new Mutex(true, "Global\\FastGithub.UI", out var firstInstance);
if (firstInstance == false)
{
this.Shutdown();
return;
}
this.fastGithub = StartFastGithub();
SetInternetExplorer();
base.OnStartup(e);
}
protected override void OnExit(ExitEventArgs e)
{
this.mutex.Dispose();
if (this.fastGithub != null && this.fastGithub.HasExited == false)
{
this.fastGithub.Kill();
}
base.OnExit(e);
}
private static Process StartFastGithub()
{
const string fileName = "fastgithub.exe";
if (File.Exists(fileName) == false)
{
return default;
}
var startInfo = new ProcessStartInfo
{
FileName = fileName,
UseShellExecute = false,
CreateNoWindow = true
};
return Process.Start(startInfo);
}
private static void SetInternetExplorer(int version = 9000)
{
var emulation = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true);
var key = $"{Process.GetCurrentProcess().ProcessName}.exe";
emulation.SetValue(key, 9000, RegistryValueKind.DWord);
base.OnStartup(e);
emulation.SetValue(key, version, RegistryValueKind.DWord);
}
}
}

View File

@ -44,6 +44,8 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />

View File

@ -1,4 +1,9 @@
using System.Windows;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;
namespace FastGithub.UI
{
@ -7,9 +12,63 @@ namespace FastGithub.UI
/// </summary>
public partial class MainWindow : Window
{
private NotifyIcon notifyIcon;
public MainWindow()
{
InitializeComponent();
var exit = new MenuItem("退出");
exit.Click += (s, e) => this.Close();
this.notifyIcon = new NotifyIcon
{
Visible = true,
Text = "FastGithub",
ContextMenu = new ContextMenu(new[] { exit }),
Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath)
};
this.notifyIcon.MouseClick += (s, e) => this.Show();
var fileName = "fastgithub.exe";
if (File.Exists(fileName))
{
var version = FileVersionInfo.GetVersionInfo(fileName);
this.Title = $"FastGithub v{version.ProductVersion}";
}
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
var hwndSource = PresentationSource.FromVisual(this) as HwndSource;
hwndSource.AddHook(WndProc);
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
const int WM_SYSCOMMAND = 0x112;
const int SC_CLOSE = 0xf060;
const int SC_MINIMIZE = 0xf020;
if (msg == WM_SYSCOMMAND)
{
if (wParam.ToInt32() == SC_CLOSE || wParam.ToInt32() == SC_MINIMIZE)
{
this.Hide();
handled = true;
}
}
return IntPtr.Zero;
}
protected override void OnClosed(EventArgs e)
{
this.notifyIcon.Icon = null;
this.notifyIcon.Dispose();
base.OnClosed(e);
}
}
}