FindWindow 和 SetForegroundWindow 替代品? [英] FindWindow and SetForegroundWindow alternatives?

查看:20
本文介绍了FindWindow 和 SetForegroundWindow 替代品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找使用 FindWindow()SetForegroundWindow() 切换到不同应用程序的旧 User32.dll 版本的替代方案.

I am searching for alternatives to the old User32.dll version of switching to a different application with FindWindow() and SetForegroundWindow().

我确实找到了第一个使用 Process.GetProcessesByName() 的替代方法,但我没有看到相应的方法来切换(设置活动/前台)到该应用程序.

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.

有没有办法不使用 User32.dll方式来做到这一点?

Is there a way of doing that without using the old way with the User32.dll?

感谢您的帮助.

编辑

我接受了@Sorceri 的答案,尽管这不是我想要的答案.

I accepted the answer of @Sorceri although it is not the answer I was looking for.

推荐答案

答案:否

但是,为了帮助下一个想找到一个窗口并从 C# 激活它的好奇者,您必须执行以下操作:

But, to help the next wonderer looking to find a window and activate it from C# here's what you have to do:

[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");

<小时>

附带说明 - 对于那些试图将窗口在您的应用程序中置于前台的人,只需调用 Activate() 方法.


As a side note - for those of you who are trying to bring a window within your application to the foreground just call the Activate() method.

这篇关于FindWindow 和 SetForegroundWindow 替代品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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