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

Posted on 2004-11-18
Last Modified: 2012-06-22
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.
Question by:gecko_au2003
    LVL 6

    Assisted Solution


          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

    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
     Set if the CTRL key is down.
     Set if the left mouse button is down.
     Set if the middle mouse button is down.
     Set if the right mouse button is down.
     Set if the SHIFT key is down.

    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.

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

    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
    LVL 23

    Author Comment

    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 !
    LVL 1

    Accepted Solution

    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

            X As Long
            Y As Long
    End Type

        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
            '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
        ' 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.

    LVL 23

    Author Comment

    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.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    twoTwo  challenge 35 72
    linearIn  challenge 23 54
    Excel object stays open 19 56
    WMI, model #, retrieving information 10 57
    INTRODUCTION We all know how to code. But at times you simply want to insert a common code block into your existing code and amend it as per your requirements. This tool not only saves you time but also saves you the pain of typing it all out aga…
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now