[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 442
  • Last Modified:

Mouse scroll button to control volume in Visual Basic 6.0 and C++ ?

I have managed to figure out how to control the volume in Visual Basic and I have one working example for the main volume control in C++, however what I want to know is how do you use the scroll button on a mouse to control the volume like they have done in winamp ??

I am looking for working examples in visual basic and C++ as well as tutorials or resources that explain how to do it.

Thank you for the time and effort.
0
gecko_au2003
Asked:
gecko_au2003
  • 2
2 Solutions
 
rushtoshankarCommented:
Hi,

      if you are developing your application in MFC then you can do this in the following manner..
just override the main window's
PreTranslateMessage(MSG* pMsg)  or OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)

in PreTranslateMessage(MSG* pMsg) function you can do as follows
--------------------------------------------------------------------------------------------------------------
fwKeys = LOWORD(wParam);    // key flags

zDelta = (short) HIWORD(wParam);    // wheel rotation

xPos = (short) LOWORD(lParam);    // horizontal position of pointer

yPos = (short) HIWORD(lParam);    // vertical position of pointer


Parameters
fwKeys
Value of the low-order word of wParam. Indicates whether various virtual keys are down. This parameter can be any combination of the following values: Value
 Description
 
MK_CONTROL
 Set if the CTRL key is down.
 
MK_LBUTTON
 Set if the left mouse button is down.
 
MK_MBUTTON
 Set if the middle mouse button is down.
 
MK_RBUTTON
 Set if the right mouse button is down.
 
MK_SHIFT
 Set if the SHIFT key is down.
 

zDelta
The value of the high-order word of wParam. Indicates the distance that the wheel is rotated, expressed in multiples or divisions of WHEEL_DELTA, which is 120. A positive value indicates that the wheel was rotated forward, away from the user; a negative value indicates that the wheel was rotated backward, toward the user.

xPos
Value of the low-order word of lParam. Specifies the x-coordinate of the pointer, relative to the upper-left corner of the screen.

yPos
Value of the high-order word of lParam. Specifies the y-coordinate of the pointer, relative to the upper-left corner of the screen.
--------------------------------------------------------------------------------------------------------------

and in OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)  function same as above function...

nFlags = fwKeys

zDelta = zDelta

xPos, yPos take this from pt
0
 
gecko_au2003Author Commented:
Any chance you or someone else can give me an example in Visual Basic 6.0 and show me where to put the code, like in a module, etc. Thank you for that example. Also what exactly is MFC ? Thank you for the time, effort and help !
0
 
jacyCommented:
Ok, you need a windows message hook in vb in order to do this.

1. Create a new standard EXE project
2. On Form1, put a label on it, call Label1
3. Copy and paste the code below:
' =========== Form1 starts ===============
Option Explicit

Private Sub Form_Load()
    ' initialise window message hook
    NextProcs = SetWindowLong(Me.hwnd, GWL_WNDPROC, _
                                    AddressOf WindowProc)
End Sub

Private Sub Form_Unload(Cancel As Integer)
    ' de-hook
    Call SetWindowLong(Me.hwnd, GWL_WNDPROC, NextProcs)

End Sub
' =========== Form1 ends ===============

4. Create a new module, call Module1
5. Copy and past the code below:

' =========== Module1 starts ===============
Option Explicit

Type POINTAPI
        X As Long
        Y As Long
End Type

Type MINMAXINFO
    ptReserved As POINTAPI
    ptMaxSize As POINTAPI
    ptMaxPosition As POINTAPI
    ptMinTrackSize As POINTAPI
    ptMaxTrackSize As POINTAPI
End Type

Private Const WM_GETMINMAXINFO = &H24
Private Const WM_MOUSEWHEEL = &H20A
Private Const WM_VSCROLL = &H115

Public NextProcs As Long
Public Const GWL_WNDPROC = -4
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal wndrpcPrev As Long, ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, lParam As Any) As Long

' MouseKeys
' 8 = ctrl key pressing while doing mouse scroll
' 4 = shift key pressing
' 0 = others
Public Sub GoMouseWheel(ByVal MouseKeys As Long, ByVal Rotation As Long, _
                            ByVal Xpos As Long, ByVal Ypos As Long)

    If Rotation > 0 Then
        'scroll up
    Else
        'scroll down
    End If

End Sub

' used to hook windows messages
Public Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, _
                            ByVal wParam As Long, ByVal lParam As Long) As Long

    On Error Resume Next
   
    Select Case uMsg
        Case WM_MOUSEWHEEL
        '
        'MouseKeys --> wp And 65535
        ' Rotation --> wp / 65536
        ' Xpos --> lp And 65535
        ' Ypos --> lp / 65536
        '
        'MyForm.MouseWheel MousKeys, Rotation, Xpos, Ypos
        '
            Form1.Label1.Caption = "Key: " & CStr(CLng(wParam And 65535)) & _
                                    ", Rotation: " & CStr(CLng(wParam / 65536)) & _
                                    ", xPos: " & CStr(CLng(lParam And 65535)) & _
                                    ", yPos: " & CStr(CLng(lParam / 65536))
            GoMouseWheel CLng(wParam And 65535), CLng(wParam / 65536), _
              CLng(lParam And 65535), CLng(lParam / 65536)
    End Select
   
QNext:
    ' put back to the windows stuff
    WindowProc = CallWindowProc(NextProcs, hwnd, uMsg, wParam, ByVal lParam)
   
End Function
' =========== Module1 ends ===============

That should give you what you want.

Jacy.
0
 
gecko_au2003Author Commented:
Thanks for both of them answers ! Is there anyway to speedy up how fast or slow the scroll button moves a slider control lin visual basic like they do in winamp to control the volume ? because at the minute I can make it move but it takes a lot of scrolling to move it up or down in terms of moving the slider.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now