我想更改系统的音量。我使用以下代码:
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"
。
我从一些互联网资源中获取此代码。
答案 0 :(得分:6)
您收到错误是因为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
静态)
答案 1 :(得分:1)
要扩展Timothy的答案,您应该将类VolumeChange提取到自己的类,并从表单按钮单击中使用它。这样,如果你想改变其他表格或类的音量,你也可以这样做。
基本上,您最终会得到两个类,即Form类和实用程序类VolumeChanger。通过表单的Handle属性创建VolumeChanger实例时,可以检索所需的句柄。
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();
}
}