【发布时间】:2014-04-20 07:54:23
【问题描述】:
我有一个控制台程序,它需要一种方法来使 我的计算机上的声音静音。
这是执行此操作的代码,但它是为 WF 编写的。控制台程序有更简单的模拟吗?
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace VolumeOff
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("winmm.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern uint waveOutGetVolume(uint hwo, ref uint dwVolume);
[DllImport("winmm.dll", SetLastError = true, CallingConvention =
CallingConvention.Winapi)]
public static extern int waveOutSetVolume(uint uDeviceID, uint dwVolume);
private void button1_Click(object sender, EventArgs e)
{
uint volume = 0;
uint hWO = 0;
waveOutGetVolume(hWO, ref volume);
textBox1.Text = volume.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
uint hWO = 0;
waveOutSetVolume(hWO, Convert.ToUInt32(textBox1.Text.ToString()));
}
}
}
编辑 1: 顺便说一句,这段代码有点错误。使用此滑块调整系统托盘中的音量(在 Windows 中调整音量)不会移动。印象是我的应用不调节系统的音量,而是别的什么。
【问题讨论】:
-
如果你减少松弛,这基本上是 3 行代码。如果计算 using 语句,则为 5。不能更容易吗?
-
你有什么尝试以此来制作控制台应用程序?顺便说一句,此代码 sn-p 使用您通常希望避免的旧 API。
-
@TaW 什么?我不知道怎么做。如果你知道,然后显示