diff --git a/App.xaml.cs b/App.xaml.cs index 3a8c3ac..e935ee7 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -19,6 +19,8 @@ public partial class App : Application public static bool UpdateTimerActive { get; private set; } = false; + private static bool firstShown { get; set; } = false; + private static readonly Mutex singleInstance = new Mutex(true, Assembly.GetExecutingAssembly().GetName().Name); protected override void OnStartup(StartupEventArgs e) @@ -38,18 +40,36 @@ protected override void OnStartup(StartupEventArgs e) MainWindowRef.Height = SteamFriendsPatcher.Properties.Settings.Default.windowHeight; } + MainWindowRef.WindowState = WindowState.Minimized; + MainWindowRef.Show(); + if (SteamFriendsPatcher.Properties.Settings.Default.minimizeToTray && SteamFriendsPatcher.Properties.Settings.Default.startMinimized) - return; + { + MainWindowRef.Hide(); + } - MainWindowRef.Show(); + if (!SteamFriendsPatcher.Properties.Settings.Default.minimizeToTray && SteamFriendsPatcher.Properties.Settings.Default.startMinimized) + { + var workingArea = SystemParameters.WorkArea; + MainWindowRef.Left = (workingArea.Width - SteamFriendsPatcher.Properties.Settings.Default.windowWidth) / 2 + workingArea.Left; + MainWindowRef.Top = (workingArea.Height - SteamFriendsPatcher.Properties.Settings.Default.windowHeight) / 2 + workingArea.Top; + firstShown = true; + } - if (SteamFriendsPatcher.Properties.Settings.Default.startMinimized) - MainWindowRef.WindowState = WindowState.Minimized; + if (!SteamFriendsPatcher.Properties.Settings.Default.startMinimized) + { + MainWindowRef.WindowState = WindowState.Normal; + } singleInstance.ReleaseMutex(); } else { + NativeMethods.PostMessage( + (IntPtr)NativeMethods.HWND_BROADCAST, + NativeMethods.WM_SHOWME, + IntPtr.Zero, + IntPtr.Zero); Current.Shutdown(1); } } @@ -78,6 +98,24 @@ private static void Setup() } } + public static void ShowMain() + { + if (!firstShown) + { + var workingArea = SystemParameters.WorkArea; + MainWindowRef.Left = (workingArea.Width - SteamFriendsPatcher.Properties.Settings.Default.windowWidth) / 2 + workingArea.Left; + MainWindowRef.Top = (workingArea.Height - SteamFriendsPatcher.Properties.Settings.Default.windowHeight) / 2 + workingArea.Top; + firstShown = true; + } + + if (!MainWindowRef.IsVisible) + MainWindowRef.Show(); + + if (MainWindowRef.WindowState == WindowState.Minimized) + MainWindowRef.WindowState = WindowState.Normal; + MainWindowRef.Activate(); + } + public static void ToggleUpdateTimer(bool status = true) { if (!UpdateTimerActive && !status) diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 9eeeed6..cac652f 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; +using System.Windows.Interop; namespace SteamFriendsPatcher { @@ -18,10 +19,27 @@ public MainWindow() Closing += MainWindow_Closing; SizeChanged += MainWindow_SizeChanged; StateChanged += MainWindow_StateChanged; + SourceInitialized += MainWindow_SourceInitialized; InitializeComponent(); SetupTrayIcon(); } + private void MainWindow_SourceInitialized(object sender, EventArgs e) + { + HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle); + source.AddHook(new HwndSourceHook(WndProc)); + } + + private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) + { + if (msg == NativeMethods.WM_SHOWME) + { + App.ShowMain(); + } + + return IntPtr.Zero; + } + private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) { NotifyIcon.Visible = false; @@ -103,12 +121,7 @@ private void SetupTrayIcon() private void ShowButton_Click(object sender, EventArgs e) { - if (!IsVisible) - Show(); - - if (WindowState == WindowState.Minimized) - WindowState = WindowState.Normal; - Activate(); + App.ShowMain(); } private static void ExitButton_Click(object sender, EventArgs e) diff --git a/NativeMethods.cs b/NativeMethods.cs index f939339..d062e4b 100644 --- a/NativeMethods.cs +++ b/NativeMethods.cs @@ -10,5 +10,14 @@ internal class NativeMethods [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] public static extern IntPtr FindWindowByClass(string lpClassName, IntPtr ZeroOnly = default); + + public const int HWND_BROADCAST = 0xffff; + public static readonly int WM_SHOWME = RegisterWindowMessage("WM_SHOWME"); + + [DllImport("user32")] + public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam); + + [DllImport("user32")] + public static extern int RegisterWindowMessage(string message); } } \ No newline at end of file