From 7e271fa4207ff974fa93460ff7cd33539f926d00 Mon Sep 17 00:00:00 2001 From: Bryant Nelson Date: Tue, 6 Feb 2018 12:44:19 -0600 Subject: [PATCH] fixes #25 --- TimeKeeper/Program.cs | 39 +++++++++++++++++++------- TimeKeeper/Utilities.cs | 12 ++++++++ TimeKeeper/WindowManipulation.cs | 2 +- TimeKeeper/frmMain.cs | 47 ++++++++++++++++++++++++-------- 4 files changed, 77 insertions(+), 23 deletions(-) diff --git a/TimeKeeper/Program.cs b/TimeKeeper/Program.cs index 81e8b1e..5f49b9c 100644 --- a/TimeKeeper/Program.cs +++ b/TimeKeeper/Program.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using NDesk.Options; @@ -9,6 +10,8 @@ namespace TimeKeeper { static class Program { + static Mutex mutex = new Mutex(true, "{5231FC65-183F-44B3-9227-E3D54EE12474}"); + /// /// The main entry point for the application. /// @@ -22,20 +25,36 @@ static void Main(string[] args) v => start_minimized = v != null }, }; - List extra; - try + if (mutex.WaitOne(TimeSpan.Zero, true)) { - extra = p.Parse(args); - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new frmMain(start_minimized)); + List extra; + try + { + extra = p.Parse(args); + + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new frmMain(start_minimized)); + } + catch (OptionException e) + { + Console.Write("Timekeeper: "); + Console.WriteLine(e.Message); + return; + } + + mutex.ReleaseMutex(); } - catch (OptionException e) + else { - Console.Write("Timekeeper: "); - Console.WriteLine(e.Message); - return; + // send our Win32 message to make the currently running instance + // jump on top of all the other windows + Utilities.NativeMethods.PostMessage( + (IntPtr)Utilities.NativeMethods.HWND_BROADCAST, + Utilities.NativeMethods.WM_SHOWME, + IntPtr.Zero, + IntPtr.Zero); } } } diff --git a/TimeKeeper/Utilities.cs b/TimeKeeper/Utilities.cs index 79cf753..16c5f90 100644 --- a/TimeKeeper/Utilities.cs +++ b/TimeKeeper/Utilities.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Linq; using System.Globalization; +using System.Runtime.InteropServices; namespace TimeKeeper { @@ -112,5 +113,16 @@ public static TimeSpan GetTime(IEnumerable entries) return retval; } + + // this class just wraps some Win32 stuff + 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); + } } } diff --git a/TimeKeeper/WindowManipulation.cs b/TimeKeeper/WindowManipulation.cs index eaf5b7d..ba07077 100644 --- a/TimeKeeper/WindowManipulation.cs +++ b/TimeKeeper/WindowManipulation.cs @@ -66,7 +66,7 @@ public static bool BringForwardWindow(string Title) } catch (Exception exc) { - MessageBox.Show("Error:" + Environment.NewLine + exc.Message); + //MessageBox.Show("Error:" + Environment.NewLine + exc.Message); return false; } } diff --git a/TimeKeeper/frmMain.cs b/TimeKeeper/frmMain.cs index f3b4e87..ea04882 100644 --- a/TimeKeeper/frmMain.cs +++ b/TimeKeeper/frmMain.cs @@ -57,13 +57,14 @@ void KeyboardHook_KeyUp(object sender, KeyEventArgs e) private void timerKeyHooks_Tick(object sender, EventArgs e) { timerKeyHooks.Enabled = false; - SetWindowFocus(); if (pressed == Keys.C) { + SetWindowFocus(); closeToolStripMenuItem_Click(sender, e); } else if (pressed == Keys.A) { + SetWindowFocus(); addToolStripMenuItem_Click(sender, e); } else if (pressed == Keys.S) @@ -461,9 +462,42 @@ private void closeToolStripMenuItem_Click(object sender, EventArgs e) CloseCurrentTask(); } + protected override void WndProc(ref Message m) + { + if (m.Msg == Utilities.NativeMethods.WM_SHOWME) + { + SetWindowFocus(); + } + base.WndProc(ref m); + } + private void SetWindowFocus() { + if(!this.Visible) + { + this.Show(); + } + + this.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; + + WindowManipulation.BringForwardWindow(this.Text); + try + { + WindowManipulation.BringForwardWindow(stillWorking.Text); + stillWorking.Focus(); + } + catch + { + + } } private void showHideToolStripMenuItem_Click(object sender, EventArgs e) @@ -474,9 +508,7 @@ private void showHideToolStripMenuItem_Click(object sender, EventArgs e) } else { - this.Show(); SetWindowFocus(); - this.WindowState = FormWindowState.Normal; } } @@ -488,15 +520,6 @@ private void notifyIcon1_DoubleClick(object sender, EventArgs e) private void notifyIcon1_BalloonTipClicked(object sender, EventArgs e) { SetWindowFocus(); - try - { - WindowManipulation.BringForwardWindow(stillWorking.Text); - stillWorking.Focus(); - } - catch - { - - } } private void exitToolStripMenuItem_Click(object sender, EventArgs e)