Programmatically Increase, Decrease and Mute the Volume
I was recently working on the Windows API and thought of creating a small application that can increase, decrease and mute the volume in .NET. Since we will be consuming unmanaged DLL functions, hence I will be using P/Invoke that enables managed code to call unmanaged functions implemented in DLL’s, like the ones in the Win32 API. For this purpose, we will be using the System.Runtime.InteropServices namespace
In this article, I assume you are familiar with interoperating with Unmanaged Code. If you have never worked with unmanaged code, please read the MSDN article Interoperating with Unmanaged Code.
I have created a Windows Application and added three Button Controls to the form as shown below:
Let us see the code that will be invoked on every button click:
C#
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
private const int APPCOMMAND_VOLUME_UP = 0xA0000;
private const int APPCOMMAND_VOLUME_DOWN = 0x90000;
private const int WM_APPCOMMAND = 0x319;
[DllImport("user32.dll")]
public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg,
IntPtr wParam, IntPtr lParam);
public Form1()
{
InitializeComponent();
}
private void btnMute_Click(object sender, EventArgs e)
{
SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
(IntPtr)APPCOMMAND_VOLUME_MUTE);
}
private void btnDecVol_Click(object sender, EventArgs e)
{
SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
(IntPtr)APPCOMMAND_VOLUME_DOWN);
}
private void btnIncVol_Click(object sender, EventArgs e)
{
SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
(IntPtr)APPCOMMAND_VOLUME_UP);
}
}
}
VB.NET
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
As seen in the code above, we are first using the DllImportAttribute attribute that provides the information needed to call a function exported from an unmanaged DLL. On each button click, we are using the SendMessageW() function to send the specified message to a window to increase, decrease and mute the volume.
I hope you liked the article and I thank you for viewing it.
Was this article worth reading? Share it with fellow developers too. Thanks!