有人可以解释一下如何通过C#切换到另一个正在运行的程序吗?
例如,如果我创建一个带有按钮事件的新应用程序 - 我想使用该按钮来调用打开的应用程序,如Internet Explorer或word。
我知道如何启动应用程序但不太确定如何在它们运行时调用它们。
这是目前为止所做的工作:
if (System.Diagnostics.Process.GetProcessesByName("ExternalApplication").Length >= 1)
{
foreach (Process ObjProcess in System.Diagnostics.Process.GetProcessesByName("ExternalApplication"))
{
ActivateApplication(ObjProcess.Id);
Interaction.AppActivate(ObjProcess.Id);
SendKeys.SendWait("~");
}
}
答案 0 :(得分:5)
我必须解决类似的问题,我不得不做一些pInvoking来完成它。请参阅下面的代码。
delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);
public static class WindowEnumerator
{
[DllImport("user32.dll", SetLastError = true)]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("USER32.DLL")]
private static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);
[DllImport("USER32.DLL")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("USER32.DLL")]
private static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("USER32.DLL")]
private static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("USER32.DLL")]
private static extern IntPtr GetShellWindow();
public static IDictionary<IntPtr, string> GetOpenWindowsFromPID(int processID)
{
IntPtr hShellWindow = GetShellWindow();
Dictionary<IntPtr, string> dictWindows = new Dictionary<IntPtr, string>();
EnumWindows(delegate(IntPtr hWnd, int lParam)
{
if (hWnd == hShellWindow) return true;
if (!IsWindowVisible(hWnd)) return true;
int length = GetWindowTextLength(hWnd);
if (length == 0) return true;
uint windowPid;
GetWindowThreadProcessId(hWnd, out windowPid);
if (windowPid != processID) return true;
StringBuilder stringBuilder = new StringBuilder(length);
GetWindowText(hWnd, stringBuilder, length + 1);
dictWindows.Add(hWnd, stringBuilder.ToString());
return true;
}, 0);
return dictWindows;
}
}
...
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
...
Process yourProcess = ???;
Dictionary<IntPtr, string> windows = (Dictionary<IntPtr, string>)WindowEnumerator.GetOpenWindowsFromPID(yourProcess.Id);
IntPtr mainWindowHandle = IntPtr.Zero;
foreach (KeyValuePair<IntPtr, string> pair in windows)
{
if (pair.Value.ToUpperInvariant() == "Main Window Title")
{
mainWindowHandle = pair.Key;
break;
}
}
if (mainWindowHandle != IntPtr.Zero)
{
if (IsIconic(mainWindowHandle))
{
ShowWindow(mainWindowHandle, 9);
}
SetForegroundWindow(mainWindowHandle);
}
答案 1 :(得分:1)
我正在使用以下内容,但我认为没有在第31行正确定义进程名称。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace test_winform_app
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
private void button1_Click(object sender, EventArgs e)
{
Process yourProcess = Process.GetProcessesByName("notepad");
Dictionary<IntPtr, string> windows = (Dictionary<IntPtr, string>)WindowEnumerator.GetOpenWindowsFromPID(yourProcess.Id);
IntPtr mainWindowHandle = IntPtr.Zero;
foreach (KeyValuePair<IntPtr, string> pair in windows)
{
if (pair.Value.ToUpperInvariant() == "Main Window Title")
{
mainWindowHandle = pair.Key;
break;
}
}
if (mainWindowHandle != IntPtr.Zero)
{
if (IsIconic(mainWindowHandle))
{
ShowWindow(mainWindowHandle, 9);
}
SetForegroundWindow(mainWindowHandle);
}
}
delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);
public static class WindowEnumerator
{
[DllImport("user32.dll", SetLastError = true)]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("USER32.DLL")]
private static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);
[DllImport("USER32.DLL")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("USER32.DLL")]
private static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("USER32.DLL")]
private static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("USER32.DLL")]
private static extern IntPtr GetShellWindow();
public static IDictionary<IntPtr, string> GetOpenWindowsFromPID(int processID)
{
IntPtr hShellWindow = GetShellWindow();
Dictionary<IntPtr, string> dictWindows = new Dictionary<IntPtr, string>();
EnumWindows(delegate(IntPtr hWnd, int lParam)
{
if (hWnd == hShellWindow) return true;
if (!IsWindowVisible(hWnd)) return true;
int length = GetWindowTextLength(hWnd);
if (length == 0) return true;
uint windowPid;
GetWindowThreadProcessId(hWnd, out windowPid);
if (windowPid != processID) return true;
StringBuilder stringBuilder = new StringBuilder(length);
GetWindowText(hWnd, stringBuilder, length + 1);
dictWindows.Add(hWnd, stringBuilder.ToString());
return true;
}, 0);
return dictWindows;
}
}
}
}
答案 2 :(得分:1)
以下代码为我工作
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace SingleInstanceWindow
{
static class Program
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
var me = Process.GetCurrentProcess();
var arrProcesses = Process.GetProcessesByName(me.ProcessName);
if (arrProcesses.Length > 1)
{
SetForegroundWindow(arrProcesses[0].MainWindowHandle);
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
如果窗口最小化,上面的代码不起作用
代码:
[DllImport("user32.dll")]
public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
用法:
SwitchToThisWindow(arrProcesses[0].MainWindowHandle, true);