VB.NET: Control the System Volume, mute, and output the current level to the user

Answered VB.NET: Control the System Volume, mute, and output the current level to the user

  • giovedì 12 aprile 2012 16:11
     
     

    Hello

    I have searched the web for code relating to changing the system volume, muting the system volume, and outputing the current level of the volume as an integer, using VB.NET.

    All the code that I have found either doesn't work, or is very long.

    Does anyone have relatively small code to achieve this.

    Thank you
    Aaron

Tutte le risposte

  • giovedì 12 aprile 2012 16:23
     
     Con risposta

    Have you tried using the below Class? Unfortunately the .NET code requires use of API function calls or external libraries to perform these operations.

    http://blogs.vbcity.com/drydo/archive/2005/06/07/2038.aspx


    Paul ~~~~ Microsoft MVP (Visual Basic)

  • giovedì 12 aprile 2012 17:52
     
     

    Hi Paul

    I had seen that, I was hoping there would be something a little bit smaller.

    Thanks
    Aaron

  • lunedì 16 aprile 2012 05:45
    Moderatore
     
     Con risposta Contiene codice

    Hi AaronMcH,

    Welcome to the MSDN forum.

    I’d like to share the smaller code. But this code just up, down and mutes the volume. It can’t show the integer currently.  You can create a new application, add three buttons and the below code and have a try.

    Imports System.Runtime.InteropServices
    Public Class Form1
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
        End Function
        Const WM_APPCOMMAND As UInteger = &H319
        Const APPCOMMAND_VOLUME_UP As UInteger = &HA
        Const APPCOMMAND_VOLUME_DOWN As UInteger = &H9
        Const APPCOMMAND_VOLUME_MUTE As UInteger = &H8
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            SendMessage(Me.Handle, WM_APPCOMMAND, &H30292, APPCOMMAND_VOLUME_UP * &H10000)
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            SendMessage(Me.Handle, WM_APPCOMMAND, &H30292, APPCOMMAND_VOLUME_DOWN * &H10000)
        End Sub
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            SendMessage(Me.Handle, WM_APPCOMMAND, &H200EB0, APPCOMMAND_VOLUME_MUTE * &H10000)
        End Sub
    End Class

    Hope this helps.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

  • venerdì 20 aprile 2012 07:08
    Moderatore
     
     

    Hi AaronMcH,

    We haven’t heard from you for several days. I’d like to mark the helpful replies as answer firstly. If you have any additional questions, you also can unmark the replay and post your question here. 

    Sorry for any inconvenience and have a nice day.

     


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

  • giovedì 3 maggio 2012 10:51
     
     
    hi this code works very very well and it works in windows 7 Cool thank 
  • lunedì 7 gennaio 2013 03:13
     
     

    Hello Mark,

    i just register me for this forum to say Thank You !!! to you...

    I searched the web for couples of hours, lookin for some tiny piece of code, like yours...

    Bravo... ;)

    For security reasons, i dont like to download certain tools, extensions, or selfmade dll...

    Anyway, you did it... ;)

    Greetz from .de

  • mercoledì 6 marzo 2013 04:56
     
     

    Thanks a lot, Mark!!!!

  • mercoledì 6 marzo 2013 07:20
     
      Contiene codice

    This code uses a single scrollbar to increase and decrease volume.

    Option Strict On
    
    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
        Private Const APPCOMMAND_VOLUME_MUTE As Integer = &H80000
        Private Const APPCOMMAND_VOLUME_UP As Integer = &HA0000
        Private Const APPCOMMAND_VOLUME_DOWN As Integer = &H90000
        Private Const WM_APPCOMMAND As Integer = &H319
    
        <DllImport("user32.dll")> Public Shared Function SendMessageW(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
        End Function
    
        Dim HScroll3 As Integer = 0
        Dim Count As Integer = 0
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_MUTE))
        End Sub
    
    
        Private Sub HScrollBar3_Scroll(sender As Object, e As ScrollEventArgs) Handles HScrollBar3.Scroll
            If Count = 1 Then
                If HScrollBar3.Value > HScroll3 Then
                    SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_UP))
                End If
                If HScrollBar3.Value < HScroll3 Then
                    SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_DOWN))
                End If
            End If
            HScroll3 = HScrollBar3.Value
            Count = 1
        End Sub
    End Class
    


    You've taught me everything I know but not everything you know.