我如何发送F4键在C#中的程序? [英] How can I send the F4 key to a process in C#?
问题描述
我开始从Windows应用程序的过程。当我preSS一个按钮,我想模拟该过程中的关键<大骨节病> F4 骨节病>的pressing。我该怎么做?
I am starting a process from a Windows application. When I press a button I want to simulate the pressing of key F4 in that process. How can I do that?
[后来编辑]我不想模仿<大骨节病> F4 骨节病>在我的形式关键的pressing,但在这个过程中,我开始。
[Later edit] I don't want to simulate the pressing of the F4 key in my form, but in the process I started.
推荐答案
要发送F4键,另一个进程则必须激活过程
To send the F4 key to another process you will have to activate that process
<一个href=\"http://bytes.com/groups/net-c/230693-activate-other-process\">http://bytes.com/groups/net-c/230693-activate-other-process提示:
- 获取进程类的实例通过的Process.Start返回
- 查询Process.MainWindowHandle
- 调用非托管Win32 API函数的ShowWindow或SwitchToThisWindow
您然后可以使用System.Windows.Forms.SendKeys.Send({F4})作为里德建议击键发送到这个过程中。
You may then be able to use System.Windows.Forms.SendKeys.Send("{F4}") as Reed suggested to send the keystrokes to this process
编辑:
下code示例运行记事本,并发送ABC来了:
The code example below runs notepad and sends "ABC" to it:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace TextSendKeys
{
class Program
{
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
static void Main(string[] args)
{
Process notepad = new Process();
notepad.StartInfo.FileName = @"C:\Windows\Notepad.exe";
notepad.Start();
// Need to wait for notepad to start
notepad.WaitForInputIdle();
IntPtr p = notepad.MainWindowHandle;
ShowWindow(p, 1);
SendKeys.SendWait("ABC");
}
}
}
这篇关于我如何发送F4键在C#中的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!