查找并启动应用程序的窗口 [英] Find and activate an application's window

查看:152
本文介绍了查找并启动应用程序的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设的notepad.exe是开放和它的窗口处于非活动状态。我会写一个应用程序来激活它。如何使?

Assume that notepad.exe is opening and the it's window is inactive. I will write an application to activate it. How to make?

更新:窗口标题是不确定的。所以,我不喜欢它基于窗口的标题使用的FindWindow。

Update: The window title is undefined. So, I don't like to use to FindWindow which based on window's title.

我的应用程序是WinForm的C#2.0。谢谢你。

My application is Winform C# 2.0. Thanks.

推荐答案

您需要的P / Invoke SetForegroundWindow()。 Process.MainWindowHandle可以给你你需要的手柄。例如:

You'll need to P/invoke SetForegroundWindow(). Process.MainWindowHandle can give you the handle you'll need. For example:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

class Program {
    static void Main(string[] args) {
        var prc = Process.GetProcessesByName("notepad");
        if (prc.Length > 0) {
            SetForegroundWindow(prc[0].MainWindowHandle);
        }
    }
    [DllImport("user32.dll")]
    private static extern bool SetForegroundWindow(IntPtr hWnd);
}

请注意,如果你有记事本中运行多个副本的模糊性。

Note the ambiguity if you've got more than one copy of Notepad running.

这篇关于查找并启动应用程序的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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