1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
| * using System;
* using System.Windows.Forms;
* using System.Runtime.InteropServices;
* using System.Collections;
*
* namespace CSUtils.Classes
* {
* /// <summary>
* /// Description résumée de Hotkey.
* /// </summary>
* public class HotKey : NativeWindow
* {
* private const int WM_HOTKEY = 0x312;
*
* /// <summary>
* /// HotKeyModifiers énumération
* /// </summary>
* [Flags]
* public enum HotKeyModifiers : int {
* Alt = 0x1,
* Control = 0x2,
* Shift = 0x4,
* Win = 0x8
* }
*
* private struct HotKeyData {
* public Keys Key;
* public HotKeyModifiers Modifiers;
* public IntPtr AtomID;
*
* public static HotKeyData Empty = new HotKeyData();
*
* public HotKeyData(Keys key, HotKeyModifiers modifiers, IntPtr atomId) {
* this.Key = key;
* this.Modifiers = modifiers;
* this.AtomID = atomId;
* }
*
* public override string ToString() {
* return Modifiers.ToString() + "+" + Key.ToString();
* }
*
* public override int GetHashCode() {
* return base.GetHashCode ();
* }
*
* public override bool Equals(object obj) {
* return this.AtomID.Equals (obj);
* }
*
* }
*
*
* private ArrayList hotkeys;
* private Form owner;
*
* public delegate void HotKeyHandler(object sender, HotKeyArgs args);
* public event HotKeyHandler HotKeyPress;
*
* public HotKey(Form owner) {
* this.AssignHandle(owner.Handle);
* this.owner = owner;
* owner.HandleCreated += new EventHandler(owner_HandleCreated);
*
* hotkeys = new ArrayList();
* }
*
* public void RegisterHotKey(Keys key, HotKeyModifiers modifiers) {
* if (!FindHotKey(key, modifiers).Equals(HotKeyData.Empty)) {
* this.registerHotKey(key, modifiers);
* }
* }
*
* public void UnregisterHotKey(Keys key, HotKeyModifiers modifiers) {
* HotKeyData hkData = FindHotKey(key, modifiers);
*
* if (!hkData.Equals(HotKeyData.Empty)) {
* unregisterHotKey(hkData);
* hotkeys.Remove(hkData);
* }
* }
*
* private HotKeyData FindHotKey(Keys key, HotKeyModifiers modifiers) {
* HotKeyData hkData;
* for (int i=0; i<this.hotkeys.Count; i++) {
* hkData = (HotKeyData)hotkeys[i];
* if (hkData.Key == key && hkData.Modifiers == modifiers) {
* return hkData;
* }
* }
*
* return HotKeyData.Empty;
* }
*
* protected override void WndProc(ref Message m) {
* base.WndProc (ref m);
* HotKeyData hkData;
* if (m.Msg == WM_HOTKEY) {
* for (int i=0; i<hotkeys.Count; i++) {
* hkData = (HotKeyData)hotkeys[i];
* if (hkData.Equals(m.WParam)) {
* if (this.HotKeyPress != null) {
* HotKeyPress(this.owner,
* new HotKeyArgs(hkData.Key, hkData.Modifiers));
* }
* break;
* }
* }
* }
* }
*
*
* /// <summary>
* /// Destructeur de la classe.
* /// Il faut libérer les raccourcis créés
* /// </summary>
* ~HotKey() {
* HotKeyData hkData;
* for (int i=hotkeys.Count-1; i>=0; i--) {
* hkData = (HotKeyData)hotkeys[i];
* this.unregisterHotKey(hkData);
* }
* }
*
* #region P/Invoke
* [DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
* private static extern IntPtr GlobalAddAtom(string lpString);
*
* [DllImport("kernel32.dll", SetLastError=true, ExactSpelling=true)]
* private static extern IntPtr GlobalDeleteAtom(IntPtr nAtom);
*
* [DllImport("user32.dll")]
* private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
*
* [DllImport("user32.dll")]
* private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
* #endregion
*
* private void registerHotKey(Keys key, HotKeyModifiers modifiers) {
* string atomName = string.Empty;
* IntPtr atomId;
* atomName = key.ToString() + modifiers.ToString() + Environment.TickCount.ToString();
* if (atomName.Length > 255) {
* atomName = atomName.Substring(0,255);
* }
*
* atomId = GlobalAddAtom(atomName);
* if (atomId == IntPtr.Zero) {
* throw new Exception("Impossible d'enregistrer l'atome du raccourci !");
* }
*
* if (!RegisterHotKey(this.Handle, atomId.ToInt32(), (int)modifiers, (int)key)) {
* GlobalDeleteAtom(atomId);
* throw new Exception("Impossible d'enregistrer le raccourci !");
* }
*
* this.hotkeys.Add(new HotKeyData(key, modifiers, atomId));
* }
*
* private void unregisterHotKey(HotKeyData hk) {
* UnregisterHotKey(this.Handle, hk.AtomID.ToInt32());
* GlobalDeleteAtom(hk.AtomID);
* }
*
*
* private void owner_HandleCreated(object sender, EventArgs e) {
* this.AssignHandle(owner.Handle);
* }
* }
*
* #region Classe HotKeyArgs
* public class HotKeyArgs : EventArgs {
* public HotKeyArgs(Keys key, HotKey.HotKeyModifiers modifiers) {
* this.modifiers = modifiers;
* this.key = key;
* }
*
* private Keys key;
* public Keys Key {
* get {return key;}
* }
*
* private HotKey.HotKeyModifiers modifiers;
* public HotKey.HotKeyModifiers Modifiers {
* get {return modifiers;}
* }
* }
* #endregion
* } |