简体   繁体   English

在C#中设置Windows 10的系统卷

[英]Set System Volume Windows 10 in C#

i searched like hours, now i ask in this Forum. 我搜索了几个小时,现在我在这个论坛中提问。

How can I control the System Volume Setting of Windows 10? 如何控制Windows 10的系统音量设置?

Which Libary I need? 我需要哪个图书馆?

I am using Visual Basic 2015 and wanna programm a Windows Universal App with C#. 我正在使用Visual Basic 2015,并希望使用C#对Windows Universal App进行编程。

The programm should be able to: 该程序应该能够:

  • Set Systemvolume to x% 将Systemvolume设置为x%

  • increase the Systemvolume by x 将Systemvolume增加x

  • decrease the Systemvolume by x 将系统体积减少x

  • get the current Systemvolume 获取当前的Systemvolume

I found a similar Question and Answer, but the Answer doesent work. 我找到了类似的“问答”,但“答案”确实起作用。

private void Mute() { 私人无效Mute(){

        SendMessageW(new WindowInteropHelper(this).Handle, WM_APPCOMMAND, new WindowInteropHelper(this).Handle,
            (IntPtr)APPCOMMAND_VOLUME_MUTE);
    }

it can't find "WindowInteropHelper". 它找不到“ WindowInteropHelper”。 But I implement: 但是我实现了:

using System; 使用系统;

using System.Windows.Forms; 使用System.Windows.Forms;

using System.Runtime.InteropServices; 使用System.Runtime.InteropServices;

You can't do that. 你不能那样做。 Universal apps are sandboxed and can't make global modifications to the system. 通用应用已被沙盒化,无法对系统进行全局修改。 This includes the system volume. 这包括系统体积。

class VolumeChanger
{
  private const byte VK_VOLUME_MUTE = 0xAD;
  private const byte VK_VOLUME_DOWN = 0xAE;
  private const byte VK_VOLUME_UP = 0xAF;
  private const UInt32 KEYEVENTF_EXTENDEDKEY = 0x0001;
  private const UInt32 KEYEVENTF_KEYUP = 0x0002;

  [DllImport("user32.dll")]
  static extern void keybd_event(byte bVk, byte bScan, UInt32 dwFlags, UInt32 dwExtraInfo);

  [DllImport("user32.dll")]
  static extern Byte MapVirtualKey(UInt32 uCode, UInt32 uMapType);

  public static void VolumeUp()
  {
     keybd_event(VK_VOLUME_UP, MapVirtualKey(VK_VOLUME_UP, 0), KEYEVENTF_EXTENDEDKEY, 0);
     keybd_event(VK_VOLUME_UP, MapVirtualKey(VK_VOLUME_UP, 0), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  }

  public static void VolumeDown()
  {
     keybd_event(VK_VOLUME_DOWN, MapVirtualKey(VK_VOLUME_DOWN, 0), KEYEVENTF_EXTENDEDKEY, 0);
     keybd_event(VK_VOLUME_DOWN, MapVirtualKey(VK_VOLUME_DOWN, 0), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  }

  public static void Mute()
  {
     keybd_event(VK_VOLUME_MUTE, MapVirtualKey(VK_VOLUME_MUTE, 0), KEYEVENTF_EXTENDEDKEY, 0);
     keybd_event(VK_VOLUME_MUTE, MapVirtualKey(VK_VOLUME_MUTE, 0), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
  }
}

Using this, you can mute, and increase or decrease Systemvolume by 2 degree. 使用此功能,您可以将系统音量静音和增加或减少2度。

I still searching a way to get the current Systemvolume. 我仍在寻找一种获取当前Systemvolume的方法。

I believe there is a way using nircmd. 我相信有一种使用nircmd的方法。

First download nircmd and attach it to the project: 首先下载nircmd并将其附加到项目:

http://www.nirsoft.net/utils/nircmd.html http://www.nirsoft.net/utils/nircmd.html

Then, call it via cmd: 然后,通过cmd调用它:

Run Command Prompt Commands 运行命令提示符命令

The commands you want are specified in the nircmd website. 您需要的命令在nircmd网站上指定。

for instance, to change the volume to x% use: 例如,要将音量更改为x%,请使用:

realativePath/nircmd.exe setsysvolume x realativePath / nircmd.exe setsysvolume x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM