在 C# 中将文本复制到记事本实例

Copying text to a notepad instance in C#

我知道这个问题已经有人回答了。这是我的问题。我有一个带有按钮和文本框的 Windows 表单。用户在文本框中输入信息,当用户单击该按钮时,将启动一个记事本实例,然后将文本框的文本加载到记事本中。

这是我的代码(我从这个网站上的一个问题中得到的)

  [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        [DllImport("User32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);

        private void btnCopyToNotepad_Click(object sender, EventArgs e)
        {
            StartNotepad();

            Process[] notepads = Process.GetProcessesByName("notepad");
            if (notepads.Length == 0) return;
            if (notepads[0] != null)
            {
                Clipboard.SetText(textBox1.Text);
                SendMessage(FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null), 0x000C, 0, textBox1.Text);
            }
        }
        private static void StartNotepad() 
        {
            Process.Start("notepad.exe");
        }

当我 运行 这段代码单步调试时,它 运行 没问题,并且逻辑按预期执行(将文本复制到记事本实例)。当我 运行 发布时,没有任何内容被复制到记事本实例中。任何想法为什么会这样?不,我不是 运行宁多个记事本实例..

您需要等到进程启动,然后发送文本:

    [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
    public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
    [DllImport("User32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);

    private void btnCopyToNotepad_Click(object sender, EventArgs e)
    {
        StartNotepad();

        Process[] notepads = null;
        while (notepads == null || notepads.Length == 0)
        {
            notepads = Process.GetProcessesByName("notepad");   
            Thread.Sleep(500);
        }

        if (notepads.Length == 0) return;
        if (notepads[0] != null)
        {
            Clipboard.SetText(textBox1.Text);
            SendMessage(FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null), 0x000C, 0, textBox1.Text);
        }
    }
    private static void StartNotepad() 
    {
        Process.Start("notepad.exe");
    }

可能是re-usable?顺便说一句,这每次都会打开一个新的记事本实例。

using System.Diagnostics;
using System.Runtime.InteropServices;

static class Notepad
{
    #region Imports
    [DllImport("user32.dll")]
    public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

    [DllImport("User32.dll")]
    private static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);

    //this is a constant indicating the window that we want to send a text message
    const int WM_SETTEXT = 0X000C;
    #endregion


    public static void SendText(string text)
    {
        Process notepad = Process.Start(@"notepad.exe");
        System.Threading.Thread.Sleep(50);
        IntPtr notepadTextbox = FindWindowEx(notepad.MainWindowHandle, IntPtr.Zero, "Edit", null);
        SendMessage(notepadTextbox, WM_SETTEXT, 0, text);
    }
}

Everton Santos 的版本可以将内容粘贴到错误的记事本实例中。每当用户打开其他记事本 windows 时,就会发生这种情况。粘贴到错误的记事本实例会覆盖该 window 的所有先前内容。撤消不起作用并导致用户自己的数据丢失。

这是一个包含指向正确记事本实例的指针的版本:

[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);

private void btnCopyToNotepad_Click(object sender, EventArgs e)
{
    var notepad = Process.Start("notepad.exe");
    notepad.WaitForInputIdle();
    Clipboard.SetText(textBox1.Text);
    SendMessage(FindWindowEx(notepad.MainWindowHandle, new IntPtr(0), "Edit", null), 0x000C, 0, textBox1.Text);
}