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 190 191 192 193 194 195 196 197
| Imports System
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports System.Collections
''' <summary>
''' Description r�sum�e de Hotkey.
''' </summary>
Public Class HotKey
Inherits NativeWindow
Private Const WM_HOTKEY As Integer = 786
Private hotkeys As ArrayList
Private owner As Form
Public Sub New(ByVal owner As Form)
MyBase.New()
Me.AssignHandle(owner.Handle)
Me.owner = owner
AddHandler owner.HandleCreated, AddressOf Me.owner_HandleCreated
hotkeys = New ArrayList
End Sub
''' <summary>
''' Destructeur de la classe.
''' Il faut lib�rer les raccourcis cr��s
''' </summary>
Private Sub New()
MyBase.New()
Dim hkData As HotKeyData
Dim i As Integer = (hotkeys.Count - 1)
Do While (i >= 0)
hkData = CType(hotkeys(i), HotKeyData)
Me.UnregisterHotKey(hkData)
i = (i - 1)
Loop
End Sub
Public Event HotKeyPress As HotKeyHandler
Public Sub RegisterHotKey_1(ByVal key As Keys, ByVal modifiers As HotKeyModifiers)
If Not FindHotKey(key, modifiers).Equals(HotKeyData.Empty) Then
Me.RegisterHotKey(key, modifiers)
End If
End Sub
Public Overloads Sub UnregisterHotKey(ByVal key As Keys, ByVal modifiers As HotKeyModifiers)
Dim hkData As HotKeyData = FindHotKey(key, modifiers)
If Not hkData.Equals(HotKeyData.Empty) Then
UnregisterHotKey(hkData)
hotkeys.Remove(hkData)
End If
End Sub
Private Function FindHotKey(ByVal key As Keys, ByVal modifiers As HotKeyModifiers) As HotKeyData
Dim hkData As HotKeyData
Dim i As Integer = 0
Do While (i < Me.hotkeys.Count)
hkData = CType(hotkeys(i), HotKeyData)
If ((hkData.Key = key) _
AndAlso (hkData.Modifiers = modifiers)) Then
Return hkData
End If
i = (i + 1)
Loop
Return HotKeyData.Empty
End Function
Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
Dim hkData As HotKeyData
If (m.Msg = WM_HOTKEY) Then
Dim i As Integer = 0
Do While (i < hotkeys.Count)
hkData = CType(hotkeys(i), HotKeyData)
If hkData.Equals(m.WParam) Then
If (Not (Me.owner) Is Nothing) Then
RaiseEvent HotKeyPress(Me.owner, New HotKeyArgs(hkData.Key, hkData.Modifiers))
End If
'TODO: Warning!!! break;If
End If
i = (i + 1)
Loop
End If
End Sub
Private Declare Function GlobalAddAtom Lib "kernel32.dll" (ByVal lpString As String) As IntPtr
Private Declare Function GlobalDeleteAtom Lib "kernel32.dll" (ByVal nAtom As IntPtr) As IntPtr
Private Overloads Declare Function RegisterHotKey Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Boolean
Private Overloads Declare Function UnregisterHotKey Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal id As Integer) As Boolean
Private Overloads Sub registerHotKey(ByVal key As Keys, ByVal modifiers As HotKeyModifiers)
Dim atomName As String = String.Empty
Dim atomId As IntPtr
atomName = (key.ToString _
+ (modifiers.ToString + Environment.TickCount.ToString))
If (atomName.Length > 255) Then
atomName = atomName.Substring(0, 255)
End If
atomId = GlobalAddAtom(atomName)
If (atomId = IntPtr.Zero) Then
Throw New Exception("Impossible d'enregistrer l'atome du raccourci !")
End If
If Not RegisterHotKey(Me.Handle, atomId.ToInt32, CType(modifiers, Integer), CType(key, Integer)) Then
GlobalDeleteAtom(atomId)
Throw New Exception("Impossible d'enregistrer le raccourci !")
End If
Me.hotkeys.Add(New HotKeyData(key, modifiers, atomId))
End Sub
Private Overloads Sub unregisterHotKey(ByVal hk As HotKeyData)
UnregisterHotKey(Me.Handle, hk.AtomID.ToInt32)
GlobalDeleteAtom(hk.AtomID)
End Sub
Private Sub owner_HandleCreated(ByVal sender As Object, ByVal e As EventArgs)
Me.AssignHandle(owner.Handle)
End Sub
''' <summary>
''' HotKeyModifiers �num�ration
''' </summary>
<Flags()> _
Public Enum HotKeyModifiers As Integer
Alt = 1
Control = 2
Shift = 4
Win = 8
End Enum
Private Structure HotKeyData
Public Key As Keys
Public Modifiers As HotKeyModifiers
Public AtomID As IntPtr
Public Shared Empty As HotKeyData = New HotKeyData
Public Sub New(ByVal key As Keys, ByVal modifiers As HotKeyModifiers, ByVal atomId As IntPtr)
Me.Key = key
Me.Modifiers = modifiers
Me.AtomID = atomId
End Sub
Public Overrides Function ToString() As String
Return (Modifiers.ToString + ("+" + Key.ToString))
End Function
Public Overrides Function GetHashCode() As Integer
Return Me.GetHashCode
End Function
Public Overrides Function Equals(ByVal obj As Object) As Boolean
Return Me.AtomID.Equals(obj)
End Function
End Structure
Public Delegate Sub HotKeyHandler(ByVal sender As Object, ByVal args As HotKeyArgs)
End Class
Public Class HotKeyArgs
Inherits EventArgs
Private key As Keys
Private modifiers As HotKey.HotKeyModifiers
Public Sub New(ByVal key As Keys, ByVal modifiers As HotKey.HotKeyModifiers)
MyBase.New()
Me.modifiers = modifiers
Me.key = key
End Sub
Public ReadOnly Property Get_Key() As Keys
Get
Return Key
End Get
End Property
Public ReadOnly Property Get_Modifiers() As HotKey.HotKeyModifiers
Get
Return Modifiers
End Get
End Property
End Class |
Partager