我试图让我的程序发送消息以控制音量,但我需要一个句柄。我目前尝试在当前代码中使用如下所示的广播,但这会导致所有进程发送一条消息,导致音量上升到 100 或 0 并冻结程序。我试过使用 Process.GetCurrentProcess().MainWindowHandle; 但这仅在我有表单或命令提示符时才有效。注意我希望使用我对当前代码的修改来让这个程序工作。
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace Utilities
{
static class VolumeMessage
{
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 static IntPtr Handle = (IntPtr)0xffff;
[DllImport("user32.dll")]
public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg,
IntPtr wParam, IntPtr lParam);
public static void Mute()
{
SendMessageW(Handle, WM_APPCOMMAND, Handle,
(IntPtr)APPCOMMAND_VOLUME_MUTE);
}
public static void volumeDown()
{
SendMessageW(Handle, WM_APPCOMMAND, Handle,
(IntPtr)APPCOMMAND_VOLUME_DOWN);
}
public static void volumeUp()
{
SendMessageW(Handle, WM_APPCOMMAND, Handle,
(IntPtr)APPCOMMAND_VOLUME_UP);
}
}
}