From 06345a0cafce11e696f1491f1cd54f4b047588ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Fri, 29 Oct 2021 13:25:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=89=98=E7=9B=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.UI/App.xaml.cs | 51 ++++++++++++++++++++++++- FastGithub.UI/FastGithub.UI.csproj | 2 + FastGithub.UI/MainWindow.xaml.cs | 61 +++++++++++++++++++++++++++++- 3 files changed, 111 insertions(+), 3 deletions(-) diff --git a/FastGithub.UI/App.xaml.cs b/FastGithub.UI/App.xaml.cs index 28caba8..dc50b11 100644 --- a/FastGithub.UI/App.xaml.cs +++ b/FastGithub.UI/App.xaml.cs @@ -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 /// 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); } + } } diff --git a/FastGithub.UI/FastGithub.UI.csproj b/FastGithub.UI/FastGithub.UI.csproj index 3b10cb3..ea61c7c 100644 --- a/FastGithub.UI/FastGithub.UI.csproj +++ b/FastGithub.UI/FastGithub.UI.csproj @@ -44,6 +44,8 @@ + + diff --git a/FastGithub.UI/MainWindow.xaml.cs b/FastGithub.UI/MainWindow.xaml.cs index 273b71f..482a20c 100644 --- a/FastGithub.UI/MainWindow.xaml.cs +++ b/FastGithub.UI/MainWindow.xaml.cs @@ -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 /// 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); } } }