Open Notepad and add Text not working

白昼怎懂夜的黑 提交于 2019-12-13 04:42:57

问题


I just saw this snippet from the internet but it doesn't work from me. It's suppose to open a new notepad application and add "asdf" into it.

Is there any wrong on the code?

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

void Test()
{
         const int WM_SETTEXT = 0x000C;

    ProcessStartInfo startInfo = new ProcessStartInfo("notepad.exe");
    startInfo.UseShellExecute = false;
    Process notepad = System.Diagnostics.Process.Start(startInfo);
    SendMessage(notepad.MainWindowHandle, WM_SETTEXT, 0, "asdf");
}

回答1:


To make sure that process is ready to accept input, call notepad.WaitForInputIdle(). And it is important to use MainWindowHandle of the process that was just created, not the any notepad process.

[DllImport("user32.dll")]
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);

static void ExportToNotepad(string text)
{
    ProcessStartInfo startInfo = new ProcessStartInfo("notepad");
    startInfo.UseShellExecute = false;

    Process notepad = Process.Start(startInfo);
    notepad.WaitForInputIdle();

    IntPtr child = FindWindowEx(notepad.MainWindowHandle, new IntPtr(0), null, null);
    SendMessage(child, 0x000c, 0, text);
}



回答2:


Following Code will do the trick for you,

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



回答3:


try this:

    [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);
        void Test()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo("notepad.exe");
            startInfo.UseShellExecute = false;
            Process notepad = System.Diagnostics.Process.Start(startInfo);
            //Wait Until Notpad Opened
            Thread.Sleep(100);
            Process[] notepads = Process.GetProcessesByName("notepad");
            IntPtr child = FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null);
            SendMessage(child, 0x000c, 0, "test");
        }


来源:https://stackoverflow.com/questions/11304755/open-notepad-and-add-text-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!