HotkeyHelper部分 :
using System;
using System.Collections.Generic;
using System.Text;
using GFSucker.Game.Utility;
namespace GFSucker.Game.Provider
{
public class HotkeyHelper : IDisposable
{
public const int MOD_ALT = 0x1;
public const int MOD_CONTROL = 0x2;
public const int MOD_SHIFT = 0x4;
public const int MOD_WIN = 0x8;
public const int WM_HOTKEY = 0x312;
private Dictionary dicHotkeys;
public HotkeyHelper()
{
dicHotkeys = new Dictionary();
}
public bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifIErs, uint vk)
{
if (dicHotkeys.ContainsKey(id))
{
if (dicHotkeys[id].IsRegistered)
{
UnregisterHotKey (dicHotkeys[id].HWnd, id); // unregister firstly
dicHotkeys [id].IsRegistered = false;
}
dicHotkeys[id].HWnd = hWnd; // set the new hwnd (normally should be the same)
}
else
{
dicHotkeys.Add(id, new Hotkey(hWnd, false));
}
dicHotkeys[id].IsRegistered = WinApi.RegisterHotKey(hWnd, id, fsModifIErs, vk);
return dicHotkeys[id].IsRegistered;
}
private bool UnregisterHotKey(IntPtr hWnd, int id)
{
return WinApi.UnregisterHotKey(hWnd, id);
}
#region Hotkey Information
class Hotkey
{
public Hotkey(IntPtr hWnd, bool isRegistered)
{
_HWnd = hWnd;
_IsRegistered = isRegistered;
}
private IntPtr _HWnd;
public IntPtr HWnd
{
get { return _HWnd; }
set { _HWnd = value; }
}
private bool _IsRegistered;
public bool IsRegistered
{
get { return _IsRegistered; }
set { _IsRegistered = value; }
}
}
#endregion
#region IDisposable 成員
public void Dispose()
{
foreach (int id in dicHotkeys.Keys)
{
if (dicHotkeys[id].IsRegistered)
{
UnregisterHotKey(dicHotkeys[id].HWnd, id);
}
}
}
#endregion
}
}