Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将消息发送到记事本++;在C中#_C#_Sendmessage - Fatal编程技术网

C# 将消息发送到记事本++;在C中#

C# 将消息发送到记事本++;在C中#,c#,sendmessage,C#,Sendmessage,当我尝试将RichTextBox中的文本发送到Notepad++时,它只发送文本的第一个字母。因此,如果我的文本框中有将其发送到Notepad++中的Notepad++,那么将显示的是S 这是我的密码 [DllImport("user32.dll", EntryPoint = "FindWindowEx")] public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string

当我尝试将RichTextBox中的文本发送到Notepad++时,它只发送文本的第一个字母。因此,如果我的文本框中有
将其发送到Notepad++中的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);
        }
    }

您遇到了字符串编码问题。NET中的字符串是UTF-16小端字符串。UTF-16中的字符串
“Send”
实际上是字节
S{0}e{0}n{0}d{0}{0}{0}
。您的
SendMessage
声明使用的是ANSI字符串方法。有很多方法可以解决这个问题。这里有一个:通过将
SendMessage
更改为
SendMessageW
来显式使用UTF-16表单

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

伟大的还有许多其他选项使用
DllImport
的某些属性和
marshallas
属性(在参数上)。但这是最简洁的。别忘了把我的答案标为已接受。:)我想将你的答案标记为已接受,但遗憾的是,我不是发布问题的人:出于某种原因,当我使用Visual Studio 2015在我的Windows 8.1计算机上编译和运行我的程序时,它什么都不做,但当我使用Windows 10计算机编译和运行程序时,它工作正常。Windows 8.1是否需要与Windows 10CharSet=CharSet.Unicode不同的东西。标准建议:使用UI自动化来避免这些细节出错。System.Windows.Automation命名空间。