-
[RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
I'm using a flexgrid to show some data from my database.
So there's a problem i can't scroll down my flexgrid with the mouse i have to click on the scroll down bar. there's another way to do this using the mouse to scroll down to the last item contained in my flexgrid a not use the scroll down bar. :)
-
Re: Scroll a MSFlexGrid
i think you have to hook the mouse: http://www.andreavb.com/tip060008.html
20/02/2005 Note: There is now a CodeBank example demonstrating how to do this: http://www.vbforums.com/showthread.php?t=388222
-
Re: Scroll a MSFlexGrid
I have a problem when i call the Hook procedure
i do it in the Form_Load event
like this
and when i compile my programm an error appears on AddrOf("WindowProc"))
Sub or Function not defined
VB Code:
Public Sub Hook(ByVal hControl_ As Long)
hControl = hControl_
lPrevWndProc = SetWindowLong(hControl, GWL_WNDPROC, AddrOf("WindowProc"))
End Sub
so i don't know if i have to declare another API or i'm missing another procedure
-
Re: Scroll a MSFlexGrid
try changing that to AddressOf
here's another example: http://www.adit.co.uk/html/mousewheelsupport.html
-
Re: Scroll down a MSFlexGrid with Mouse Wheel
when i change the AddrOf to AddressOf vb says Expected: identifier
Maybe i can test the other example to check if that works
and if a get any results i will post them here to let you know how well i'm doing that change on the code
i notice that in this code i hook the Form
VB Code:
Public Sub WheelHook(PassedForm As Form)
On Error Resume Next
Set MyForm = PassedForm
LocalHwnd = PassedForm.hWnd
LocalPrevWndProc = SetWindowLong(LocalHwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
-
Re: Scroll down a MSFlexGrid with Mouse Wheel
i think that second example looks better, go with that.
-
Re: Scroll down a MSFlexGrid with Mouse Wheel
i made this change
VB Code:
AddrOf ("WindowProc")
'for this
AddressOf WindowProc
but there's no change in my flexgrid
maybe when a call Hook i have to Hook the Form
-
Re: Scroll down a MSFlexGrid with Mouse Wheel
when i Hook Me.hWnd
says Objects doesn't support this property or method 438
maybe i have to check better the lines of the code
and test the other code and see any change to post and RESOLVE this thread
THX
-
1 Attachment(s)
Re: Scroll down a MSFlexGrid with Mouse Wheel
I've attached a example project of the second link.
-
Re: Scroll down a MSFlexGrid with Mouse Wheel
I have tested the code in my programm and works fine.
but there's something i see
I have to copy and paste the procedure MouseWheel in each Form i have, because i have at least 15 forms with a flexgrid so maybe i can put that in my module
-
1 Attachment(s)
Re: Scroll down a MSFlexGrid with Mouse Wheel
I have made some changes to the example code you sent me bushmobile
The problem is that in some forms I have two MSFlexGrid's and the hook only worked with one, because the flexgrid is only in the Form and the MouseWheel uses MSlexGrid1 but in this case i have more than one FlexGrid, and not exactly named MSFlexGrid1
So i did this changes to improve your code:
- Put the MouseWheel in the Module bas including another variable as parameter MyGrid as MSFlexGrid, to change the control to make the mousewheel changes
- Create a variable in my module, MyGrid as MSFlexGrid
- Add a parameter in the WheelHook of my PassedFlexGrid and set my variable MyGrid to this control passed
- Modify the WindowProc procedure to call mousewheel from my module and set the variable MyGrid in MouseWheel procedure.
-To call WheelHook i do it when in the Events MSFlexGrid_Click and MSFlexGrid_GotFocus like this
VB Code:
Private Sub MSFlexGrid1_Click()
WheelUnHook
WheelHook Me, MSFlexGrid1
End Sub
Private Sub MSFlexGrid1_GotFocus()
WheelUnHook
WheelHook Me, MSFlexGrid1
End Sub
and Unhook when the Control LostFocus
VB Code:
Private Sub MSFlexGrid1_LostFocus()
WheelUnHook
End Sub
you can check the changes in the uploaded zip file
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
I thought this problem was all related to the driver associated with the particular mouse. Did you attempt to fix it without using this API concept?
Did you also consider change the hook based on what grid the mouse was hovered over - instead of actual focus?
-
1 Attachment(s)
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
I have attached the code to have it working with multiple flexgrids & forms.
szlamany: you can't always guarantee that your user will have mouse drivers that support it - mine certainly doesn't!
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
Thanks so much. I've had complaints from some of my clients about this very problem - I've always downplayed them and told them to find better drivers.
This is a much nicer solution - I will try to implement it!
Thanks again!
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
This is a great solution, I only have two 'bad' comments.
The first is that the step is a bit too large - as it doesn't take into account Fixed rows, partially-visible rows, or scrollbars. That is easy to fix using the .RowIsVisible method in the FlexGridScroll sub like this:
VB Code:
Lstep = Int(Lstep)
Do While Not (.RowIsVisible(.TopRow + Lstep))
Lstep = Lstep - 1
Loop
(admittely I personally prefer smaller steps, say Lstep = 3)
The second issue is that the grid doesn't scroll the grid that the mouse is over (which is how it normally works), only the active one. To 'correct' this I guess that the MouseWheel sub would need to check the mouse co-ordinates, and see if a grid is underneath.
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
si - having not really reviewed the code yet - could't the hook change from grid to grid based on the grid's MOUSEMOVE event? Or do you think that is too expensive?
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
yeah, i was aware of both of those. i was just showing that that code could be used with multiple things (and was feeling a bit too lazy to add the checking which one it's over stuff :) )
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
Fair enough, good work tho - I'll be using a modified version of it :D
Quote:
Originally Posted by szlamany
si - having not really reviewed the code yet - could't the hook change from grid to grid based on the grid's MOUSEMOVE event? Or do you think that is too expensive?
That would be possible, but you'd need to use the mousemove events of all controls (and the form) to ensure that the appropriate control is used. Without having tested yet, I personally think an API check of the mouse position (when a scroll event takes place) would be more apt.
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
Quote:
Originally Posted by szlamany
si - having not really reviewed the code yet - could't the hook change from grid to grid based on the grid's MOUSEMOVE event? Or do you think that is too expensive?
Oh - I see the difference you mean...
Wheel should only scroll a grid if it's over a grid.
If the mouse moves off the grid the wheel should do nothing...
Or I could also see the argument for saying that the last "grid" hovered over can retain wheel scroll power - regardless of if the mouse leaves the grid...
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
Quote:
Originally Posted by si_the_geek
Fair enough, good work tho - I'll be using a modified version of it :D
That would be possible, but you'd need to use the mousemove events of all controls (and the form) to ensure that the appropriate control is used. Without having tested yet, I personally think an API check of the mouse position (when a scroll event takes place) would be more apt.
That's right...
I can see that the mouse wheel works right now if a combo box on my form has focus - so I'd be stealing the "mouse moves" from that combo box if the mouse is over the grid...
I guess I'll have to start testing this and see where it gets me.
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
Actually I've just done a quick test with a combo and list, it seems that they also have the same behaviour - it is the active control that scrolls, no matter where the mouse is.
It's strange considering that Outlook etc don't work that way, but it means that the code above (with adjustments for the scroll amount) is spot on. :)
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
wow, getting it right through laziness, that's a first for me :)
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
ok, i've added in your adjustment from post #15, si, and I've added an IsOver function so it scrolls the grid it's over rather than the one with focus.
Edit: Better version now available in the CodeBank
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
I said that i had resolved my problem, buy maybe it wasn't true because when i use MouseWheel sub worked for all of my FlesGrids only one didn't work so I check again and download WheelHook3.zip from bushmobile where i can scroll the flexgrid when the mouse pointer is over so this resolved my problem.
So now i can scroll all the flexgrids of my programm and there's no problem
THX guys who responded to this post
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
Quote:
Originally Posted by bushmobile
ok, i've added in your adjustment from post #15, si, and I've added an IsOver function so it scrolls the grid it's over rather than the one with focus.
Bushmobile - I tried the version the the codebank - wheelhook3.zip.
I added to your test project a combobox. As long as the combobox does not have focus, the wheel scrolls whatever flexgrid you are over.
If the combobox gets focus, then the wheel on scrolls the combobox - regardless of what you hover over.
Do you think this can easily be overcome? Or am I stuck using the "click" to focus on flexgrid version?
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
I have managed to sort this :)
See the CodeBank thread
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
I've not done much API work or subclassing...
So I'm curious what this does:
VB Code:
WindowProc = CallWindowProc(LocalPrevWndProc.Item("#" & Lwnd), Lwnd, Lmsg, Wparam, Lparam)
I'm already doing this in my app:
VB Code:
Public Function WndProc(ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
'Let the window process messages as well
WndProc = CallWindowProc(m_lPrevProc, hwnd, wMsg, wParam, lParam)
'See what message has been sent to the window
If wMsg = WM_ACTIVATEAPP Then
'It's the activateapp message so call the sub on the form
Call m_frmHooked.AppFocus(CBool(wParam))
End If
If wMsg = WM_MOUSEWHEEL Then
Call MsgBox("test")
End If
End Function
So I need to add your MOUSEWHEEL message check to this existing routine - and it's already working to call the MSGBOX when I use the wheel...
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
i've also not done much subclassing, so i'm probably on the same learning curve :)
VB Code:
WindowProc = CallWindowProc(LocalPrevWndProc.Item("#" & Lwnd), Lwnd, Lmsg, Wparam, Lparam)
does the same as :
VB Code:
WndProc = CallWindowProc(m_lPrevProc, hwnd, wMsg, wParam, lParam)
just things are named differently and i've used a collection to store the lPrevProc values.
before we integrate your code into mine (or visa versa), why are you subclassing the WM_ACTIVATEAPP message, and not just using the Form_Activate event?
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
Quote:
Originally Posted by bushmobile
before we integrate your code into mine (or visa versa), why are you subclassing the WM_ACTIVATEAPP message, and not just using the Form_Activate event?
Form_Activate does not fire when your app is left and/or returned to.
WM_ACTIVATEAPP message does fire.
We do it to make sure the "control" we desire the focus to be on still has the focus.
I think the snippet I posted:
Code:
'Let the window process messages as well
WndProc = CallWindowProc(m_lPrevProc, hwnd, wMsg, wParam, lParam)
indicates that this CallWindowProc passes the message back along to the windows processes.
Maybe not calling this function can suppress the drop-down type controls from grabbing the mouse wheel without have to hook and unhook it in all those places.
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
ok, i've got rid of the need for those fiddly _DropDown, _LostFocus things by using CB_DROPDOWNSTATE, to check if it's dropdown. I've updated the zip in the codebank.
you should be able to just add a case in like the below so you can implement your WM_ACTIVATEAPP code.
VB Code:
Case WM_ACTIVATEAPP
Set fFrm = GetForm(Lwnd)
If Not fFrm Is Nothing Then Call fFrm.AppFocus(CBool(wParam))
regarding the CallWindowProc stuff in the WheelHook4.zip, the
VB Code:
WindowProc = CallWindowProc(LocalPrevWndProc.Item("#" & Lwnd), Lwnd, Lmsg, Wparam, Lparam)
line (or whatever equivalent you wish to use) needs to be after the monitoring of the messages, because sometimes we don't want to process the message, just discard it (in the case of the combobox being selected, but the mouse not on it.)
There are of course alternative ways to do what I've done. I'm using the vbAccelerator SSubTmr to manage subclassing in my current project.
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
I've actually got a bit of testing and modification to go through here...
That WM_ACTIVATEAPP is being listened to by the MDI-parent form of my app.
I really want this WM_MOUSEWHEEL to be caught by whatever childform has focus.
So I'm off to experimenting with having two WINDOWPROC functions in my app - one for the parent-window and another for this child window/mouse wheel stuff.
But thanks again for this - it's really going to make many of my more sophisticated users happy.
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
It is working for me now. I'm still hooking the parent form so I can listen for WM_ACTIVATEAPP.
And I am changing the mousewheel hook based on what child form has focus. If I alt-tab back and forth from child to child the mousewheel action is perfect.
I haven't yet decided how to make it pick the grid to scroll. I do a lot of "hiding" grids by putting them behind other grids - got radio-buttons that make grids full-size or restore-to-original. On forms with several grids I'm starting to think that whenever the user mouses-over a grid that becomes the "wheel grid" - even if they do not stay on the grid (like move slightly off the grid so as to see the data, you know how the mouse and tool-tip can get in the way sometimes).
I'm so excited I'm going to have to add a "new feature" pop-up window to my app so my customers know this one got enhanced.
edit: btw - why are you not simply scrolling it one row up or down in your samples?
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
Quote:
So I'm off to experimenting with having two WINDOWPROC functions in my app - one for the parent-window and another for this child window/mouse wheel stuff.
Messing around with it is the best way to learn :thumb: :)
Quote:
btw - why are you not simply scrolling it one row up or down in your samples?
cos that's just how it was on the website i got the original code from.
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
Just a modification by me..
VB Code:
Public Sub FlexGridScroll(ByRef FG As MSFlexGrid, ByVal MouseKeys As Long, ByVal Rotation As Long, ByVal Xpos As Long, ByVal Ypos As Long)
Dim NewValue As Long
Dim Lstep As Single
On Error Resume Next
With FG
Lstep = .Height / .RowHeight(0)
Lstep = Int(Lstep)
[B] If .Rows < Lstep Then Exit Sub[/B]
Do While Not (.RowIsVisible(.TopRow + Lstep))
Lstep = Lstep - 1
Loop
If Rotation > 0 Then
NewValue = .TopRow - Lstep
If NewValue < 1 Then
NewValue = 1
End If
Else
NewValue = .TopRow + Lstep
If NewValue > .Rows - 1 Then
NewValue = .Rows - 1
End If
End If
.TopRow = NewValue
End With
End Sub
The bolded line, because it would erase all the lines except one if the msflex was less than LStep..
Hope it works for you guys also :)
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
Another thing, to prevent my IDE from getting messed up, and killing my ability to pause..
In the form_load, put this instead of just straight up hooking:
VB Code:
If App.LogMode = 1 Then Call WheelHook(Me)
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
Didn't know about that...
We always use:
Code:
On Error Resume Next
Debug.Print 1 / 0
gDebugMode = (Err.Number <> 0)
btw - your change about .Rows makes sense...
but in the long run this is a perfect time to ignore all errors in the routine - we simply don't want to stop with any error that might pop-up when using the wheel.
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
Hey |2eM!x,
good spot :thumb:, I've added your change about the rows to the codebank example.
I've also added a little note mentioning that the code uses subclassing, and directed them to the thread on detecting if you're running in the IDE.
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
We already had tons of events already tracking grid selection and even grid hover (we set the tooltip text to the full "cell value" - if it's not fully displayed - when you hover over a cell).
So I ended up setting a form-level variable with the "grid" to be "mouse-wheel" associated.
Clicking into a grid sets that grid as the mouse-wheel grid. Hovering into it does the same. But hovering out does not change the setting.
And this was the simple function we used to move up/down
VB Code:
Public Function ScrollGrid(f As Form, lngMouseKeys As Long, lngXP As Long, lngYP As Long, lngRotation As Long) As Boolean
Dim booSkipScroll As Boolean
Debug.Print " ScrollGrid"
On Error GoTo Error_Handler
Begin:
'Call StatusMessage(f, f.Caption & " " & CStr(lngXP) & " " & CStr(lngYP) & " " & CStr(lngRotation), False)
With f.flxInput(f.mlngGridScroll)
If .RowIsVisible(.FixedRows) And .RowIsVisible(.Rows - 1) Then
booSkipScroll = True
Else
booSkipScroll = False
End If
If Not booSkipScroll Then
If lngRotation < 0 Then ' scroll down
.TopRow = .TopRow + 1
Else ' scroll up
.TopRow = .TopRow - 1
End If
End If
End With
Rtn_Caller:
Exit Function
Error_Handler:
' Call Fatal_Error(Err.Number, Err.Source, Err.Description, "ScrollGrid")
Resume Rtn_Caller
End Function
Also @ !2M - thanks for the heads up - made a quick adjustment to not scroll a grid if all the rows are already displayed :)
-
Re: [RESOLVED] Scroll down a MSFlexGrid with Mouse Wheel
Can I use it with DataGrid control ?