Skip to content
This repository has been archived by the owner on May 15, 2022. It is now read-only.

Commit

Permalink
If another instance is launched, show first instance
Browse files Browse the repository at this point in the history
Side effect of implementing this is that I had to fix starting it tray so that the window gets properly initialized.

This is to prepare for an option to not show tray icon.
  • Loading branch information
PhantomGamers committed Jun 13, 2019
1 parent ef7d201 commit 17a769e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 10 deletions.
46 changes: 42 additions & 4 deletions App.xaml.cs
Expand Up @@ -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)
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 19 additions & 6 deletions MainWindow.xaml.cs
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;

namespace SteamFriendsPatcher
{
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions NativeMethods.cs
Expand Up @@ -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);
}
}

0 comments on commit 17a769e

Please sign in to comment.