Control Volume in vb.net

2019-06-04 10:16发布

问题:

I need to control the main computer volume in vb.net. I have searched the web and tried all the examples I could find. None of the have worked.

Does anyone have some snippets of code that work with vb.net 2010 to control volume?

Thanks
giodamelio

回答1:

I found an answer. Its uses Nirsoft's NirCmd (32-bit , 64-bit)

Public Class Sound
    Dim nircmd As String
    Const MAXVOL As Integer = 65535

    Public Sub New(ByVal nircmd_location As String)
        nircmd = nircmd_location
    End Sub

    Public Sub setVol(ByVal level As Integer)

        Dim p As New ProcessStartInfo
        p.FileName = nircmd
        p.Arguments = "setsysvolume " & (MAXVOL * (level / 100)).ToString
        Process.Start(p)

    End Sub
End Class

You can then use somthing like

Dim vol As New Sound(path_to_nircmd)
vol.setVol(50)


回答2:

I know this question is really old, but this worked for me:

http://www.dotnetcurry.com/showarticle.aspx?ID=431

Imports System
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Namespace WindowsFormsApplication1
    Partial Public Class Form1
        Inherits Form
        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

        Private Sub btnMute_Click(ByVal sender As Object, ByVal e As EventArgs)
            SendMessageW(Me.Handle, WM_APPCOMMAND, _
                          Me.Handle, New IntPtr(APPCOMMAND_VOLUME_MUTE))
        End Sub

        Private Sub btnDecVol_Click(ByVal sender As Object, ByVal e As EventArgs)
            SendMessageW(Me.Handle, WM_APPCOMMAND, _
                         Me.Handle, New IntPtr(APPCOMMAND_VOLUME_DOWN))
        End Sub

        Private Sub btnIncVol_Click(ByVal sender As Object, ByVal e As EventArgs)
            SendMessageW(Me.Handle, WM_APPCOMMAND, _
                         Me.Handle, New IntPtr(APPCOMMAND_VOLUME_UP))
        End Sub
    End Class
End Namespace


回答3:

Check out VB.NET Master Volume Control Class



标签: vb.net volume