将文字写入另一个窗口

问题描述 投票:0回答:2

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

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;

        }

    }
}
c# text output
2个回答
1
投票

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

安装名为AutoItX.Dotnet的nuget包

那么这只是一个问题:

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

0
投票

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

    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()获取句柄

© www.soinside.com 2019 - 2024. All rights reserved.