tags:

views:

3217

answers:

3

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?

+2  A: 

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.

Stefan
+3  A: 
    [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

ebattulga
A: 

hey guys...i'm trying to implement a remote desktop like application..one of the things i want to implement in it is to able to type some info on a notepad which i use to display back at the remote desktop...can you please give me a fully working code for which atleast i can send textual info across to a notepad in my system only so that i can understand how it works?