Pls, vérifier si un exemple ci-dessous fonctionnerait pour vous:
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;
est ici comment vous pouvez appeler ceci:
string temp = ReadRTF(richTextBox1.Handle);
Console.WriteLine(temp);
sur mon test richedit ce rendement suivant chaîne:
{\ rtf1 \ ansi \ ansicpg1252 \ deff0 \ deflang1033 {\ fonttbl {\ f0 \ fnil \ fcharset0 Microsoft Sans Serif;}} \ viewkind4 \ uc1 \ pard \ qc \ f0 \ fs17 test paragraphe \ par \ pard paragraphe de test \ par }
espérons que cette aide, ce qui est