C# FindWindow 和 SetForegroundWindow 替代品?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11512373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FindWindow and SetForegroundWindow alternatives?
提问by fdomig
I am searching for alternatives to the old User32.dllversion of switching to a different application with FindWindow()and SetForegroundWindow().
我正在寻找User32.dll使用FindWindow()和切换到不同应用程序的旧版本的替代方案SetForegroundWindow()。
I did find an alternative to the first with the usage of Process.GetProcessesByName()but I do not see the corresponding method to switch (set active/foreground) to that application.
我确实找到了第一个的替代方法,Process.GetProcessesByName()但我没有看到相应的方法来切换(设置活动/前台)到该应用程序。
Is there a way of doing that without using the oldway with the User32.dll?
有没有办法在不使用旧方法的情况下做到这一点User32.dll?
Thank you for your help.
感谢您的帮助。
EDIT
编辑
I accepted the answer of @Sorceri although it is not the answer I was looking for.
我接受了@Sorceri 的答案,尽管这不是我想要的答案。
采纳答案by noelicus
Answer: No.
回答:没有。
But, to help the next wonderer looking to find a window and activate it from C# here's what you have to do:
但是,为了帮助下一个想找到一个窗口并从 C# 激活它的好奇者,您必须执行以下操作:
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
void ActivateApp(string processName)
{
Process[] p = Process.GetProcessesByName(processName);
// Activate the first application we find with this name
if (p.Count() > 0)
SetForegroundWindow(p[0].MainWindowHandle);
}
To bring notepad to the front, for example, you would call:
例如,要将记事本放在前面,您可以调用:
ActivateApp("notepad");
As a side note - for those of you who are trying to bring a window within your applicationto the foreground just call the Activate() method.
作为旁注 - 对于那些试图将应用程序中的窗口置于前台的人,只需调用Activate() 方法。
回答by Sorceri
You can use System.Diagnostics.Process Objectfor a FindWindow equivalent. There currently is no equivalent for SetForegroundWindow. You will want use Pinvoke with SetForgroundWindow.
您可以使用System.Diagnostics.Process ObjectFindWindow 等效项。目前没有 SetForegroundWindow 的等效项。您需要将 Pinvoke 与 SetForgroundWindow 结合使用。
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
回答by Chibueze Opata
You could use SetActiveWindowas an alternative to SetForeGroundWindow. I'd say you should go through all the Windows Manipulation Api Functionsand see if there's something you're missing out.
您可以将其SetActiveWindow用作SetForeGroundWindow. 我会说你应该浏览所有的Windows 操作 Api 函数,看看是否有你错过的东西。
Also, note that you can obtain the handle of the System.Diagnostics.Processobject via the Process.Handleproperty.
另外请注意,您可以System.Diagnostics.Process通过Process.Handle属性获取对象的句柄。
回答by Mark Arnott
An alternative to SetForeGroundWindow is VisualBasic's AppActivate
SetForeGroundWindow 的替代方案是 VisualBasic 的 AppActivate
Call it like this
像这样称呼
Microsoft.VisualBasic.Interaction.AppActivate("WindowTitle")
Just because it is in the VisualBasic namespace doesn't mean you can't use it in C#.
仅仅因为它在 VisualBasic 命名空间中并不意味着您不能在 C# 中使用它。
Full Documentation here
完整文档在这里

