运行 c# 进程时如何不失去焦点?
How to not lose focus when running a c# process?
我正在尝试通过控制台中的 process.diagnostics
在新进程中 运行 一个 mp3 文件;但是,控制台会做其他事情,所以我不想失去注意力。我环顾了论坛,最终我认为您需要使用两个属性来执行此操作:
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 媒体播放器打开并从控制台获取焦点。
我做错了什么?
问题是通过诊断创建进程不一致 - 它可能会也可能不会 return hProcess 句柄返回,因此不会受到进程操作的影响,因为它可以使用现有的操作的过程 - 你得到的句柄是没有意义的。
然而,这并没有结束。我找到了一种更好的方法来处理从控制台播放的文件。
System.Media.AudioPlayer
允许以非常一致、可预测的方式处理 .wav 文件。我用 this useful tool 转换了我的 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 (:
我正在尝试通过控制台中的 process.diagnostics
在新进程中 运行 一个 mp3 文件;但是,控制台会做其他事情,所以我不想失去注意力。我环顾了论坛,最终我认为您需要使用两个属性来执行此操作:
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 媒体播放器打开并从控制台获取焦点。
我做错了什么?
问题是通过诊断创建进程不一致 - 它可能会也可能不会 return hProcess 句柄返回,因此不会受到进程操作的影响,因为它可以使用现有的操作的过程 - 你得到的句柄是没有意义的。
然而,这并没有结束。我找到了一种更好的方法来处理从控制台播放的文件。
System.Media.AudioPlayer
允许以非常一致、可预测的方式处理 .wav 文件。我用 this useful tool 转换了我的 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 (: