diff --git a/02_Windows_App/Perun_v1/01_Classes/NativeMethods.cs b/02_Windows_App/Perun_v1/01_Classes/NativeMethods.cs new file mode 100644 index 0000000..4372f27 --- /dev/null +++ b/02_Windows_App/Perun_v1/01_Classes/NativeMethods.cs @@ -0,0 +1,13 @@ +// this class just wraps some Win32 stuff that we're going to use for blocking of multiple instances +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")] + public static extern int RegisterWindowMessage(string message); +} \ No newline at end of file diff --git a/02_Windows_App/Perun_v1/01_Classes/Program.cs b/02_Windows_App/Perun_v1/01_Classes/Program.cs index 0d3e1a5..2476321 100644 --- a/02_Windows_App/Perun_v1/01_Classes/Program.cs +++ b/02_Windows_App/Perun_v1/01_Classes/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Threading; using System.Windows.Forms; namespace Perun_v1 @@ -9,24 +10,39 @@ namespace Perun_v1 /// /// Główny punkt wejścia dla aplikacji. /// + + // Mutex to block multiple instances + static Mutex mutex = new Mutex(true, "{5a710507-92e9-4664-9b91-918b8b82f107}"); [STAThread] - static void Main() { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - - Application.ApplicationExit += new EventHandler(Application_ApplicationExit); - - void Application_ApplicationExit(object sender, EventArgs e) + if (mutex.WaitOne(TimeSpan.Zero, true)) { - Properties.Settings.Default.Save(); - } + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.ApplicationExit += new EventHandler(Application_ApplicationExit); - Application.Run(new form_Main()); + void Application_ApplicationExit(object sender, EventArgs e) + { + Properties.Settings.Default.Save(); + } + Application.Run(new form_Main()); + mutex.ReleaseMutex(); + } + else + { + // send our Win32 message to make the currently running instance + // jump on top of all the other windows + NativeMethods.PostMessage( + (IntPtr)NativeMethods.HWND_BROADCAST, + NativeMethods.WM_SHOWME, + IntPtr.Zero, + IntPtr.Zero); + MessageBox.Show("Only one instance is allowed."); + } } - + } } diff --git a/02_Windows_App/Perun_v1/02_Forms/form_Main.cs b/02_Windows_App/Perun_v1/02_Forms/form_Main.cs index 9ea419c..18de3ce 100644 --- a/02_Windows_App/Perun_v1/02_Forms/form_Main.cs +++ b/02_Windows_App/Perun_v1/02_Forms/form_Main.cs @@ -544,5 +544,27 @@ namespace Perun_v1 } } } + + protected override void WndProc(ref Message m) + { + if (m.Msg == NativeMethods.WM_SHOWME) + { + ShowMe(); + } + base.WndProc(ref m); + } + private void ShowMe() + { + if (WindowState == FormWindowState.Minimized) + { + WindowState = FormWindowState.Normal; + } + // get our current "TopMost" value (ours will always be false though) + bool top = TopMost; + // make our form jump to the top of everything + TopMost = true; + // set it back to whatever it was + TopMost = top; + } } } diff --git a/02_Windows_App/Perun_v1/Perun_v1.csproj b/02_Windows_App/Perun_v1/Perun_v1.csproj index f867364..bcda4e7 100644 --- a/02_Windows_App/Perun_v1/Perun_v1.csproj +++ b/02_Windows_App/Perun_v1/Perun_v1.csproj @@ -32,7 +32,7 @@ Perun true Perun.htm - 1 + 2 0.4.0.%2a false true @@ -116,6 +116,7 @@ + Form