c#将键盘命令发送到另一个窗口/进程 [英] c# Sending keyboard commands to another window / process

查看:169
本文介绍了c#将键盘命令发送到另一个窗口/进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我到目前为止所使用的代码,但我还没有能够解决如何发送键盘命令到OUTLOOK过程。



我想能够使用Tab命令/键和输入命令/键。



这是我到目前为止尝试的

  using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;

命名空间配置
{
类程序
{
[STAThread]
static void Main(string [] args)
{
System.Threading.Thread.Sleep(30); // 300000
TextReader tr = new StreamReader(config.txt);
Clipboard.SetText(tr.ReadLine());
tr.Close();

var proc = Process.GetProcessesByName(OUTLOOK)。FirstOrDefault();
if(proc!= null& proc.MainWindowHandle!= IntPtr.Zero)
{
SetForegroundWindow(proc.MainWindowHandle);
//SendKeys.Send(\"{ENTER});
// Clipboard.GetText();
}
}

[DllImport(user32)]
private static extern bool SetForegroundWindow(IntPtr hwnd);
}
}


解决方案

code> SendMessage 是您要找的。
也许您的问题已经在这里解决了:如何在C#/ Win32中向记事本发送文本?


I am trying to write a program that will take a line of data and pass it into another window / process.

This is the code I have so far, but I have not been able to work out how I would send the keyboard command to the OUTLOOK process.

I would like to be able to use the Tab command / key and the Enter command / key.

This is what I have tried so far

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;

namespace Config
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            System.Threading.Thread.Sleep(30);//300000
            TextReader tr = new StreamReader("config.txt");
            Clipboard.SetText(tr.ReadLine());
            tr.Close();

            var proc = Process.GetProcessesByName("OUTLOOK").FirstOrDefault();
            if (proc != null && proc.MainWindowHandle != IntPtr.Zero)
            {
                SetForegroundWindow(proc.MainWindowHandle);
                //SendKeys.Send("{ENTER}");
                //   Clipboard.GetText();
            }
        }

        [DllImport("user32")]
        private static extern bool SetForegroundWindow(IntPtr hwnd);
    }
}

解决方案

SendMessage is what you're looking for. Maybe your question was already solved here: How to send text to Notepad in C#/Win32?

这篇关于c#将键盘命令发送到另一个窗口/进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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