user32.dll SendMessage VolumeUp和VolumeDown问题

时间:2015-02-10 04:05:51

标签: c# unity3d sendmessage user32 findwindow

我试图创建一个可以控制另一个应用程序的音量的Unity3D应用程序。到目前为止,我已经让它改变了所有应用程序的数量,但是我很难让它只改变一个,例如Spotify的。这是在Windows 8上,但我也希望它适用于Windows 7。

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 SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string strClassName, string strWindowName);

[DllImport("user32.dll", SetLastError = true)]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);

[DllImport("user32.dll")]
public static extern IntPtr GetActiveWindow();

public void DecreaseVolume()
{
    IntPtr hWnd = FindWindow("SpotifyMainWindow", "Spotify");

    if (hWnd == IntPtr.Zero)
        return;

    uint pID;
    GetWindowThreadProcessId(hWnd, out pID);

    Debug.Log(pID);

    SendMessage(hWnd, WM_APPCOMMAND, hWnd, (IntPtr)APPCOMMAND_VOLUME_DOWN);
}

public void IncreaseVolume()
{
    // same as above, but with APPCOMMAND_VOLUME_UP
}

我非常确定我从FindWindow()获取了正确的句柄(hWnd),因为打印出的pID与我的任务管理器中的Spotify的PID相匹配,但是所有这一切都改变了音量一切都取代了Spotify。这与我没有指定特定句柄的结果相同,例如

SendMessage(IntPtr.Zero, WM_APPCOMMAND, IntPtr.Zero, (IntPtr)APPCOMMAND_VOLUME_DOWN);

或者如果我使用GetActiveWindow()

直到今天,我还没有弄乱这些windows api的东西,所以任何帮助都会非常感激!

0 个答案:

没有答案