我如何发送F4键在C#中的程序? [英] How can I send the F4 key to a process in C#?

查看:300
本文介绍了我如何发送F4键在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提示:


  1. 获取进程类的实例通过的Process.Start返回

  2. 查询Process.MainWindowHandle

  3. 调用非托管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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆