将 EM_STREAMOUT 与 c# 和 RichEditBox 一起使用的示例

发布于 2024-09-09 22:04:14 字数 526 浏览 1 评论 0原文

我试图使用 WM_GETTEXT 从 RichEdit 字段获取文本,但遇到了一些问题,所以我找到了 EM_STREAMOUT,这尤其适用于 RichEdit。我找到了这段代码并玩了 稍微用了一下,但我无法让它们工作:

delegate uint EditStreamCallback(IntPtr dwCookie, IntPtr pbBuff, int cb, out int pcb);

struct EDITSTREAM
{
public IntPtr dwCookie;
public uint dwError;
public EditStreamCallback pfnCallback;
}

[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern IntPtr SendMessage(HandleRef hwnd, uint msg, uint wParam, ref EDITSTREAM lParam);

也许有人有一个在 C# 中使用它的工作示例?

谢谢大卫

i trying to get a text from a RichEdit field with WM_GETTEXT, but i run into some problems, so I found EM_STREAMOUT, this is especially for RichEdit. I found this code and played
a little bit with it, but i can't get them to work:

delegate uint EditStreamCallback(IntPtr dwCookie, IntPtr pbBuff, int cb, out int pcb);

struct EDITSTREAM
{
public IntPtr dwCookie;
public uint dwError;
public EditStreamCallback pfnCallback;
}

[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern IntPtr SendMessage(HandleRef hwnd, uint msg, uint wParam, ref EDITSTREAM lParam);

maybe someone have a working example of using this in c#?

thx david

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小红帽 2024-09-16 22:04:14

请检查下面的示例是否适合您:

private string ReadRTF(IntPtr handle)
{
    string result = String.Empty;
    using (MemoryStream stream = new MemoryStream())
    {
        EDITSTREAM editStream = new EDITSTREAM();
        editStream.pfnCallback = new EditStreamCallback(EditStreamProc);
        editStream.dwCookie = stream;

        SendMessage(handle, EM_STREAMOUT, SF_RTF, editStream);

        stream.Seek(0, SeekOrigin.Begin);
        using (StreamReader reader = new StreamReader(stream))
        {
            result = reader.ReadToEnd(); 
        }
    }
    return result;
}

private int EditStreamProc(MemoryStream dwCookie, IntPtr pbBuff, int cb, out int pcb)
{
    pcb = cb;
    byte[] buffer = new byte[cb];
    Marshal.Copy(pbBuff, buffer, 0, cb);
    dwCookie.Write(buffer, 0, cb);
    return 0;
}

private delegate int EditStreamCallback(MemoryStream dwCookie, IntPtr pbBuff, int cb, out int pcb);

[StructLayout(LayoutKind.Sequential)]
private class EDITSTREAM
{
    public MemoryStream dwCookie;
    public int dwError;
    public EditStreamCallback pfnCallback;
}

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(HandleRef hwnd, uint msg, uint wParam, ref EDITSTREAM lParam);

public const int WM_USER = 0x0400;
public const int EM_STREAMOUT = WM_USER + 74;
public const int SF_RTF = 2;

以下是您如何调用它:

string temp = ReadRTF(richTextBox1.Handle);
Console.WriteLine(temp);

在我的测试 richedit 上,这将返回以下字符串:

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0
微软无衬线字体;}}
\viewkind4\uc1\pard\qc\f0\fs17 测试
段落\par \pard 测试段落\par
}

这有帮助,问候

Pls, check if an example below would work for you:

private string ReadRTF(IntPtr handle)
{
    string result = String.Empty;
    using (MemoryStream stream = new MemoryStream())
    {
        EDITSTREAM editStream = new EDITSTREAM();
        editStream.pfnCallback = new EditStreamCallback(EditStreamProc);
        editStream.dwCookie = stream;

        SendMessage(handle, EM_STREAMOUT, SF_RTF, editStream);

        stream.Seek(0, SeekOrigin.Begin);
        using (StreamReader reader = new StreamReader(stream))
        {
            result = reader.ReadToEnd(); 
        }
    }
    return result;
}

private int EditStreamProc(MemoryStream dwCookie, IntPtr pbBuff, int cb, out int pcb)
{
    pcb = cb;
    byte[] buffer = new byte[cb];
    Marshal.Copy(pbBuff, buffer, 0, cb);
    dwCookie.Write(buffer, 0, cb);
    return 0;
}

private delegate int EditStreamCallback(MemoryStream dwCookie, IntPtr pbBuff, int cb, out int pcb);

[StructLayout(LayoutKind.Sequential)]
private class EDITSTREAM
{
    public MemoryStream dwCookie;
    public int dwError;
    public EditStreamCallback pfnCallback;
}

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(HandleRef hwnd, uint msg, uint wParam, ref EDITSTREAM lParam);

public const int WM_USER = 0x0400;
public const int EM_STREAMOUT = WM_USER + 74;
public const int SF_RTF = 2;

here's how you can call this:

string temp = ReadRTF(richTextBox1.Handle);
Console.WriteLine(temp);

on my test richedit this returns following string:

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0
Microsoft Sans Serif;}}
\viewkind4\uc1\pard\qc\f0\fs17 test
paragraph\par \pard test paragraph\par
}

hope this helps, regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文