我正在尝试以编程方式更改PC上的音量但是我没有使用此代码获得任何结果,我在此处(How to programmatically set the system volume?)执行了另一个答案来执行此操作。没有错误,但是当我调用该函数时没有任何反应,任何人都可以帮忙吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace UnRestrict.Forms
{
static class Sounds
{
//hexidecimal values for the values needed
private const int VOLUP = 0xA0000;
private const int VOLDOWN = 0x90000;
private const int VOLMUTE = 0x80000;
private const int APPCMND = 0x319;
[DllImport("user32.dll")]
public static extern IntPtr SendMessageW(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
public static void VolumeUp()
{
SendMessageW(IntPtr.Zero, APPCMND, IntPtr.Zero, (IntPtr) VOLUP);
}
public static void VolumeDown()
{
SendMessageW(IntPtr.Zero, APPCMND, IntPtr.Zero, (IntPtr)VOLDOWN);
}
public static void VolumeMute()
{
SendMessageW(IntPtr.Zero, APPCMND, IntPtr.Zero, (IntPtr)VOLMUTE);
}
}
}
答案 0 :(得分:0)
我修复了它,我在某个地方读到了这句话,SendMessageW()
对hwnd
和wParam
参数没有任何作用,你可以传递一个空指针(IntPtr.Zero)
而没有问题。这是我错的地方。我将IntPtr.Zero值更改为Process.GetCurrentProcess().Handle
,现在一切都很好!
奇怪的是,返回的vlaue仍然不是'1',但我也读到这可能是正常的。
最终守则:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UnRestrict.Forms
{
static class Sounds
{
//hexidecimal values for the values needed
private const int VOLUP = 0xA0000;
private const int VOLDOWN = 0x90000;
private const int VOLMUTE = 0x80000;
private const int APPCMND = 0x319;
[DllImport("user32.dll")]
public static extern IntPtr SendMessageW(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
public static void VolumeUp()
{
int val = (Int32)SendMessageW(Process.GetCurrentProcess().Handle, APPCMND, Process.GetCurrentProcess().Handle, (IntPtr)VOLUP);
}
public static void VolumeDown()
{
SendMessageW(Process.GetCurrentProcess().Handle, APPCMND, Process.GetCurrentProcess().Handle, (IntPtr)VOLDOWN);
}
public static void VolumeMute()
{
SendMessageW(Process.GetCurrentProcess().Handle, APPCMND, Process.GetCurrentProcess().Handle, (IntPtr)VOLMUTE);
}
}
}