API calls, an Event handler, custom menu bar and modal

D

DrivenByHim

In a perfect world, Excel VBA forms would include mouse scroll wheel
functionality. Unfortunately, Excel VBA does not process the WM_MOUSEWHEEL.
I had to write my own event handler using calls to user32.dll.

The problem is, I can't seem to combine "hooking" my event handler (by
making calls to SetWindowLong with the GWL_WNDPROC index) AND making calls to
customize the window (by making calls to SetWindowLong with the GWL_STYLE
index as well as some other basic calls to SetWindowPos).

On top of that, if the excel form is set to "modal" = false, my event
handler seems to be spammed with messages that it won't send off to it's
previous windows handler (Excel). Thus, I cannot keep the excel application
minimized and have my form appear as though it is a separate application.

The event handler is as follows in modified code: (Please assume that all
variables and constants are declared correctly, and that the code is working)

Private Function Fnc_EventHandler(ByVal Lwnd As Long, _
ByVal Lmsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Dim MouseKeys As Long
Dim Rotation As Long

Select Case Lmsg

'Handle mouse wheel messages
Case WM_MOUSEWHEEL

MouseKeys = wParam And 65535
Rotation = wParam / 65536

'MouseWheel function
Call MouseWheelHandler(Rotation)

'Pass everything else back to excel for processing
Case Else

Fnc_EventHandler = CallWindowProc(g_LocalPrevWndProc, Lwnd,
Lmsg, wParam, lParam)

End Select

End Function

Here is my hook in modified code:

Public Sub Sub_Hook(ByRef FormMain As UserForm, _
ByRef c_Main_ParamRef As My_Main_Class)

Dim WindowStyle As Long

Set g_Form_Main = FormMain
Set c_Main_ParamRef = My_Main_Class

g_LocalHwnd = FindWindow("ThunderdFrame", g_Form_Main.Caption)

g_LocalPrevWndProc = SetWindowLong(g_LocalHwnd, GWL_WNDPROC, AddressOf
Fnc_EventHandler)

'More calls to user32.dll, only my event handler OR the following code
works, not both! The following code adds my form to the taskbar as well as
adds a minimmize button

g_ReturnResult = GetWindowLong(g_LocalHwnd, GWL_STYLE)

If (g_ReturnResult And &H20000) = 0 Then

Call SetWindowLong(g_LocalHwnd, GWL_STYLE, g_ReturnResult Or
WS_MINIMIZEBOX)

End If

WindowStyle = GetWindowLong(g_LocalHwnd, GWL_EXSTYLE)

WindowStyle = WindowStyle Or WS_EX_APPWINDOW

g_ReturnResult = SetWindowPos(g_LocalHwnd, HWND_TOP, _
0, 0, _
0, 0, _
SWP_NOMOVE Or SWP_NOSIZE Or
SWP_NOACTIVATE Or SWP_HIDEWINDOW)

g_ReturnResult = SetWindowLong(g_LocalHwnd, GWL_EXSTYLE, lint_WindowStyle)

g_ReturnResult = SetWindowPos(g_LocalHwnd, HWND_TOP, _
0, 0, _
0, 0, _
SWP_NOMOVE Or SWP_NOSIZE Or
SWP_NOACTIVATE Or SWP_SHOWWINDOW)

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top