Programmatically Increase, Decrease and Mute the Volume

Posted by: Suprotim Agarwal , on 12/14/2009, in Category WinForms & WinRT
Views: 103142
Abstract: This article demonstrates how to programmatically increase, decrease and mute the volume
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:
Volume App
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.
Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles
Suprotim Agarwal, ASP.NET Architecture MVP, MCSD, MCAD, MCDBA, MCSE, is the CEO of A2Z Knowledge Visuals Pvt. He primarily works as an Architect Consultant and provides consultancy on how to design and develop .NET centric database solutions.

Suprotim is the founder and primary contributor to DotNetCurry, DNC .NET Magazine, SQLServerCurry and DevCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls. and is authoring another one at The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by PRiNCE on Saturday, December 25, 2010 12:38 AM
thanks.....
Comment posted by FireFly3000 on Friday, September 9, 2011 4:05 AM
this.Handle???? It doesn't seams to find it. I have just copy pasted the code so do I miss anything?
Comment posted by Glenn on Saturday, January 7, 2012 11:12 AM
Hi this has helped me out on something I am working on. One question though, Is it possible to make the volume increase continuosly without needing to repeatidly click on the button?
Comment posted by patrick on Friday, February 10, 2012 3:54 PM
Is he(it) possible to get back the information of the icon of the dumb volume
merci
Comment posted by Kishor Patil on Sunday, March 4, 2012 11:41 PM
It nice it’s worked properly thank you.............very much
Comment posted by vijay on Tuesday, March 6, 2012 11:19 PM
i want to decrease volume 505 directly please send code to my mail
Comment posted by vijaya on Tuesday, March 6, 2012 11:36 PM
i want to decrease volume 50% directly please send code to my mail
Comment posted by Asanka on Sunday, June 3, 2012 2:15 AM
Thank u very much agrawal. This is really helpful one. I am developing a web application. I need to decrease the volume by a given percentage. Can you mail me some coding to do that immediately. Really appreciate your work. Thanks in advance
Comment posted by lokesh on Thursday, July 12, 2012 6:56 AM
Very good post.. thank you
Comment posted by bartj on Tuesday, September 4, 2012 6:26 PM
Thank you! Your code works without any problems. You might mention that when you call the mute function it toggles back and forth between mute and un-mute. Just a detail most people would know but some may be confused as to how to un-mute.  Thanks again
Comment posted by Suprotim Agarwal on Friday, September 7, 2012 12:23 AM
bartj: Thanks for that comment!
Comment posted by sajjad on Tuesday, October 16, 2012 3:08 PM
Sir G great
Comment posted by ali on Monday, August 5, 2013 2:25 AM
i want to mute the application,not the whole sound system of my pc....help me out its urgent.......
Comment posted by Fernando on Saturday, October 26, 2013 7:29 AM
Thanks!
Comment posted by khan on Friday, June 20, 2014 5:34 PM
how to disable powerline to laptop using c#....plzzzz ans
Comment posted by ewr on Monday, June 23, 2014 7:34 AM
t

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel