TreeView (JKP) Mouse Scrolling - Make it work with this code

sijpie

Well-known Member
Joined
Nov 1, 2008
Messages
3,734
Not a question, just some code to improve your project(s)


Jan-Karel Pieterse and Peter Thornton have some time ago developed a very nice alternative to the standard VBA TreeView, which overcomes all the issues with 32bit and 64 bit Windows and Office and looks nicer as well. And it is FREE.
An MSForms (all VBA) treeview

However the code as provided by Jan Karel and Peter does not cater for mouse scrolling; ie if your tree view extends outside the frame provided then scroll bars appear but you will need to click on the scroll bar to move the tree in the frame (The treeview is held in a Frame control).

Providing code to use the mouse scroll button means hooking into the mouse events using API codes. Peter Thornton provided a nice piece of code to scroll a combobox or listbox (http://social.msdn.microsoft.com/Fo...n-userform-listbox-in-excel-2010?forum=isvvba).
With the hard work done it was easier to adapt this to scrolling within a frame.


Note!!! Adding this code can affect the stability of your project, so test it in depth.




The code that intercepts the mouse events and tells the Frame what to do is as follows and should be stored in a standard code module.


<font face=Calibri><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><SPAN style="color:#007F00">' >>>>  This code to be in a Normal Code Module (for instance modMouseScroll) <<<<</SPAN><br><br> <SPAN style="color:#007F00">' Based on code from Peter Thornton here:</SPAN><br> <SPAN style="color:#007F00">' http://social.msdn.microsoft.com/Forums/en-US/7d584120-a929-4e7c-9ec2-9998ac639bea/mouse-scroll-in-userform-listbox-in-excel-2010?forum=isvvba</SPAN><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Type</SPAN> POINTAPI<br>    X                               <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    Y                               <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Type</SPAN><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Type</SPAN> MOUSEHOOKSTRUCT<br>    pt                              <SPAN style="color:#00007F">As</SPAN> POINTAPI<br>    hwnd                            <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    wHitTestCode                    <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    dwExtraInfo                     <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Type</SPAN><br> <br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> GetWindowLong <SPAN style="color:#00007F">Lib</SPAN> "user32.dll" _<br>    Alias "GetWindowLongA" ( _<br>    <SPAN style="color:#00007F">ByVal</SPAN> hwnd <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>    <SPAN style="color:#00007F">ByVal</SPAN> nIndex <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br> <br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> SetWindowsHookEx <SPAN style="color:#00007F">Lib</SPAN> "user32" _<br>    Alias "SetWindowsHookExA" ( _<br>    <SPAN style="color:#00007F">ByVal</SPAN> idHook <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>    <SPAN style="color:#00007F">ByVal</SPAN> lpfn <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>    <SPAN style="color:#00007F">ByVal</SPAN> hmod <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>    <SPAN style="color:#00007F">ByVal</SPAN> dwThreadId <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br> <br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> CallNextHookEx <SPAN style="color:#00007F">Lib</SPAN> "user32" ( _<br>    <SPAN style="color:#00007F">ByVal</SPAN> hHook <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>    <SPAN style="color:#00007F">ByVal</SPAN> nCode <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>    <SPAN style="color:#00007F">ByVal</SPAN> wParam <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>    lParam <SPAN style="color:#00007F">As</SPAN> Any) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br> <br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> UnhookWindowsHookEx <SPAN style="color:#00007F">Lib</SPAN> "user32" ( _<br>    <SPAN style="color:#00007F">ByVal</SPAN> hHook <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br> <br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> WindowFromPoint <SPAN style="color:#00007F">Lib</SPAN> "user32" ( _<br>    <SPAN style="color:#00007F">ByVal</SPAN> xPoint <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>    <SPAN style="color:#00007F">ByVal</SPAN> yPoint <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br> <br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> GetCursorPos <SPAN style="color:#00007F">Lib</SPAN> "user32.dll" ( _<br>    <SPAN style="color:#00007F">ByRef</SPAN> lpPoint <SPAN style="color:#00007F">As</SPAN> POINTAPI) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> GetActiveWindow <SPAN style="color:#00007F">Lib</SPAN> "user32" () <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br> <br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Const</SPAN> WH_MOUSE_LL          <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> = 14<br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Const</SPAN> WM_MOUSEWHEEL        <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> = &H20A<br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Const</SPAN> HC_ACTION            <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> = 0<br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Const</SPAN> GWL_HINSTANCE        <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> = (-6)<br> <br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Const</SPAN> cSCROLLCHANGE        <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> = 20<br> <br><SPAN style="color:#00007F">Private</SPAN> mLngMouseHook              <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><SPAN style="color:#00007F">Private</SPAN> mFrameHwnd                 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><SPAN style="color:#00007F">Private</SPAN> mbHook                     <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN><br><SPAN style="color:#00007F">Private</SPAN> mCtl                       <SPAN style="color:#00007F">As</SPAN> MSForms.Control<br><SPAN style="color:#00007F">Dim</SPAN> n                              <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br> <br> <br><br><SPAN style="color:#00007F">Sub</SPAN> HookFrameScroll(frm <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>, ctl <SPAN style="color:#00007F">As</SPAN> MSForms.Control)<br>    <SPAN style="color:#00007F">Dim</SPAN> lngAppInst <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> hwndUnderCursor <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> tPT <SPAN style="color:#00007F">As</SPAN> POINTAPI<br>     GetCursorPos tPT<br>     hwndUnderCursor = WindowFromPoint(tPT.X, tPT.Y)<br>     <SPAN style="color:#007F00">'>> uncomment below if you want to give the treeview focus once the user scrolls it</SPAN><br><SPAN style="color:#007F00">'     If Not frm.ActiveControl Is ctl Then</SPAN><br><SPAN style="color:#007F00">'             ctl.SetFocus</SPAN><br><SPAN style="color:#007F00">'     End If</SPAN><br>     <SPAN style="color:#00007F">If</SPAN> mFrameHwnd <> hwndUnderCursor <SPAN style="color:#00007F">Then</SPAN><br>             UnhookFrameScroll<br>             <SPAN style="color:#00007F">Set</SPAN> mCtl = ctl<br>             mFrameHwnd = hwndUnderCursor<br>             lngAppInst = GetWindowLong(mFrameHwnd, GWL_HINSTANCE)<br>             <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> mbHook <SPAN style="color:#00007F">Then</SPAN><br>                     mLngMouseHook = SetWindowsHookEx( _<br>                        WH_MOUSE_LL, AddressOf FrameMouseProc, lngAppInst, 0)<br>                     mbHook = mLngMouseHook <> 0<br>             <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>     <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> UnhookFrameScroll()<br>     <SPAN style="color:#00007F">If</SPAN> mbHook <SPAN style="color:#00007F">Then</SPAN><br>                <SPAN style="color:#00007F">Set</SPAN> mCtl = <SPAN style="color:#00007F">Nothing</SPAN><br>             UnhookWindowsHookEx mLngMouseHook<br>             mLngMouseHook = 0<br>             mFrameHwnd = 0<br>             mbHook = <SPAN style="color:#00007F">False</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Function</SPAN> FrameMouseProc( _<br>            <SPAN style="color:#00007F">ByVal</SPAN> nCode <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> wParam <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>            <SPAN style="color:#00007F">ByRef</SPAN> lParam <SPAN style="color:#00007F">As</SPAN> MOUSEHOOKSTRUCT) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> errH <SPAN style="color:#007F00">'Resume Next</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> (nCode = HC_ACTION) <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#00007F">If</SPAN> WindowFromPoint(lParam.pt.X, lParam.pt.Y) = mFrameHwnd <SPAN style="color:#00007F">Then</SPAN><br>            <SPAN style="color:#00007F">If</SPAN> wParam = WM_MOUSEWHEEL <SPAN style="color:#00007F">Then</SPAN><br>                FrameMouseProc = <SPAN style="color:#00007F">True</SPAN><br>                <SPAN style="color:#00007F">If</SPAN> lParam.hwnd > 0 <SPAN style="color:#00007F">Then</SPAN><br>                    mCtl.ScrollTop = Application.Max(0, mCtl.ScrollTop - cSCROLLCHANGE)<br>                <SPAN style="color:#00007F">Else</SPAN><br>                    mCtl.ScrollTop = Application.Min(mCtl.ScrollHeight - mCtl.InsideHeight, mCtl.ScrollTop + cSCROLLCHANGE)<br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>                <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Function</SPAN><br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>         <br>    <SPAN style="color:#00007F">End</SPAN> If<br>    FrameMouseProc = CallNextHookEx( _<br>    mLngMouseHook, nCode, wParam, <SPAN style="color:#00007F">ByVal</SPAN> lParam)<br>    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Function</SPAN><br>errH:<br>    UnhookFrameScroll<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN><br><br></FONT>



Then we need to have the frame process the mouseover events. This can be done two ways
1. Add the code to the clsTreeView class
2. Add the code to the userform module


Don't do both!


1. Add the code to the clsTreeView class
Advantage: your treeviews in any form will have automatic scroll capability
Disadvantage: when updating the classmodules with a new version, you will need to remember to copy this code. so mark it.


Add the following code to the class module, I put it (about halfway) after the TreeControl_Click() sub



<font face=Calibri><SPAN style="color:#007F00">' This Sub needs to be added to the clsTreeView class module</SPAN><br><SPAN style="color:#007F00">' <<<  Added for mouse scrolling >>></SPAN><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> TreeControl_MouseMove(<SPAN style="color:#00007F">ByVal</SPAN> Button <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> Shift <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> X <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Single</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> Y <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Single</SPAN>)<br>    <SPAN style="color:#007F00">' intercept the mouse move events and use it to scroll the treeview</SPAN><br>    HookFrameScroll Me.TreeControl.Parent, TreeControl<br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>



2. Add the code to the userform module
Advantage: no messing with the class module
Disadvantage: need to add it for every treeview used


Add the following code to the Userform module where the treeview is. I have kept the framename as used in the Treeview Demo.


<font face=Calibri><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> frTreeControl_MouseMove(<SPAN style="color:#00007F">ByVal</SPAN> Button <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> Shift <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> X <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Single</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> Y <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Single</SPAN>)<br>    <SPAN style="color:#007F00">' intercept the mouse move events and use it to scroll the treeview</SPAN><br>    HookFrameScroll Me, frTreeControl<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>


That's it. It's wonderful to see it work.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

sijpie

Well-known Member
Joined
Nov 1, 2008
Messages
3,734
OOps, I forgot one crucial ingredient:


Lastly we need to close any open hooks when we close the form. In the userform module add:




<font face=Calibri><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> UserForm_QueryClose(Cancel <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, CloseMode <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>)<br>    UnhookListBoxScroll<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 

filipejrc

New Member
Joined
Nov 13, 2018
Messages
9
Hello Sijpie.
I know this thread goes back to 2015, but I wonder is it is possible do adapt the code to MS Access.
I've been trying, but did not managed to overcome a lot of erros that were raised.

Hope someone could help.

Best regards,
Filipe Caetano
Portugal
 

filipejrc

New Member
Joined
Nov 13, 2018
Messages
9

ADVERTISEMENT

Thank you for replying.
Yes I'm using JKP Treeview in Access.
I know it is designed to be built on an userform but with the help of Peter Thornton and Jan Karel I've been able to implement the treeview with relative success.
Do you know how can I change and what shall be changed on your code so it can be used in MS Access?

Sorry for my poor english :)
 

sijpie

Well-known Member
Joined
Nov 1, 2008
Messages
3,734
Filipe,

I can remember I had a lot of issues initially. I just checked my code. I notice I am not using the code in the class module, but in the code for the userform I have a sub for each frame where I want to use the mouse scroll.

But I also notice that I have disabled scrolling altogether by setting a compiletime flag. So maybe in the end I did not get it to work properly either.
Anyway, I cannot test my program on my work PC, so it will have to wait over the weekend when I am at home.
 

filipejrc

New Member
Joined
Nov 13, 2018
Messages
9

ADVERTISEMENT

Thank you very much.
I will wait anxiously and hoping that you can help me with this
.

Best regards
 

sijpie

Well-known Member
Joined
Nov 1, 2008
Messages
3,734
Hi Filipe, I notice I made a lot of changes to the code after i posted it here! For starters I split up the scroll function for form scroll, listbox scroll and frame scrolling. Then I made some fundamental changes to parts of code. I will post the code below, and hopefully we can make it work in your case as well.
 

sijpie

Well-known Member
Joined
Nov 1, 2008
Messages
3,734
OK, after having had some issues with the code I posted at the top of this thread, I made some significant changes to the code. I will post the new code here and try to guide you through how to use it.

This code will make forms, frames and listboxes scrollable with the mouse wheel. But it hooks into the Windows operating system, and so it could get some strange results when not properly implemented. It is very important that each time it hooks into the system, it also gets unhooked!


====================================

To do this properly requires the following:
  • A code module renamed to MouseScroll. This module will hold the public and private functions to make the various controls scrollable.
  • Code in the Form module for EACH control that you want to have mouse scrolling. (And yes, a form is a control itself)


Create a new code module and rename it MouseScroll. In this code module Mousescroll paste the following code:

<font face=Calibri><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br><SPAN style="color:#007F00">''////////////////////////////////////////////////////////////////////'</SPAN><br><SPAN style="color:#007F00">''</SPAN><br><SPAN style="color:#007F00">'' Uncomment below to disable mousescroll on form for debugging       '</SPAN><br><SPAN style="color:#007F00">''</SPAN><br><SPAN style="color:#007F00">''\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'</SPAN><br><SPAN style="color:#007F00">'Public Const bNOMOUSEDebug As Boolean = True</SPAN><br><SPAN style="color:#007F00">''\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'</SPAN><br><br><br> <SPAN style="color:#007F00">' Based on code from Peter Thornton here:</SPAN><br> <SPAN style="color:#007F00">' http://social.msdn.microsoft.com/Forums/en-US/7d584120-a929-4e7c-9ec2-9998ac639bea/mouse-scroll-in-userform-listbox-in-excel-2010?forum=isvvba</SPAN><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Type</SPAN> POINTAPI<br>    X                               <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    Y                               <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Type</SPAN><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Type</SPAN> MOUSEHOOKSTRUCT<br>    pt                              <SPAN style="color:#00007F">As</SPAN> POINTAPI<br>    hwnd                            <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    wHitTestCode                    <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    dwExtraInfo                     <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Type</SPAN><br> <br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> FindWindow <SPAN style="color:#00007F">Lib</SPAN> "user32" _<br>    Alias "FindWindowA" ( _<br>    <SPAN style="color:#00007F">ByVal</SPAN> lpClassName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, _<br>    <SPAN style="color:#00007F">ByVal</SPAN> lpWindowName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br> <br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> GetWindowLong <SPAN style="color:#00007F">Lib</SPAN> "user32.dll" _<br>    Alias "GetWindowLongA" ( _<br>    <SPAN style="color:#00007F">ByVal</SPAN> hwnd <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>    <SPAN style="color:#00007F">ByVal</SPAN> nIndex <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br> <br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> SetWindowsHookEx <SPAN style="color:#00007F">Lib</SPAN> "user32" _<br>    Alias "SetWindowsHookExA" ( _<br>    <SPAN style="color:#00007F">ByVal</SPAN> idHook <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>    <SPAN style="color:#00007F">ByVal</SPAN> lpfn <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>    <SPAN style="color:#00007F">ByVal</SPAN> hmod <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>    <SPAN style="color:#00007F">ByVal</SPAN> dwThreadId <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br> <br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> CallNextHookEx <SPAN style="color:#00007F">Lib</SPAN> "user32" ( _<br>    <SPAN style="color:#00007F">ByVal</SPAN> hHook <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>    <SPAN style="color:#00007F">ByVal</SPAN> nCode <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>    <SPAN style="color:#00007F">ByVal</SPAN> wParam <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>    lParam <SPAN style="color:#00007F">As</SPAN> Any) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br> <br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> UnhookWindowsHookEx <SPAN style="color:#00007F">Lib</SPAN> "user32" ( _<br>    <SPAN style="color:#00007F">ByVal</SPAN> hHook <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br> <br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> WindowFromPoint <SPAN style="color:#00007F">Lib</SPAN> "user32" ( _<br>    <SPAN style="color:#00007F">ByVal</SPAN> xPoint <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>    <SPAN style="color:#00007F">ByVal</SPAN> yPoint <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br> <br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> GetCursorPos <SPAN style="color:#00007F">Lib</SPAN> "user32.dll" ( _<br>    <SPAN style="color:#00007F">ByRef</SPAN> lpPoint <SPAN style="color:#00007F">As</SPAN> POINTAPI) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> GetActiveWindow <SPAN style="color:#00007F">Lib</SPAN> "user32" () <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br> <br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Const</SPAN> WH_MOUSE_LL          <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> = 14<br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Const</SPAN> WM_MOUSEWHEEL        <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> = &H20A<br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Const</SPAN> HC_ACTION            <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> = 0<br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Const</SPAN> GWL_HINSTANCE        <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> = (-6)<br> <br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Const</SPAN> cSCROLLCHANGE        <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> = 24<br> <br><SPAN style="color:#00007F">Private</SPAN> mLngFoMouseHook            <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><SPAN style="color:#00007F">Private</SPAN> mLngLiMouseHook            <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><SPAN style="color:#00007F">Private</SPAN> mLngFrMouseHook            <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><SPAN style="color:#00007F">Private</SPAN> mFormHwnd                  <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><SPAN style="color:#00007F">Private</SPAN> mListBoxHwnd               <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><SPAN style="color:#00007F">Private</SPAN> mFrameHwnd                 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><SPAN style="color:#00007F">Private</SPAN> mbFoHook                   <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN><br><SPAN style="color:#00007F">Private</SPAN> mbLiHook                   <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN><br><SPAN style="color:#00007F">Private</SPAN> mbFrHook                   <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> mForm                          <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN><br><SPAN style="color:#00007F">Private</SPAN> mFoCtl                     <SPAN style="color:#00007F">As</SPAN> MSForms.Control<br><SPAN style="color:#00007F">Private</SPAN> mLiCtl                     <SPAN style="color:#00007F">As</SPAN> MSForms.Control<br><SPAN style="color:#00007F">Private</SPAN> mFrCtl                     <SPAN style="color:#00007F">As</SPAN> MSForms.Control<br><SPAN style="color:#00007F">Dim</SPAN> n                              <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br> <br> <br><SPAN style="color:#00007F">Sub</SPAN> HookFormScroll(oForm <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>)<br>    <SPAN style="color:#00007F">Dim</SPAN> lngAppInst                  <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> hwndUnderCursor             <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><br>    <SPAN style="color:#00007F">Set</SPAN> mForm = oForm<br>    hwndUnderCursor = FindWindow("ThunderDFrame", oForm.Caption)<br><SPAN style="color:#007F00">'    Debug.Print "Form window: " & hwndUnderCursor</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> mFormHwnd <> hwndUnderCursor <SPAN style="color:#00007F">Then</SPAN><br>        UnhookFormScroll<br><SPAN style="color:#007F00">'        Debug.Print "Unhook old proc"</SPAN><br>        mFormHwnd = hwndUnderCursor<br>        lngAppInst = GetWindowLong(mFormHwnd, GWL_HINSTANCE)<br>        <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> mbFoHook <SPAN style="color:#00007F">Then</SPAN><br>            mLngFoMouseHook = SetWindowsHookEx( _<br>                WH_MOUSE_LL, AddressOf FormMouseProc, lngAppInst, 0)<br>            mbFoHook = mLngFoMouseHook <> 0<br><SPAN style="color:#007F00">'            If mbFoHook Then Debug.Print "Form hooked"</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> UnhookFormScroll()<br>    <SPAN style="color:#00007F">If</SPAN> mbFoHook <SPAN style="color:#00007F">Then</SPAN><br>        UnhookWindowsHookEx mLngFoMouseHook<br>        mLngFoMouseHook = 0<br>        mFormHwnd = 0<br>        mbFoHook = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    Debug.Print "Unhook formscroll"<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><br><SPAN style="color:#00007F">Sub</SPAN> HookListBoxScroll(frm <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>, ctl <SPAN style="color:#00007F">As</SPAN> MSForms.Control)<br>    <SPAN style="color:#00007F">Dim</SPAN> lngAppInst <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> hwndUnderCursor <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> tPT <SPAN style="color:#00007F">As</SPAN> POINTAPI<br>     GetCursorPos tPT<br>     hwndUnderCursor = WindowFromPoint(tPT.X, tPT.Y)<br>     <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> frm.ActiveControl <SPAN style="color:#00007F">Is</SPAN> ctl <SPAN style="color:#00007F">Then</SPAN><br>             ctl.SetFocus<br>     <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>     Debug.Print tPT.X & ", " & tPT.Y<br>     <SPAN style="color:#00007F">If</SPAN> mListBoxHwnd <> hwndUnderCursor <SPAN style="color:#00007F">Then</SPAN><br>             UnhookListBoxScroll<br>             <SPAN style="color:#00007F">Set</SPAN> mLiCtl = ctl<br>             mListBoxHwnd = hwndUnderCursor<br>             lngAppInst = GetWindowLong(mListBoxHwnd, GWL_HINSTANCE)<br>             <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> mbLiHook <SPAN style="color:#00007F">Then</SPAN><br>                     mLngLiMouseHook = SetWindowsHookEx( _<br>                        WH_MOUSE_LL, AddressOf ListMouseProc, lngAppInst, 0)<br>                     mbLiHook = mLngLiMouseHook <> 0<br>             <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>     <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> UnhookListBoxScroll()<br>     <SPAN style="color:#00007F">If</SPAN> mbLiHook <SPAN style="color:#00007F">Then</SPAN><br>                <SPAN style="color:#00007F">Set</SPAN> mLiCtl = <SPAN style="color:#00007F">Nothing</SPAN><br>             UnhookWindowsHookEx mLngLiMouseHook<br>             mLngLiMouseHook = 0<br>             mListBoxHwnd = 0<br>             mbLiHook = <SPAN style="color:#00007F">False</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><br><SPAN style="color:#00007F">Sub</SPAN> HookFrameScroll(frm <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>, ctl <SPAN style="color:#00007F">As</SPAN> MSForms.Control)<br>    <SPAN style="color:#00007F">Dim</SPAN> lngAppInst <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> hwndUnderCursor <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> tPT <SPAN style="color:#00007F">As</SPAN> POINTAPI<br>     GetCursorPos tPT<br>     hwndUnderCursor = WindowFromPoint(tPT.X, tPT.Y)<br>     <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> frm.ActiveControl <SPAN style="color:#00007F">Is</SPAN> ctl <SPAN style="color:#00007F">Then</SPAN><br> <SPAN style="color:#007F00">'            ctl.SetFocus</SPAN><br>     <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>     <SPAN style="color:#00007F">If</SPAN> mFrameHwnd <> hwndUnderCursor <SPAN style="color:#00007F">Then</SPAN><br>             UnhookFrameScroll<br>             <SPAN style="color:#00007F">Set</SPAN> mFrCtl = ctl<br>             mFrameHwnd = hwndUnderCursor<br>             lngAppInst = GetWindowLong(mFrameHwnd, GWL_HINSTANCE)<br>             <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> mbFrHook <SPAN style="color:#00007F">Then</SPAN><br>                     mLngFrMouseHook = SetWindowsHookEx( _<br>                        WH_MOUSE_LL, AddressOf FrameMouseProc, lngAppInst, 0)<br>                     mbFrHook = mLngFrMouseHook <> 0<br>             <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>     <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> UnhookFrameScroll()<br>     <SPAN style="color:#00007F">If</SPAN> mbFrHook <SPAN style="color:#00007F">Then</SPAN><br>                <SPAN style="color:#00007F">Set</SPAN> mFrCtl = <SPAN style="color:#00007F">Nothing</SPAN><br>             UnhookWindowsHookEx mLngFrMouseHook<br>             mLngFrMouseHook = 0<br>             mFrameHwnd = 0<br>             mbFrHook = <SPAN style="color:#00007F">False</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Function</SPAN> FormMouseProc( _<br>    <SPAN style="color:#00007F">ByVal</SPAN> nCode <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> wParam <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>    <SPAN style="color:#00007F">ByRef</SPAN> lParam <SPAN style="color:#00007F">As</SPAN> MOUSEHOOKSTRUCT) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> errH <SPAN style="color:#007F00">'Resume Next</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> (nCode = HC_ACTION) <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#00007F">If</SPAN> GetActiveWindow = mFormHwnd <SPAN style="color:#00007F">Then</SPAN><br>             <br>            <SPAN style="color:#00007F">If</SPAN> wParam = WM_MOUSEWHEEL <SPAN style="color:#00007F">Then</SPAN><br>                FormMouseProc = <SPAN style="color:#00007F">True</SPAN><br>                <SPAN style="color:#00007F">If</SPAN> lParam.hwnd > 0 <SPAN style="color:#00007F">Then</SPAN><br>                    mForm.ScrollTop = Application.Max(0, mForm.ScrollTop - cSCROLLCHANGE)<br>                <SPAN style="color:#00007F">Else</SPAN><br>                    mForm.ScrollTop = Application.Min(mForm.ScrollHeight - mForm.InsideHeight, mForm.ScrollTop + cSCROLLCHANGE)<br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>                <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Function</SPAN><br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>         <br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    FormMouseProc = CallNextHookEx( _<br>    mLngFoMouseHook, nCode, wParam, <SPAN style="color:#00007F">ByVal</SPAN> lParam)<br>    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Function</SPAN><br>errH:<br>    UnhookFormScroll<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN><br><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Function</SPAN> ListMouseProc( _<br>             <SPAN style="color:#00007F">ByVal</SPAN> nCode <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> wParam <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>             <SPAN style="color:#00007F">ByRef</SPAN> lParam <SPAN style="color:#00007F">As</SPAN> MOUSEHOOKSTRUCT) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> idx <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> errH<br>    <SPAN style="color:#00007F">If</SPAN> (nCode = HC_ACTION) <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#00007F">If</SPAN> WindowFromPoint(lParam.pt.X, lParam.pt.Y) = mListBoxHwnd <SPAN style="color:#00007F">Then</SPAN><br>            <SPAN style="color:#00007F">If</SPAN> wParam = WM_MOUSEWHEEL <SPAN style="color:#00007F">Then</SPAN><br>                ListMouseProc = <SPAN style="color:#00007F">True</SPAN><br>                <SPAN style="color:#00007F">If</SPAN> lParam.hwnd > 0 <SPAN style="color:#00007F">Then</SPAN> idx = -1 <SPAN style="color:#00007F">Else</SPAN> idx = 1<br>                idx = idx + mLiCtl.ListIndex<br>                <SPAN style="color:#00007F">If</SPAN> idx >= 0 <SPAN style="color:#00007F">Then</SPAN> mLiCtl.ListIndex = idx<br>                <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Function</SPAN><br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">Else</SPAN><br>            UnhookListBoxScroll<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    ListMouseProc = CallNextHookEx( _<br>                    mLngLiMouseHook, nCode, wParam, <SPAN style="color:#00007F">ByVal</SPAN> lParam)<br>    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Function</SPAN><br>errH:<br>     UnhookListBoxScroll<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN><br><br><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Function</SPAN> FrameMouseProc( _<br>            <SPAN style="color:#00007F">ByVal</SPAN> nCode <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> wParam <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _<br>            <SPAN style="color:#00007F">ByRef</SPAN> lParam <SPAN style="color:#00007F">As</SPAN> MOUSEHOOKSTRUCT) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> errH <SPAN style="color:#007F00">'Resume Next</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> (nCode = HC_ACTION) <SPAN style="color:#00007F">Then</SPAN><br><SPAN style="color:#007F00">'        If GetActiveWindow = mFormHwnd Then</SPAN><br>        <SPAN style="color:#00007F">If</SPAN> WindowFromPoint(lParam.pt.X, lParam.pt.Y) = mFrameHwnd <SPAN style="color:#00007F">Then</SPAN><br>            <SPAN style="color:#00007F">If</SPAN> wParam = WM_MOUSEWHEEL <SPAN style="color:#00007F">Then</SPAN><br>                FrameMouseProc = <SPAN style="color:#00007F">True</SPAN><br>                <SPAN style="color:#00007F">If</SPAN> lParam.hwnd > 0 <SPAN style="color:#00007F">Then</SPAN><br>                    mFrCtl.ScrollTop = Application.Max(0, mFrCtl.ScrollTop - cSCROLLCHANGE)<br>                <SPAN style="color:#00007F">Else</SPAN><br>                    mFrCtl.ScrollTop = Application.Min(mFrCtl.ScrollHeight - mFrCtl.InsideHeight, mFrCtl.ScrollTop + cSCROLLCHANGE)<br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>                <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Function</SPAN><br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>         <br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    FrameMouseProc = CallNextHookEx( _<br>                mLngFrMouseHook, nCode, wParam, <SPAN style="color:#00007F">ByVal</SPAN> lParam)<br>    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Function</SPAN><br>errH:<br>    UnhookFrameScroll<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN><br></FONT>

Now for the TreeControl, which is held in a frame, I want to refer to the HookFrameScroll function above. So in the Module for the form which holds this treeControl frame I have this code to start the hook, and to remove the hook when the form closes:

<font face=Calibri><SPAN style="color:#007F00">'*******************************************</SPAN><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> frTreeControl_MouseMove(<SPAN style="color:#00007F">ByVal</SPAN> Button <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> Shift <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> X <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Single</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> Y <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Single</SPAN>)<br><SPAN style="color:#007F00">'</SPAN><br><SPAN style="color:#007F00">' Enable mouse scrolling on the frame holding</SPAN><br><SPAN style="color:#007F00">' the tree control</SPAN><br><SPAN style="color:#007F00">'*******************************************</SPAN><br>    HookFrameScroll Me, frTreeControl<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><SPAN style="color:#007F00">'*******************************************</SPAN><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> UserForm_QueryClose(Cancel <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, CloseMode <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>)<br><SPAN style="color:#007F00">'</SPAN><br><SPAN style="color:#007F00">' X-button -> hand over to btnClose</SPAN><br><SPAN style="color:#007F00">'*******************************************</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> CloseMode = 0 <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#007F00">' do what the Cancel button does</SPAN><br>        btnClose_Click  <SPAN style="color:#007F00">'>> rename this to whatever your close button sub is called <<</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#007F00">' unhook mouse scroll on tree</SPAN><br>    UnhookFrameScroll<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br></FONT>
 

filipejrc

New Member
Joined
Nov 13, 2018
Messages
9
Thank you very much for your help and for the new code. Unfortunately, I did not succeed in implementing it. When the mouse is over the treeview frame it moves erratically and in slow motion and after 2 seconds MS Access unexpectedly quits. If I comment, in the sub procedure "HookFrameScroll", this line:
mLngFrMouseHook = SetWindowsHookEx (_
WH_MOUSE_LL, AddressOf FrameMouseProc, lngAppInst, 0)

Then the mouse moves back normally and MS Access does not shut down, but the mouse scroll does not work either
 

Watch MrExcel Video

Forum statistics

Threads
1,129,299
Messages
5,635,387
Members
416,856
Latest member
silentir

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top