如何在运行c#进程时不失去焦点
本文关键字:失去 焦点 进程 运行 | 更新日期: 2023-09-27 18:04:47
我试图在一个新的进程中运行mp3文件通过process.diagnostics
在c#中从控制台;然而,主机也有其他功能,所以我不想失去重点。我看了看论坛,最终我发现你需要使用两个属性来做到这一点:
StartInfo.CreateNoWindow = true; // has to be set to true
info.WindowStyle = ProcessWindowStyle.Hidden; // or minimized.
尽管如此,当我尝试以下操作时:
ProcessStartInfo info = new ProcessStartInfo(@"path to mp3 file here");
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(info);
我仍然没有对焦,一个带有mp3文件的windows媒体播放器打开并从控制台获取对焦。
我做错了什么?
问题是通过诊断创建进程不一致-它可能也可能不返回hProcess句柄,因此不会受到对该进程的操作的影响,因为它可以使用现有进程进行操作-并且您获得的句柄是没有意义的。
然而,这并不是结束。我发现了一个更好的方法来处理从控制台播放文件。System.Media.AudioPlayer
允许一个非常一致的,可预测的方式处理。wav文件。我用这个有用的工具转换了我的mp3,现在代码就像这样简单:
public SoundPlayer playOpeningTheme()
{
SoundPlayer myPlayer = new SoundPlayer(@"Path here.wav");
myPlayer.PlayLooping();
return myPlayer;
}
之后使用soundplayer对象来影响播放非常简单,例如:
SoundPlayer myPlayer = playOpeningTheme();
// do stuff while the music is playing
// and then easily, without all the process mess, you can simply:
myPlayer.Stop();
myPlayer.Dispose();
// and keep on from here (: