基于.NET Framework 4.0开发,包含滑块设置音量,键盘F1、F2调节音量。
通过滑块调节系统音量核心代码
private void SetVolume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (isVolumeChange)
{
return;
}
this.Dispatcher.BeginInvoke(new Action(() =>
{
VolumeHelper.VolumeHelper.SetVolume((int)SetVolume.Value);
TextVolume.Text = Volume + ((int)SetVolume.Value).ToString();
SoundPlayer player = new SoundPlayer();
player.Stream = Properties.Resources.Volume;
player.Play();
}));
}
通过键盘系统音量核心代码
#region 获取键盘键值
private void Window_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
switch (e.Key.ToString())
{
case "F1":
VolumeUP();
this.Dispatcher.BeginInvoke(new Action(() =>
{
SetVolume.Value = SetVolume.Value + 5;
SoundPlayer player = new SoundPlayer();
player.Stream = Properties.Resources.Volume;
player.Play();
}));
break;
case "F2":
VolumeDown();
this.Dispatcher.BeginInvoke(new Action(() =>
{
SetVolume.Value = SetVolume.Value - 5;
SoundPlayer player = new SoundPlayer();
player.Stream = Properties.Resources.Volume;
player.Play();
}));
break;
}
}
#endregion
private void VolumeUP()
{
this.Dispatcher.Invoke(new Action(delegate
{
WindowInteropHelper wndHelper = new WindowInteropHelper(this);
for (int i = 0; i < 5; i++)
{
SendMessage(wndHelper.Handle, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_UP * 0x10000);
SoundPlayer player = new SoundPlayer();
player.Stream = Properties.Resources.Volume;
player.Play();
}
}));
}
private void VolumeDown()
{
this.Dispatcher.Invoke(new Action(delegate
{
WindowInteropHelper wndHelper = new WindowInteropHelper(this);
for (int i = 0; i < 5; i++)
{
SendMessage(wndHelper.Handle, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_DOWN * 0x10000);
SoundPlayer player = new SoundPlayer();
player.Stream = Properties.Resources.Volume;
player.Play();
}
}));
}
C# 音量调节Demo源码下载
|