Programmatically Increase, Decrease and Mute the Volume

Posted by: Suprotim Agarwal , on 12/14/2009, in Category WinForms & WinRT
Views: 164565
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:
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;
        public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg,
            IntPtr wParam, IntPtr lParam);
        public Form1()
        private void btnMute_Click(object sender, EventArgs e)
            SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
        private void btnDecVol_Click(object sender, EventArgs e)
            SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
        private void btnIncVol_Click(object sender, EventArgs e)
            SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
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.

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for ten consecutive times. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by PRiNCE on Saturday, December 25, 2010 12:38 AM
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
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 me out its urgent.......
Comment posted by Fernando on Saturday, October 26, 2013 7:29 AM
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