VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling) - Page 2-VBForums
Page 2 of 2 FirstFirst 12
Results 41 to 63 of 63

Thread: VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling)

  1. #41
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,692

    Re: VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling)

    Quote Originally Posted by qvqnytowl
    For the last couple of years, I used the code listed in the above posts to do my mouse scrolling and it worked fairly well. One thing I didn't like was that if I was debugging my program and the program hit a break point, VB would close with the dreaded "VB must shutdown now..." message. If I had made a couple of changes but forgotten to hit the save button prior, oh well. Small inconvenience I suppose.
    It's simple enough to set a variable in Form_Load to tell whether you're in the IDE. (Debug.Print 1/0 and set the variable to "I'm in the IDE" in the error trap, then resume next.)

    If you're NOT in the IDE, set up the subclassing for the mousewheel. Same thing in Form_Unload - only reset the subclassing if you're not in the IDE. For the few times you actually need to work on the scrolling you can comment out the line that sets the variable.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  2. #42
    New Member
    Join Date
    Sep 2008
    Posts
    1

    Re: VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling)

    Hi, I'm using VB6. I used the WheelHook-NestedControls reference code from post 1 in my exe project and it works very well.

    However, when I ported the code into a ActiveX project, the mouse wheel was no longer working on the flexgrid. I'm guessing I cannot simply use GetParent for the ocx code, but my understanding in this area is weak. Can someone help? Thank you.

  3. #43
    New Member
    Join Date
    May 2009
    Posts
    1

    Re: VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling)

    Hi,

    I know very little about code and don't follow most of this thread, but I get the idea that your code can solve my problem and am hoping that someone can give me a little bit of help with it.

    I am using Excel 2003 and whenever a listbox has the focus and you use the mousewheel, Excel crashes completely. Will the code in this thread fix that problem? Can someone tell me where to paste it in (sheetcode?) so that it will work?

    Thanks!!!

    Tai

  4. #44
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    37,235

    Re: VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling)

    Welcome to VBForums

    Adding code (whether from here or elsewhere) will not do you any good.

    There might be existing code that runs, in which case temporarily removing it might remove the problem too (but perhaps cause other issues). Alternatively there may be corruption in Excel or your mouse driver, or some other issue.


    It would be best to post a new thread about your problem in our General PC forum (or perhaps the Office Development forum instead).

  5. #45
    New Member
    Join Date
    Oct 2009
    Posts
    1

    Re: VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling)

    Thank you soo much Bushmobile... this code has helped me a lot

  6. #46
    Member
    Join Date
    May 2009
    Posts
    56

    Re: VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling)

    How can I use it with DataGrid ?

  7. #47
    New Member
    Join Date
    Nov 2009
    Posts
    1

    Re: VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling)

    Original post by qvqnytowl

    It even overrides the MouseWheel code if you leave it in your application. (I commented out all my code so I wouldn't have the debug problem anymore.)

    Oh, did I mention it's free?

    http://www.geocities.com/SiliconVall...freewheel.html

    Anyway, I hope this is helpful to someone.



    Sounds useful, as has been this excellent thread. Unfortunately, the link no longer works. Anyone have any suggestions? Thanks..
    Last edited by DROB; Nov 18th, 2009 at 09:31 PM. Reason: inexperienced user

  8. #48
    Lively Member
    Join Date
    Mar 2009
    Posts
    98

    Re: VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling)

    2nd emotion to alMubarmij post... how can we use it to DataGrid?

  9. #49
    New Member
    Join Date
    Jul 2009
    Posts
    7

    Re: VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling)

    This is a very interesting thread and I hope that there are still some veterans of this thread lurking about. One of the final code revisions concerned using the mouse wheel with any depth of nested controls.

    My problem is just the opposite. I have several MDI forms, all of which have a PictureBox which acts as a container for an array of over 250 TextBox controls, over 20 CheckBox controls, and various and sundry other stuff. The only thing I need to scroll is the PictureBox (which has scrollbars) on the form that has control, regardless of which control on the form has the focus. Is there an efficient way to hook the WM_MOUSEWHEEL message for all of the controls and pass it to the VScroll without having to call WheelHook and WheelUnHook on all of the controls in the form?

    Ray

  10. #50
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    37,235

    Re: VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling)

    I haven't checked the code, but I suspect you do need to hook/unhook all of them - which wont be too hard because you can use a For Each loop, eg:
    Code:
    Dim objControl as Control
    For Each objControl In Me.Controls
      Select Case TypeName(objControl)
      Case "TextBox", "PictureBox"
         'hook/unhook here using objControl
      End Select
    Next objControl

  11. #51
    VBaholic & Loving It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    GetWindowRect()
    Posts
    12,816

    Re: VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling)

    Quote Originally Posted by RCox View Post
    ... Is there an efficient way to hook the WM_MOUSEWHEEL message for all of the controls and pass it to the VScroll without having to call WheelHook and WheelUnHook on all of the controls in the form?
    Ray
    I am not knocking the original code, but personally I would have done it differently. Wouldn't use subclassing, instead would use a windows hook: SetWindowsHookEx with the WH_GETMESSAGE hook type. Hooks are for the entire thread; not a single window. It not only passed the window message but which hWnd the message was intended for. So this can be used as is, or overridden with custom code to form a chain....

    This change would be a change in logic for the entire project uploaded in post #1. So it isn't a simple change. To enable only a single window at a time to get the mousewheel message, a public variable can be used that contains the active hWnd. To allow a chain of windows, then more creativity would be required.

    That's my two-cents worth. Doesn't directly answer your question, I know.
    Last edited by LaVolpe; Apr 21st, 2010 at 02:43 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {GDI+ Classes/Samples} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VBA Control Arrays} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  12. #52
    New Member
    Join Date
    Jul 2009
    Posts
    7

    Re: VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling)

    OK...I'm trying to make this work with the SetWindowsHookEx method and am having no luck in vb6. Code is as follows:

    HTML Code:
    Form Code_______
    
    
    Private Sub Form_Activate()
       ...
      IMWheel_Hook
       ...
    End Sub
    
    Private Sub Form_Deactivate()
       ...
      IMWheel_Unhook
       ...
    End Sub
    
    Public Sub WheelMoved(ByVal delta As Long, X As Long, Y As Long)
     ' this just to tell me when it is basically working...functional code later!
      response = MsgBox("Mouse wheel moved", vbOKOnly, "Mouse")
    End Sub
    
    
    End Form Code___________
    
    
    Module Code ___________
    
    Public Const WH_GETMESSAGE = 3
    Public Const MSH_MOUSEWHEEL = "MSWHEEL_ROLLMSG"
    Public IMWHEEL_MSG&
    Public HWND_HOOK&
    
    Public Type WH_MSG
      hwnd As Long
      Message As Long
      wParam As Long
      lParam As Long
      time As Long
      pt As POINTAPI
    End Type
    
    Public Declare Function RegisterWindowMessage& Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String)
    Public Declare Function SetWindowsHookEx& Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long)
    Public Declare Function UnhookWindowsHookEx& Lib "user32" (ByVal hHook As Long)
    Public Declare Function CallNextHookEx& Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Integer, lParam As Any)
    Public Declare Function GetCurrentThreadId Lib "kernel32" () As Long
    
    Public Function IMWheel_Hook() As Long
      IMWHEEL_MSG& = RegisterWindowMessage(MSH_MOUSEWHEEL)
      HWND_HOOK& = SetWindowsHookEx(WH_GETMESSAGE, AddressOf IMWheel, 0, GetCurrentThreadId)
    End Function
    
    Public Sub IMWheel_Unhook()
      UnhookWindowsHookEx HWND_HOOK&
    End Sub
    
    Public Function IMWheel(ByVal nCode As Long, ByVal wParam As Long, lParam As WH_MSG) As Long
       If lParam.Message = IMWHEEL_MSG& Then
         SARUniv.WheelMoved lParam.wParam, lParam.pt.X, lParam.pt.Y
       End If
      IMWheel = CallNextHookEx(HWND_HOOK&, nCode, wParam, lParam)
    End Function

    When run in the environment, nothing happens with the scrollwheel. When the app is ended, the MsgBox will pop up however many times the scrollwheel was moved while the app was running. When compiled (pcode) and run, nothing at all happens with the scrollwheel.

    Anyone spot anything wrong?

    R.

  13. #53
    VBaholic & Loving It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    GetWindowRect()
    Posts
    12,816

    Re: VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling)

    Don't only look for IMWHEEL_MSG (primarily used for older O/S), also look for WM_MOUSEWHEEL (and in Vista+, WM_MOUSEHWHEEL for horizontal mousewheel messaes). Read the msdn documentation for more information
    Insomnia is just a byproduct of, "It can't be done"

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {GDI+ Classes/Samples} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VBA Control Arrays} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  14. #54
    New Member
    Join Date
    Jun 2010
    Posts
    1

    Re: VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling)

    I joined just for this thread! Wow, if I can figure out how this works, there are a number of cool uses for the wheel.....

  15. #55
    New Member
    Join Date
    Sep 2010
    Posts
    3

    Re: VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling)

    Hey there,

    I like this, works like a charm.

    However once the form has loaded, I'm finding debug doesn't work correctly - once you hit a breakpoint the VB6 IDE doesn't respond to any mouse clicks.

    You can use F-keys to navigate debugging but you can't get focus on your debug window or immediate window. And if your app form happens to be in front you can't see the code you're debugging either.

  16. #56
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    37,235

    Re: VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling)

    Welcome to VBForums

    Unfortunately that behaviour is expected (or rather, it is better than expected) because this code uses hooking. What normally happens when you try to debug code that uses hooking is that VB crashes completely, and it wouldn't be surprising if it happens to you at some point.

    There are ways to avoid the crashes if you really need to debug the hooking code itself, but the simplest solution is to not use hooking while you are running code in VB. To make that easier use code like this InIDE routine, and make these changes:
    Code:
    Private Sub Form_Activate()
       ...
      If Not InIDE Then IMWheel_Hook
       ...
    End Sub
    
    Private Sub Form_Deactivate()
       ...
      If Not InIDE Then IMWheel_Unhook
       ...
    End Sub

  17. #57
    New Member
    Join Date
    Sep 2010
    Posts
    3

    Re: VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling)

    Thanks for that si,

    What I don't understand is why "non hook" code also has this issue. e.g. putting some code on the click event of a control, and debugging that, is doing the same thing.

    I'm new to the concept of hooking and not really sure when "the windows function" gets called or even what it is (as it is only ever unhelpfully referred to as "the windows function" in msdn). If I take a wild guess that it gets called for all events of the form and it's controls, then in effect none of the app is de-buggable.

    I guess there's no way to have hooking enabled AND debug then - which is a shame because you might one day want to debug some of the mousewheel stuff.

  18. #58
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    37,235

    Re: VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling)

    Does the same thing happen for a brand new project? If not then my fix above is incomplete (I wasn't thorough!)

    As for the term "the windows function", I would have to see the article that uses it to be sure, but unless you have the need to write your own hooking code I recommend simply using this code as a black-box that "just works".

    In terms of debugging the hooking code, it is possible as I implied above... but it takes more effort, and is unlikely to be needed because this has been well tested by many people already.

  19. #59
    New Member
    Join Date
    Sep 2010
    Posts
    3

    Re: VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling)

    Sorry, my bad - I meant "the window procedure"

    http://allapi.mentalis.org/apilist/SetWindowLong.shtml
    http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

    It's nice that I can set a new address to it, but what is it? Or am I just being thick

    As for your fix - I thought that was just a blanket "turn it all off or on" depending what mode you're in. I haven't tried it yet but it's good to know it's there.

    What I was saying is that with hooking turned on, I can't debug *any* code. So lets say I add a new control to the form, and on click put up a messagebox - debug that line of code and it still gets "stuck" behind the app window. It's no big deal if you turn the hooking off in debug anyway (as per your solution) - I was just being curious

  20. #60
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    37,235

    Re: VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling)

    If you look at post #52 above, the window procedure is the IMWheel function. Hooking causes it to be run automatically by Windows when particular things happen (such as the mouse wheel moving).

    Hooking does cause problems for debugging the entire project, which is why my suggestion disables the hooking when running in VB.

  21. #61
    New Member
    Join Date
    Jul 2011
    Posts
    15

    Re: VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling)

    I'm using an AdobePDF view control which does not have a hWnd method. Doesn't look like there is any discussion about controls without that method.

    Any way to get it to work without it?

    I have attached a sample project so you know what I'm working with (youll have to have adobe reader 8 or above). The user needs to scroll the pdf viewer while focus is on the text box (though they could leave the mouse over the adobe control like in the examples in this thread).
    Attached Files Attached Files
    Last edited by Malisk; Aug 10th, 2011 at 03:22 PM.

  22. #62
    New Member
    Join Date
    Aug 2011
    Posts
    1

    Re: VB6 - MouseWheel with Any Control (originally just MSFlexGrid Scrolling)

    There's no need to say this is a Great code.
    I've used it mostly for MSHFlexgrids, and now I wanted to use it for VScrolls, searching this thread someone mentioned them and gave me the idea to modify the code a little:

    Code:
    Private Sub MouseWheel(ByVal MouseKeys As Long, _
                           ByVal Rotation As Long, _
                           ByVal Xpos As Long, _
                           ByVal Ypos As Long, _
                           ByVal frm As Form)
    Dim ctl As Control
    Dim bHandled As Boolean
    Dim bOver As Boolean
    Dim ctlDefaultVScroll As Control
       
       Set ctlDefaultVScroll = Nothing
      
       For Each ctl In frm.Controls
          ' Is the mouse over the control
          On Error Resume Next
          bOver = (ctl.Visible And IsOver(ctl.hWnd, Xpos, Ypos))
          On Error GoTo 0
          
          If bOver Or TypeOf ctl Is VScrollBar Then
             ' If so, respond accordingly
          
             If bOver Then bHandled = True
          
             Select Case True
                
                Case TypeOf ctl Is MSHFlexGrid
                   FlexGridScroll ctl, MouseKeys, Rotation, Xpos, Ypos
                    
                Case TypeOf ctl Is PictureBox, TypeOf ctl Is Image
                   PictureBoxZoom ctl, MouseKeys, Rotation, Xpos, Ypos
                      
                Case TypeOf ctl Is ListBox, TypeOf ctl Is TextBox, TypeOf ctl Is ComboBox
                   ' These controls already handle the mousewheel themselves, so allow them to:
                   If ctl.Enabled Then ctl.SetFocus
                
                Case TypeOf ctl Is VScrollBar
                   Set ctlDefaultVScroll = ctl
                
                Case Else
                    bHandled = False
       
             End Select
             If bHandled Then Exit Sub
          End If
          bOver = False
       Next ctl
       
       ' Scroll was not handled by any controls, so treat as a general message send to the form
       
       ' Instead, I check if it has a VScroll, and if it has, I use it
       ' Me.Caption = "Form Scroll " & IIf(Rotation < 0, "Down", "Up")
       If Not ctlDefaultVScroll Is Nothing Then
          With ctlDefaultVScroll
             .Value = MouseWheelChange(.Value, -1 * Sgn(Rotation) * .LargeChange, .Min, .Max)
          End With
       End If
      
    End Sub
    
    Private Function MouseWheelChange(CurrentValue As Variant, DeltaValue As Variant, MinValue As Variant, MaxValue As Variant) As Variant
    Dim newvalue As Variant
       newvalue = CurrentValue + DeltaValue
       If newvalue < MinValue Then
          newvalue = MinValue
       ElseIf newvalue > MaxValue Then
          newvalue = MaxValue
       End If
       MouseWheelChange = newvalue
    End Function
    That way, if no control is selected, and the form has a VScroll, it will use it.


  23. #63
    New Member
    Join Date
    Mar 2008
    Posts
    13

    Re: VB6 - MouseWheel Scrolling in MSFlexGrids

    WARNING ABOUT THIS CODE

    I just spent the last two months trying to figure out why my Visual Basic 6 IDE froze on both my Windows 7 and XP computers. Even reformatted the hard drive in the XP and reloaded Windows, drivers, updates, etc. IDE still froze. I finally traced it down to this mouse wheel module. It work fine for a year. Not any more!

    Just now I noticed the fine print: "These codes use subclassing which can cause your IDE to crash".

    I sure did for me.

    Beware of this code!!!!!

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.