在C#中将SendMessage发送到Notepad ++

时间:2017-02-22 21:26:05

标签: c# sendmessage

当我尝试将文本从我的RichTextBox发送到Notepad ++时,它只发送文本的第一个字母。因此,如果我在Notepad ++中的文本框Send this to Notepad++中显示的所有内容都是S

这是我的代码

[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 button2_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), "Scintilla", null);
            SendMessage(child, 0x000C, 0, RichTextBox1.Text);
        }
    }

1 个答案:

答案 0 :(得分:4)

您遇到了字符串编码问题。 .NET中的字符串是UTF-16 little-endian字符串。 UTF-16中的字符串S{0}e{0}n{0}d{0}{0}{0}实际上是字节SendMessage。您的SendMessage声明正在使用ANSI字符串方法。有很多方法可以解决这个问题。以下是:将SendMessageW更改为[DllImport("User32.dll")] public static extern int SendMessageW(IntPtr hWnd, int uMsg, int wParam, string lParam); ,明确使用UTF-16表单。

{{1}}