我试图用RichEdit从WM_GETTEXT字段中获取文本,但是遇到了一些问题,所以我找到了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#中使用这个的工作示例?
thx大卫
发布于 2011-03-15 02:30:19
请检查一下下面的例子是否对你有用:
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微软Sans Serif;}\viewkind4 4\uC1 \pard \qc\f0\f0 17测试段\par\pard测试段\
}
希望这能帮上忙
https://stackoverflow.com/questions/3236086
复制相似问题