Skip to content
Browse files

Fix merge conflict

  • Loading branch information...
2 parents 7c91c80 + 41cf1db commit 978f0bc9807466f0d08d32c9f07efd3e964c904e @ejsmile committed Mar 1, 2012
Showing with 180 additions and 3 deletions.
  1. +7 −1 Client/Client.csproj
  2. +106 −0 Client/HotKey.cs
  3. +66 −2 Client/MainWindow.xaml.cs
  4. +1 −0 ToDoTests/ToDoTests.csproj
View
8 Client/Client.csproj
@@ -59,6 +59,8 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
+ <Reference Include="System.Drawing" />
+ <Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />
@@ -79,6 +81,7 @@
<Compile Include="FilterDialog.xaml.cs">
<DependentUpon>FilterDialog.xaml</DependentUpon>
</Compile>
+ <Compile Include="HotKey.cs" />
<Compile Include="Options.xaml.cs">
<DependentUpon>Options.xaml</DependentUpon>
</Compile>
@@ -155,12 +158,15 @@
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
- <Resource Include="TodoTouch_512.ico" />
+ <Resource Include="TodoTouch_512.ico">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </Resource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resource.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resource.Designer.cs</LastGenOutput>
+ <SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
View
106 Client/HotKey.cs
@@ -0,0 +1,106 @@
+using System;
+using System.Diagnostics.Contracts;
+using System.Runtime.InteropServices;
+using System.Windows;
+using System.Windows.Forms;
+using System.Windows.Input;
+using System.Windows.Interop;
+
+namespace Client
+{
+ //Use code from http://www.assembla.com/code/outcoldman_p/subversion/nodes/BlogProjects/WpfApplicationHotKey
+ internal class HotKeyWinApi
+ {
+ public const int WmHotKey = 0x0312;
+
+ [DllImport("user32.dll", SetLastError = true)]
+ public static extern bool RegisterHotKey(IntPtr hWnd, int id, ModifierKeys fsModifiers, Keys vk);
+
+ [DllImport("user32.dll", SetLastError = true)]
+ public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
+ }
+
+ public sealed class HotKey : IDisposable
+ {
+ public event Action<HotKey> HotKeyPressed;
+
+ private readonly int _id;
+ private bool _isKeyRegistered;
+ readonly IntPtr _handle;
+
+ public HotKey(ModifierKeys modifierKeys, Keys key, Window window)
+ : this(modifierKeys, key, new WindowInteropHelper(window))
+ {
+ Contract.Requires(window != null);
+ }
+
+ public HotKey(ModifierKeys modifierKeys, Keys key, WindowInteropHelper window)
+ : this(modifierKeys, key, window.Handle)
+ {
+ Contract.Requires(window != null);
+ }
+
+ public HotKey(ModifierKeys modifierKeys, Keys key, IntPtr windowHandle)
+ {
+ Contract.Requires(modifierKeys != ModifierKeys.None || key != Keys.None);
+ Contract.Requires(windowHandle != IntPtr.Zero);
+
+ Key = key;
+ KeyModifier = modifierKeys;
+ _id = GetHashCode();
+ _handle = windowHandle;
+ RegisterHotKey();
+ ComponentDispatcher.ThreadPreprocessMessage += ThreadPreprocessMessageMethod;
+ }
+
+ ~HotKey()
+ {
+ Dispose();
+ }
+
+ public Keys Key { get; private set; }
+
+ public ModifierKeys KeyModifier { get; private set; }
+
+ public void RegisterHotKey()
+ {
+ if (Key == Keys.None)
+ return;
+ if (_isKeyRegistered)
+ UnregisterHotKey();
+ _isKeyRegistered = HotKeyWinApi.RegisterHotKey(_handle, _id, KeyModifier, Key);
+ if (!_isKeyRegistered)
+ throw new ApplicationException("Hotkey already in use");
+ }
+
+ public void UnregisterHotKey()
+ {
+ _isKeyRegistered = !HotKeyWinApi.UnregisterHotKey(_handle, _id);
+ }
+
+ public void Dispose()
+ {
+ ComponentDispatcher.ThreadPreprocessMessage -= ThreadPreprocessMessageMethod;
+ UnregisterHotKey();
+ }
+
+ private void ThreadPreprocessMessageMethod(ref MSG msg, ref bool handled)
+ {
+ if (!handled)
+ {
+ if (msg.message == HotKeyWinApi.WmHotKey
+ && (int)(msg.wParam) == _id)
+ {
+ OnHotKeyPressed();
+ handled = true;
+ }
+ }
+ }
+
+ private void OnHotKeyPressed()
+ {
+ if (HotKeyPressed != null)
+ HotKeyPressed(this);
+ }
+ }
+}
View
68 Client/MainWindow.xaml.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows;
@@ -60,6 +61,8 @@ enum SortType
Task _updating;
int _intelliPos;
DispatcherTimer _dispatcherTimer;
+ System.Windows.Forms.NotifyIcon _notifyIcon;
+ HotKey _hotkey;
WindowLocation _previousWindowLocaiton;
@@ -68,6 +71,32 @@ public MainWindow()
try
{
InitializeComponent();
+ //add tray icon
+ try
+ {
+ _notifyIcon = new System.Windows.Forms.NotifyIcon();
+ _notifyIcon.Text = this.Title;
+ _notifyIcon.Icon = new System.Drawing.Icon("TodoTouch_512.ico");
+ _notifyIcon.Visible = true;
+ _notifyIcon.DoubleClick += (sender, args) => HideUnHideWindow();
+ }
+ catch (Exception ex)
+ {
+ var msg = "Error create tray icon";
+ Log.Error(msg, ex);
+ }
+ //add global key
+ try
+ {
+ _hotkey = new HotKey(ModifierKeys.Windows | ModifierKeys.Alt, System.Windows.Forms.Keys.T, this);
+ _hotkey.HotKeyPressed += (k) => HideUnHideWindow();
+ }
+ catch (Exception ex)
+ {
+ var msg = "Error Global HotKey Registered";
+ Log.Error(msg, ex);
+ MessageBox.Show(ex.Message, msg, MessageBoxButton.OK);
+ }
webBrowser1.Navigate("about:blank");
@@ -103,10 +132,45 @@ public MainWindow()
ThreadPool.QueueUserWorkItem(x => CheckForUpdates());
}
+ #region protected methods
- #region private methods
+ protected override void OnStateChanged(EventArgs e)
+ {
+ if (WindowState == WindowState.Minimized)
+ this.Hide();
- private void Reload()
+ base.OnStateChanged(e);
+ }
+
+
+
+ #region private methods
+
+ private void HideUnHideWindow()
+ {
+ if (this.WindowState == WindowState.Minimized)
+ {
+ this.Show();
+ this.Topmost = true;
+ this.Activate();
+ this.WindowState = WindowState.Normal;
+ }
+ else
+ {
+ this.WindowState = WindowState.Minimized;
+ this.Hide();
+ }
+ }
+
+ private void OnClose(object sender, System.ComponentModel.CancelEventArgs args)
+ {
+ _notifyIcon.Dispose();
+ _notifyIcon = null;
+ }
+
+ #endregion
+
+ private void Reload()
{
try
{
View
1 ToDoTests/ToDoTests.csproj
@@ -66,6 +66,7 @@
<EmbeddedResource Include="Data.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Data.Designer.cs</LastGenOutput>
+ <SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

0 comments on commit 978f0bc

Please sign in to comment.
Something went wrong with that request. Please try again.