C# 在 .net 中切换窗口

标签 c# .net process dllimport

您好,我正在尝试使用 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/

相关文章:

c - 在进程(不是线程)中使用 semaphore.h

c# - 创建通用类型的字典

c# - 如何将对象注入(inject) WCF 的 IErrorHandler?

c# - session 在 IHttpModule 中不可用

c# - Entity Framework 存储过程单一结果集

.Net 哈希表 - Contains 与 ContainsKey

process - 准备用于流程挖掘的 csv 文件

dependencies - 有关管理发布依赖项的提示?

c# - 更改 DataGridCell 与其内容相关的背景

c# - IIS Express 工作进程不释放程序集