您好,我正在尝试使用 C# 将窗口切换到正在运行(即使已最小化)的其他程序。
我想知道为什么这行不通。
错误信息: 参数 1:无法从“System.Diagnostics.Process”转换为“System.IntPtr”
当它进入循环时,我认为 proc 变量会引用适当的窗口处理程序。这不是真的吗? 非常感谢您的帮助。
//declarations
using system.IO;
using System.Runtime.InteropServices;
//more
//namespace here
//class here
//initialize method
//related .dll import
[DllImport("user32.dll")]
public static extern void SwitchToThisWindow(IntPtr hWnd);
String ProcWindow = "itunes";
//function which calls switchWindow() is here but not important
//now we have switch window.
private void switchWindow()
{
Process[] procs = Process.GetProcessesByName(ProcWindow);
foreach (Process proc in procs)
{
//switch to process by name
SwitchToThisWindow(proc);
}
}
对于 future 的读者: 我在我的代码中从另一个问题到了这一点。 Correct way (in .NET) to switch the focus to another application
最佳答案
SwitchToThisWindow 需要一个句柄 到您要在该进程中切换到的窗口。
尝试
SwitchToThisWindow(proc.MainWindowHandle);
关于C# 在 .net 中切换窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18364504/