使用C#将Message发送到Notepad ++ [英] SendMessage to Notepad++ in C#

查看:84
本文介绍了使用C#将Message发送到Notepad ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将文本从RichTextBox发送到Notepad ++时,它仅发送文本的第一个字母.因此,如果我在文本框中将发送到Notepad ++中的Notepad ++ ,则显示的全部是 S .

When I try to send text from my RichTextBox to Notepad++ it only sends the first letter of the text. So if I had in my text box Send this to Notepad++ in Notepad++ all that would show up is 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中的字符串发送" 实际上是字节 S {0} e {0} n {0} d {0} {0} {0} .您的 SendMessage 声明使用的是ANSI字符串方法.有很多方法可以解决此问题.这是一个:通过将 SendMessage 更改为 SendMessageW 来显式使用UTF-16形式.

You are running into a string encoding issue. Strings in .NET are UTF-16 little-endian strings. The string "Send" in UTF-16 is actually the bytes S{0}e{0}n{0}d{0}{0}{0}. Your declaration of SendMessage is using the ANSI string method. There are a number of ways to solve this. Here's one: Explicitly use the UTF-16 form by changing SendMessage to SendMessageW.

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

这篇关于使用C#将Message发送到Notepad ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆