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

如何将F4键发送到C#中的进程?

我正在从 Windows应用程序启动一个进程.当我按下一个按钮时,我想模拟按F4键.我怎样才能做到这一点?

[稍后编辑]我不想以我的形式模拟F4键的按压,但在我开始的过程中.

解决方法

要将F4键发送到另一个进程,您将必须激活该进程

http://bytes.com/groups/net-c/230693-activate-other-process建议:

>获取Process.Start返回的类实例
> Query Process.MainWindowHandle
>调用非托管Win32 API函数“ShowWindow”或“SwitchToThisWindow”

然后,您可以使用System.Windows.Forms.SendKeys.Send(“{F4}”),作为Reed建议将击键发送到此进程

编辑:

下面的代码示例运行记事本并发送“ABC”到它:

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");
            }
    }
}

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

相关推荐