//新增列舉
public enum 發聲
{
待檢測音效卡
, 有音效卡
, 無音效卡
}
public static class msgboxExtensions
(
static 發聲 發聲 = 發聲.待檢測音效卡;
//發聲函式
private static dynamic spVoice = null;
public static void say(this string msg)
{
if (發聲 == 發聲.待檢測音效卡)
{
try
{
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_SoundDevice");
var soundDevices = searcher.Get();
if (soundDevices.Count > 0)
{
發聲 = 發聲.有音效卡;
Console.WriteLine("系統中存在音效卡。");
}
else
{
發聲 = 發聲.無音效卡;
Console.WriteLine("未檢測到音效卡。");
}
}
catch (Exception ex)
{
Console.WriteLine($"發生錯誤:{ex.Message}");
}
}
if (發聲 == 發聲.有音效卡)
{
if (spVoice == null)
{
Type type = Type.GetTypeFromProgID("SAPI.SpVoice");
spVoice = Activator.CreateInstance(type);
}
Thread newThread = new Thread(new ThreadStart(() =>
{
spVoice.Speak(msg);
}));
newThread.IsBackground = true;
newThread.Start();
}
msg.messageBox();
}
}