Changing the master volume in C#
Ok, I have a problem, I have tried so hard just to change the master volume in C#, it seems like the answerers of the questions appear to be doing rocket science, or they just add a bunch of extra junk.
Could someone just show me how I can mak开发者_运维技巧e a class that has a Volume{get{}set{}} property in it that lets me change the master volume. A requirement is that it has to work in 64 and 32 bit OS. Nothing else, just a volume integer.
You can get an idea from the following code, just instead of putting those functions in events, invoke them in the property setter.
Do this :
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;
private const int APPCOMMAND_MEDIA_PLAY_PAUSE = 0xE0000;
[DllImport("user32.dll")]
public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg,
IntPtr wParam, IntPtr lParam);
public Form1()
{
InitializeComponent();
}
private void btnPlayPause_Click(object sender, EventArgs e)
{
SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
(IntPtr)APPCOMMAND_MEDIA_PLAY_PAUSE);
}
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);
}
hope this help.
精彩评论