Skip to content

Commit

Permalink
fixes #25 (PR #26)
Browse files Browse the repository at this point in the history
fixes #25
  • Loading branch information
bethune-bryant authored Feb 6, 2018
2 parents 9cbd6e1 + 7e271fa commit e7caa56
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 23 deletions.
39 changes: 29 additions & 10 deletions TimeKeeper/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,6 +10,8 @@ namespace TimeKeeper
{
static class Program
{
static Mutex mutex = new Mutex(true, "{5231FC65-183F-44B3-9227-E3D54EE12474}");

/// <summary>
/// The main entry point for the application.
/// </summary>
Expand All @@ -22,20 +25,36 @@ static void Main(string[] args)
v => start_minimized = v != null },
};

List<string> extra;
try
if (mutex.WaitOne(TimeSpan.Zero, true))
{
extra = p.Parse(args);

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain(start_minimized));
List<string> 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);
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions TimeKeeper/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using System.Runtime.InteropServices;

namespace TimeKeeper
{
Expand Down Expand Up @@ -112,5 +113,16 @@ public static TimeSpan GetTime(IEnumerable<TimeEntry> 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);
}
}
}
2 changes: 1 addition & 1 deletion TimeKeeper/WindowManipulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
47 changes: 35 additions & 12 deletions TimeKeeper/frmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -474,9 +508,7 @@ private void showHideToolStripMenuItem_Click(object sender, EventArgs e)
}
else
{
this.Show();
SetWindowFocus();
this.WindowState = FormWindowState.Normal;
}
}

Expand All @@ -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)
Expand Down

0 comments on commit e7caa56

Please sign in to comment.