如何以编程方式更改系统卷
本文关键字:系统 方式更 编程 | 更新日期: 2023-09-27 18:15:37
我想更改系统的音量。我使用以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace shaped_form
{
class VolumeChange
{
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;
[DllImport("user32.dll")]
public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
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);
}
}
}
但是我在突出显示的地方得到了一个错误,如"shaped_form.VolumeChange does not contain a definition for Handle"
。
我从一些互联网资源得到这个代码。
你得到一个错误,因为this.Handle
没有定义在一个普通的香草类,如VolumeChange
类,你有那里。它来自system.windows.forms
form
类。
它需要,因为SendMessageW
方法需要一个窗口句柄
因为你没有在你的新类中定义它,所以你不能使用它。
尝试在VolumeChange
的构造函数中传递一个有效的窗口句柄,并使用它来调用SendMessageW
public class VolumeChange
{
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 IntPtr windowHandle;
public VolumeChange(IntPtr hwnd)
{
windowHandle = hwnd;
}
[DllImport("user32.dll")]
public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
private void btnMute_Click(object sender, EventArgs e)
{
SendMessageW(windowHandle, WM_APPCOMMAND, windowHandle,(IntPtr)APPCOMMAND_VOLUME_MUTE);
}
private void btnDecVol_Click(object sender, EventArgs e)
{
SendMessageW(windowHandle, WM_APPCOMMAND, windowHandle,(IntPtr)APPCOMMAND_VOLUME_DOWN);
}
private void btnIncVol_Click(object sender, EventArgs e)
{
SendMessageW(windowHandle, WM_APPCOMMAND,windowHandle,(IntPtr)APPCOMMAND_VOLUME_UP);
}
}
如果在构造函数中传递窗口句柄不是一个选项,(例如,如果您想使VolumeChange
为静态)
查看GetActiveWindow (MSDN)
要展开Timothy的答案,您应该将类VolumeChange提取到它自己的类中,并在单击表单按钮时使用它。这样,如果你想改变音量从其他形式或类,你也可以。
基本上,你最终有两个类,你的Form类和实用程序类VolumeChanger。在创建VolumeChanger实例时,通过表单的Handle属性获取所需的Handle。public class VolumeChanger
{
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;
public VolumeChange(IntPtr hwnd)
{
Handle = hwnd;
}
public IntPtr Handle { get; private set; }
[DllImport("user32.dll")]
public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
public void Mute() {
SendMessageW(Handle, WM_APPCOMMAND, Handle, (IntPtr)APPCOMMAND_VOLUME_MUTE);
}
public void IncreaseVolume() {
SendMessageW(Handle, WM_APPCOMMAND, Handle, (IntPtr)APPCOMMAND_VOLUME_UP);
}
public void DecreaseVolume() {
SendMessageW(Handle, WM_APPCOMMAND, Handle, (IntPtr)APPCOMMAND_VOLUME_DOWN);
}
}
public class Form1 : Form {
private VolumeChanger _volume;
public Form1() {
InitializeComponent();
_volume = new VolumeChanger(Handle);
}
private void btnMute_Click(object sender, EventArgs e)
{
_volume.Mute();
}
private void btnDecVol_Click(object sender, EventArgs e)
{
_volume.DecreaseVolume();
}
private void btnIncVol_Click(object sender, EventArgs e)
{
_volume.IncreaseVolume();
}
}