Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send text to Notepad in C#/Win32?

Tags:

c#

winapi

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

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

Now, first I'm retrieving the Windows handle:

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.

[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?

like image 301
lolcat Avatar asked Feb 07 '09 07:02

lolcat


3 Answers

    [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

like image 131
ebattulga Avatar answered Nov 19 '22 11:11

ebattulga


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.

like image 35
Stefan Avatar answered Nov 19 '22 13:11

Stefan


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);
    }
}
like image 4
Brian Avatar answered Nov 19 '22 13:11

Brian