1

我想向当前选择的应用程序写入文本,但它的写入垃圾并导致发生奇怪的事情。

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

namespace i_allbwn
{
    class Program
    {

        static void Main(string[] args)
        {
            Thread.Sleep(500);
            ActionWithChance.brif_allbwn();

            Console.ReadKey();
        }
    }
    class ActionWithChance
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

        public const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key down flag
        public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag

        public static void brif_allbwn()
        {
            argraffu(new String[] {
                    "line1",
                    "line2",
                    "line3",

            }
                    );
        }

        public static void allbwn(Byte[] Name)
        {
            for (int i = 0; i < Name.Length; i++)
            {
                Console.WriteLine("Writing   " + (Char)Name[i]);
                keybd_event((Byte)Name[i], 0, KEYEVENTF_EXTENDEDKEY, 0);
                Thread.Sleep(10);
                keybd_event((Byte)Name[i], 0, KEYEVENTF_KEYUP, 0);
            }
        }
        public static void argraffu(String[] text)
        {
            foreach (String s in text)
            {
                allbwn(ToByteArray(s));
                keybd_event((Byte)'\r', 0, KEYEVENTF_EXTENDEDKEY, 0);
                Thread.Sleep(10);
                keybd_event((Byte)'\r', 0, KEYEVENTF_KEYUP, 0);
            }
        }

        public static Byte[] ToByteArray(String StringToConvert)
        {

            Char[] CharArray = StringToConvert.ToCharArray();

            Byte[] ByteArray = new Byte[CharArray.Length];

            for (int i = 0; i < CharArray.Length; i++)
            {

                ByteArray[i] = Convert.ToByte(CharArray[i]);

            }

            return ByteArray;

        }

    }
}
4

2 回答 2

1

我这样做的功能是这样的:

    public const Int32 WM_CHAR = 0x0102;

    public void SendKeys(string message)
    {
        foreach (char c in message)
        {
            int charValue = c;
            IntPtr val = new IntPtr((Int32)c);
            SendMessage(WindowHandle, WM_CHAR, val, new IntPtr(0));
        }
    }

基本上我正在做的是获取应用程序的句柄,例如:

    Process proc = Process.GetProcessesByName("Notepad")[0];

然后使用 proc.MainModule.Handle() 获取句柄

于 2013-02-15T22:22:18.883 回答
1

Autoit 库使与外部窗口的交互变得非常容易。

安装名为AutoItX.Dotnet

那么这只是一个问题:

using AutoIt;

class Program
{
    static void Main(string[] args)
    {
        AutoItX.Run("notepad.exe", null);
        AutoItX.WinWait("Untitled - Notepad");
        AutoItX.ControlSend("Untitled - Notepad", "", "[CLASSNN:Edit1]", "testing");

        //ControlSend is the ideal way to send text, but you can also pretend text was typed into the keyboard:
        AutoItX.Send("howdy pilgrim");
    }
}
于 2019-02-23T00:57:48.020 回答