微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 向程序发送击键

在窗体中,我做了一个按钮,我试图让它发送F1到一个特定的窗口(如FireFox,我的电脑等)

我的问题是:

>我该如何用窗口的名字来做? (如“Mozilla Firefox”)
>如何按进程的名称进行操作? (如firefox.exe)

解决方法

按窗口名称
[DllImport("User32.dll")] 
static extern IntPtr FindWindow(string lpClassName,string lpWindowName);  
[DllImport("User32.dll")] 
static extern int SetForegroundWindow(IntPtr hWnd);

IntPtr ptrFF = FindWindow(null,"Mozilla Firefox");
SetForegroundWindow(ptrFF);
SendKeys.SendWait("{F1}");

按进程名称

Process proc = Process.GetProcessesByName("firefox")[0];
IntPtr ptrFF = proc.Handle;
SetForegroundWindow(ptrFF);
SendKeys.SendWait("{F1}");

原文地址:https://www.jb51.cc/csharp/93531.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐