Method of Controlling System Volume and Silence Based on Addition and Subtraction Buttons by C

  • 2021-08-12 03:21:00
  • OfStack

This paper describes the method of C # to control the volume and mute of the system based on addition and subtraction buttons. Share it for your reference. The details are as follows:


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 
namespace VolumnSet 
{ 
  public partial class Form1 : Form 
  { 
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam); 
    const uint WM_APPCOMMAND = 0x319; 
    const uint APPCOMMAND_VOLUME_UP = 0x0a; 
    const uint APPCOMMAND_VOLUME_DOWN = 0x09; 
    const uint APPCOMMAND_VOLUME_MUTE = 0x08; 
    public Form1() 
    { 
      InitializeComponent(); 
    } 
    private void button1_Click(object sender, EventArgs e) 
    { 
      // Increase the volume  
      SendMessage(this.Handle, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_UP * 0x10000); 
    } 
    private void button2_Click(object sender, EventArgs e) 
    { 
      // Volume reduction  
      SendMessage(this.Handle, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_DOWN * 0x10000); 
    } 
    private void checkBox1_CheckedChanged(object sender, EventArgs e) 
    { 
      // Silence  
      SendMessage(this.Handle, WM_APPCOMMAND, 0x200eb0, APPCOMMAND_VOLUME_MUTE * 0x10000); 
    } 
  } 
}

I hope this article is helpful to everyone's C # programming.


Related articles: