假设 notepad.exe 正在打开并且它的窗口处于非事件状态。我会写一个应用程序来激活它。如何制作?
更新:窗口标题未定义。所以,我不喜欢使用基于窗口标题的 FindWindow。
我的应用程序是 Winform C# 2.0。谢谢。
最佳答案
您需要 P/调用 SetForegroundWindow()。 Process.MainWindowHandle 可以为您提供所需的句柄。例如:
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);
}
如果您有多个运行的记事本副本,请注意歧义。
关于c# - 查找并激活应用程序的窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2817707/