Hi Greg,
To do this, you must subclass then "Main" userform and use API functions as
follows :
For example, in a standard module:
Option Explicit
Declare Function SetWindowLong& Lib "user32" Alias _
"SetWindowLongA" (ByVal hWnd&, ByVal nIndex&, ByVal wNewWord&)
Declare Function RegisterHotKey& Lib "user32" (ByVal hWnd& _
, ByVal id&, ByVal fsModifiers&, ByVal vk&)
Declare Function UnregisterHotKey& Lib "user32" (ByVal hWnd&, ByVal id&)
Declare Function CallWindowProc& Lib "user32.dll" Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc&, ByVal hWnd&, ByVal Msg&, ByVal wParam&, ByVal
lParam&)
Public OldWinProc&
Public Const HOTKEY_ID& = 0
Public Const GWL_WNDPROC = (-4)
Function NewWinProc&(ByVal hWnd&, ByVal Msg&, ByVal wParam&, ByVal lParam&)
If Msg = &H82 Then ' WM_NCDESTROY
UnregisterHotKey hWnd, HOTKEY_ID
SetWindowLong hWnd, GWL_WNDPROC, OldWinProc
ElseIf Msg = &H312 Then ' WM_HOTKEY
Call Main.HotkeyExecute
End If
NewWinProc = CallWindowProc(OldWinProc, hWnd, Msg, wParam, lParam)
End Function
Sub Test()
Main.Show
End Sub
In the Main module:
Option Explicit
Private Declare Function FindWindow& Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName$, ByVal lpWindowName$)
Private Sub UserForm_Initialize()
Dim hWnd&: hWnd = FindWindow(vbNullString, Me.Caption)
' Register the hotkey.
If RegisterHotKey(hWnd, HOTKEY_ID, 0&, vbKeyF2) Then
' Subclass the UserForm to watch for WM_HOTKEY messages.
OldWinProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf NewWinProc)
End If
End Sub
Sub HotkeyExecute()
Me.Hide
Sales.Show
Me.Show
End Sub
Regards,
MP