1

インターネットからこのスニペットを見たばかりですが、うまくいきません。新しいメモ帳アプリケーションを開き、それに「asdf」を追加するとします。

コードに誤りはありませんか?

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

3 に答える 3

4

プロセスが入力を受け入れる準備ができていることを確認するには、 を呼び出しますnotepad.WaitForInputIdle()MainWindowHandleまた、任意のメモ帳プロセスではなく、作成したばかりのプロセスを使用することが重要です。

[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);
}
于 2015-05-13T15:45:00.987 に答える
0

次のコードはあなたのためにトリックを行います、

[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);
        }
    }
于 2012-07-03T05:31:37.057 に答える
0

これを試して:

    [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");
        }
于 2012-07-03T05:45:29.990 に答える