将EM_STREAMOUT与c#和RichEditBox一起使用的示例 [英] Example of using EM_STREAMOUT with c# and RichEditBox

查看:274
本文介绍了将EM_STREAMOUT与c#和RichEditBox一起使用的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用WM_GETTEXT从RichEdit字段中获取文本,但是遇到了一些问题,所以我发现EM_STREAMOUT,尤其是对于RichEdit.我找到了此代码并播放了 一点点,但我不能让他们工作:

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);

也许有人有一个在c#中使用它的有效示例?

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

thx大卫

推荐答案

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

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;

您可以这样称呼它:

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

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

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测试 段落\ par \ pard测试段落\ par }

{\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 }

希望这会有所帮助

这篇关于将EM_STREAMOUT与c#和RichEditBox一起使用的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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