启动过程中没有偷焦点(C#) [英] Starting a process without stealing focus (C#)

查看:173
本文介绍了启动过程中没有偷焦点(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够没有它偷焦点启动过程(包括控制台和窗口)。 NET框架,我发现要做到这一点Microsoft.VisualBasic.Interaction.Shell与Microsoft.VisualBasic.AppWinStyle内的唯一途径。[最小化|标准]。NoFocus(它映射到SW_SHOWMINNOACTIVE / SW_SHOWMA正在通过传递给ShellExecute的)

I need to be able to start processes (both console and windowed) without it stealing focus. The only way within the .NET framework that I found to do this is Microsoft.VisualBasic.Interaction.Shell with Microsoft.VisualBasic.AppWinStyle.[Minimized|Normal]NoFocus (which map to SW_SHOWMINNOACTIVE/SW_SHOWMA being passed through to ShellExecute).

在我的code(这不偷焦点)的当前版本中,我使用的System.Diagnostics.Process,依靠一些,让我的功能,其中Interaction.Shell方法不。

In the current version of my code (which does steal focus), I am using System.Diagnostics.Process, and relying on some of the functionality that gives me, which the Interaction.Shell method does not.

2的问题(一个严肃,一个宣泄我无奈地说,我真的不指望一个很好的答案)

2 questions (one serious, and one venting my frustration that I don't really expect a good answer to)

1)。我是正确,我没有选择,只能包裹的CreateProcess或自己的ShellExecuteEx,还是我失去了一些其他的解决方案?我真的很希望避免这一点,因为流程是这样的比这个疏忽了完整的和有用的其他包装,而且会实现这么多的功能,P / Invoke调用调试,以及各种配套的痛苦。

1.) Am I correct that I have no choice but to wrap CreateProcess or ShellExecuteEx myself, or am I missing some other solution? I was really hoping to avoid this, as Process is such a complete and useful wrapper other than this oversight, and there would be so much functionality to implement, P/Invoke calls to debug, and all sorts of assorted pain.

2)为什么一支球队在微软创建一个这样的(否则)完整的包装,然后排除ProcessWindowStyle的可能值的一半,而另一个团队创建了一个类似的包装,是更完整,但所提供的所有有用窗口样式?

2.) Why would one team at Microsoft create such a (otherwise) complete wrapper, and then exclude half of the possible values from ProcessWindowStyle, while another team created a similar wrapper that was much less complete, but provided all the useful window styles?

推荐答案

在VB.Net团队已经做了许多有利于缓和东西有关窗口仪器仪表开发,我看不出有什么问题,增加了一个VB DLL的引用和使用在C#程序。

The VB.Net team has done much more to ease things for the developer regarding instrumentation of windows, and I see no problem adding a reference to a VB dll and use that in your C# program.

这是两队的侧重点不同,仅此而已。你不应该觉得不好,如果它解决您的问题用Microsoft.VisualBasic.Interaction.Shell。

It's two teams with different focus, that's all. And you shouldn't feel bad about using Microsoft.VisualBasic.Interaction.Shell if it solves your issue.

您也可以使用反射来看看实际的执行和落实code自己,如果你不想基准的DLL。

You can also use Reflector to see the actual implementation and implement the code yourself if you don't want to reference the dll.

int pid = Interaction.Shell("notepad.exe", AppWinStyle.NormalFocus);
Process p = Process.GetProcessById(pid);
p.Exited += ((o, e) => Console.WriteLine("Exit"));
p.EnableRaisingEvents = true;
Console.ReadLine();

在这里,我使用命令行方式揭开序幕的过程中,得到一个处理从PID的过程中,勾上的事件。你甚至可以以中止进程做p.Kill()。

Here I use the Shell method to kick off the process, get a handle to the process from the pid, and hook on events. You can even do p.Kill() in order to abort the process.

它开始成为一个有点hackish合我的口味,但它的作品。用随机GUID或东西,使其独特替换NEWWINDOW。

It's starting to become a bit hackish to my taste, but it works. Replace "NEWWINDOW" with a random guid or something to make it unique.

Microsoft.VisualBasic.Interaction.Shell(@"cmd.exe /c ""start cmd.exe /k title NEWWINDOW""", AppWinStyle.NormalFocus);
foreach (var process in Process.GetProcessesByName("cmd"))
{
    if (process.MainWindowTitle.EndsWith("NEWWINDOW"))
    {
        process.Exited += ((o, e) => Console.WriteLine("Exit"));
        process.EnableRaisingEvents = true;
    }
}

这篇关于启动过程中没有偷焦点(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆