C# 如何使用WPF使窗口静音?

C# 如何使用WPF使窗口静音?,c#,wpf,windows,volume,C#,Wpf,Windows,Volume,我正在学习C#和WPF,并有了一个小实用程序的想法。我想要一个大的红色按钮,它只能做一件事:完全静音/取消静音所有Windows声音(系统嘟嘟声、WMP、DVD播放机等)。我在VS 2008中浏览了对象浏览器,但似乎找不到我需要的东西:一个会影响所有Windows的静音 它是不是System.Windows.Input.mediacomands.MuteVolume,我只是不知道如何使用它 感谢使用C#和/或WPF在正确方向上提供的任何指针。:) 我非常确定该命令是由单个WPF控件用于静音的。例

我正在学习C#和WPF,并有了一个小实用程序的想法。我想要一个大的红色按钮,它只能做一件事:完全静音/取消静音所有Windows声音(系统嘟嘟声、WMP、DVD播放机等)。我在VS 2008中浏览了对象浏览器,但似乎找不到我需要的东西:一个会影响所有Windows的静音

它是不是
System.Windows.Input.mediacomands.MuteVolume
,我只是不知道如何使用它


感谢使用C#和/或WPF在正确方向上提供的任何指针。:)

我非常确定该命令是由单个WPF控件用于静音的。例如,如果CommandTarget是MediaElement,则在执行该命令时,它将静音。不幸的是,我想你得多做点工作。google提供了一些使用p/invoke方式的示例,这可能是目前在.NET中使用的唯一方式:

对于XP:

对于Vista/7:

您可以使用NAudio(http://naudio.codeplex.com/releases/view/79035). 下载最新版本。提取DLL并在C#项目中引用DLL NAudio

然后添加以下代码以迭代所有可用的音频设备,并尽可能使其静音

        try
        {
            //Instantiate an Enumerator to find audio devices
            NAudio.CoreAudioApi.MMDeviceEnumerator MMDE = new NAudio.CoreAudioApi.MMDeviceEnumerator();
            //Get all the devices, no matter what condition or status
            NAudio.CoreAudioApi.MMDeviceCollection DevCol = MMDE.EnumerateAudioEndPoints(NAudio.CoreAudioApi.DataFlow.All, NAudio.CoreAudioApi.DeviceState.All);
            //Loop through all devices
            foreach (NAudio.CoreAudioApi.MMDevice dev in DevCol)
            {
                try
                {
                    //Show us the human understandable name of the device
                    System.Diagnostics.Debug.Print(dev.FriendlyName);
                    //Mute it
                    dev.AudioEndpointVolume.Mute = true;
                }
                catch (Exception ex)
                {
                    //Do something with exception when an audio endpoint could not be muted
                }
            }
        }
        catch (Exception ex)
        {
            //When something happend that prevent us to iterate through the devices
        }