Skip to content

Commit

Permalink
实现应用程序单例运行
Browse files Browse the repository at this point in the history
  • Loading branch information
laggage committed Feb 7, 2020
1 parent 155e6e7 commit 6390c0b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
4 changes: 2 additions & 2 deletions WinPowerHelper.Wpf/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewmodels="clr-namespace:WinPowerHelper.Wpf.ViewModels"
StartupUri="MainWindow.xaml"
xmlns:system="clr-namespace:System;assembly=System.Runtime">
xmlns:system="clr-namespace:System;assembly=System.Runtime"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
Expand Down
21 changes: 21 additions & 0 deletions WinPowerHelper.Wpf/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
namespace WinPowerHelper
{
using System;
using System.Threading;
using System.Windows;
using WinPowerHelper.Wpf.Interaction;

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public App()
{
Startup += (e, args) =>
{
_mutex = new Mutex(true, "WinPowerHelper", out bool isNew);
if (!isNew)
{
NativeMethods.PostMessage(
(IntPtr)NativeMethods.HWND_BROADCAST,
NativeMethods.WM_SHOWME,
IntPtr.Zero,
IntPtr.Zero);
Environment.Exit(0);
}
};
}

private Mutex _mutex;
}
}
15 changes: 15 additions & 0 deletions WinPowerHelper.Wpf/Interaction/NativeMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace WinPowerHelper.Wpf.Interaction
{
using System;
using System.Runtime.InteropServices;

internal class NativeMethods
{
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", CharSet = CharSet.Unicode)]
public static extern int RegisterWindowMessage(string message);
}
}
17 changes: 17 additions & 0 deletions WinPowerHelper.Wpf/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
namespace WinPowerHelper
{
using System;
using System.Windows;
using System.Windows.Interop;
using WinPowerHelper.Wpf.Interaction;

/// <summary>
/// Interaction logic for MainWindow.xaml
Expand All @@ -11,5 +14,19 @@ public MainWindow()
{
InitializeComponent();
}

protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
var source = PresentationSource.FromVisual(this) as HwndSource;
source.AddHook(WndProc);
}

private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled)
{
if (msg == NativeMethods.WM_SHOWME)
Activate();
return IntPtr.Zero;
}
}
}

0 comments on commit 6390c0b

Please sign in to comment.