程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#的Hotkey簡單封裝(2)

C#的Hotkey簡單封裝(2)

編輯:關於C語言

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
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved