Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向程序发送击键_C#_Winforms_Sendkeys_Keystroke - Fatal编程技术网

C# 向程序发送击键

C# 向程序发送击键,c#,winforms,sendkeys,keystroke,C#,Winforms,Sendkeys,Keystroke,在窗口窗体中,我制作了一个按钮,并试图使其将F1发送到特定窗口(如FireFox、我的电脑等) 我的问题是: 我怎么用窗户的名字来做?(如“Mozilla Firefox”) 如何按流程的名称进行操作?(如firefox.exe) 查看课程。按窗口名称: [DllImport("User32.dll")] static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("User32

在窗口窗体中,我制作了一个按钮,并试图使其将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}");

@或Betzalel-使用过程名称更新。用于社区文档目的。根据我的经验,“IntPtr ptrFF=proc.Handle;”不会抓住正确的句柄。您应该改用proc.MainWindowHandle。使用Spy确保这是正确的++