Mar*_*arv 1 c# wpf powerpoint .net-3.5
我编写了一个程序,当用户可以自己设置的时间间隔过去时,会弹出并播放声音。
现在,我希望当 Powerpoint 在演示模式下运行并且间隔时间结束时它保持静音,这样程序就不会出现在屏幕顶部,并且在向外部观众进行演示时不会播放声音。
使用的 PowerPoint 版本是 07/10/13 (12.0/14.0/15.0) 我找不到任何方法来确定演示模式是否正在运行。
该程序不是 PowerPoint 插件或类似的东西,只是一个普通的 WPF 桌面应用程序。
抱歉,如果这看起来有点贪婪地回答我自己的问题,但我认为这个答案将帮助有同样问题的人:
只需添加名为“Microsoft PowerPoint 15.0 Object Libary”的 COM 引用 - 它在引用列表中显示为“Microsoft.Office.Interop.PowerPoint”
以下代码测试了运行演示文稿的情况,并测试了其适用于版本 2007/10/13 (12.0/14.0/15.0):
var PPT = new Microsoft.Office.Interop.PowerPoint.Application();
if (PPT.SlideShowWindows.Count > 0)
{ //a PowerPoint Presentation mode is currently running}
else
{//there is no PowerPoint Presentation mode running}
Run Code Online (Sandbox Code Playgroud)
一些错误报告表明,如果 PowerPoint 根本没有运行或演示模式未处于活动状态,仅按上述方式执行可能会导致异常,因此我稍微修改了代码:
private bool IsPPTPresentationRunning()
{
Process[] prozesse = Process.GetProcesses();
foreach (Process p in prozesse)
{//searches for a running PowerPoint process
if (p.ProcessName == "POWERPNT")
{
try
{
Microsoft.Office.Interop.PowerPoint.Application PPT =
new Microsoft.Office.Interop.PowerPoint.Application();
if (PPT.SlideShowWindows.Count > 0)
return true;
else
return false;
}
//Catches any exception that seems to get thrown when
// powerpoint is not in Presentation mode
catch (Exception)
{
return false;
}
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)