Never used hooks before:
I'm using Excel. When The Excel window is minimized my Form is hidden.
What I want to do: When the Excel minimized icon is clicked, I need to show my Form again. There is no Application event
I can tap to show my form so I'm trying to use a Hook.
Here's what I have so far...
My Declarations in a mod:
My hook calling prodedure:VB Code:
Option Explicit Public Const HCBT_MINMAX = 1 Public Const WH_MAX As Long = 11 Public Const WH_CBT As Long = 5 Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long Public Declare Function GetCurrentThreadId Lib "kernel32.dll" () As Long Public hHook As Long
My Callback Procedure:VB Code:
Application.WindowState = xlMinimized ThreadId = GetCurrentThreadId() hHook = SetWindowsHookEx(WH_CBT, AddressOf RESIZEProc, 0, ThreadId)
In the procedure above MainHwnd is already set when the form is initilized.VB Code:
Public Sub RESIZEProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) 'if idHook is less than zero, no further processing is required If idHook < 0 Then 'call the next hook RESIZEProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam) Else If hHook = HCBT_MINMAX Then MsgBox "Message Was Sent To Minimize or Maximize The Window" End If If wParam = MainHwnd Then MsgBox "MainHwnd Was Passed" End If RESIZEProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam) End If End Sub
When using VBA you have to get your own handle using other API's because
there is no Me.Hwnd available. Therefore I have already set a global variable
called MainHwnd that contains the correct handle.
Problem, regardless if all of this is set up correctly, I can't run it yet because
when I compile the project I get "argument not optional" on RESIZProc in the following code:
VB Code:
If idHook < 0 Then 'call the next hook RESIZEProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam) Else
Can someone tell me if I'm headed in the right direction with this. My intention is to eventually use the
following code section as the location in which to show my form again.
VB Code:
If hHook = HCBT_MINMAX Then MsgBox "Message Was Sent To Minimize or Maximize The Window" End If




Reply With Quote