如何在C#/ Win32中将文本发送到记事本?

时间:2021-04-21 00:07:27

I'm trying to use SendMessage to Notepad, so that I can insert written text without making Notepad the active window.

我正在尝试将SendMessage用于记事本,这样我就可以插入书面文本而不会将记事本作为活动窗口。

I have done something like this in the past using SendText, but that required giving Notepad focus.

我过去使用SendText做了类似的事情,但这需要给予Notepad焦点。

Now, first I'm retrieving the Windows handle:

现在,首先我要检索Windows句柄:

Process[] processes = Process.GetProcessesByName("notepad");
Console.WriteLine(processes[0].MainWindowHandle.ToString());

I've confirmed it's the right handle for Notepad, the same shown within Windows Task Manager.

我已经确认它是Notepad的正确句柄,在Windows任务管理器中也是如此。

[DllImport("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);

From here, I haven't been able to get SendMessage to work in all my experimentation. Am I going in the wrong direction?

从这里开始,我无法让SendMessage在我的所有实验中工作。我走错了方向吗?

3 个解决方案

#1


    [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 button1_Click(object sender, EventArgs e)
    {
        Process [] notepads=Process.GetProcessesByName("notepad");
        if(notepads.Length==0)return;            
        if (notepads[0] != null)
        {
            IntPtr child= FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null);
            SendMessage(child, 0x000C, 0, textBox1.Text);
        }
    }

WM_SETTEXT=0x000c

#2


You first have to find the child window where the text is entered. You can do this by finding the child window with the window class "Edit". Once you have that window handle, use WM_GETTEXT to get the text which is already entered, then modify that text (e.g., add your own), then use WM_SETTEXT to send the modified text back.

首先必须找到输入文本的子窗口。您可以通过查找窗口类为“编辑”的子窗口来完成此操作。获得该窗口句柄后,使用WM_GETTEXT获取已输入的文本,然后修改该文本(例如,添加您自己的文本),然后使用WM_SETTEXT将修改后的文本发回。

#3


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

#1


    [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 button1_Click(object sender, EventArgs e)
    {
        Process [] notepads=Process.GetProcessesByName("notepad");
        if(notepads.Length==0)return;            
        if (notepads[0] != null)
        {
            IntPtr child= FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null);
            SendMessage(child, 0x000C, 0, textBox1.Text);
        }
    }

WM_SETTEXT=0x000c

#2


You first have to find the child window where the text is entered. You can do this by finding the child window with the window class "Edit". Once you have that window handle, use WM_GETTEXT to get the text which is already entered, then modify that text (e.g., add your own), then use WM_SETTEXT to send the modified text back.

首先必须找到输入文本的子窗口。您可以通过查找窗口类为“编辑”的子窗口来完成此操作。获得该窗口句柄后,使用WM_GETTEXT获取已输入的文本,然后修改该文本(例如,添加您自己的文本),然后使用WM_SETTEXT将修改后的文本发回。

#3


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