Ciel
Ciel

Reputation: 17752

pinvoke, sendmessagebystring, retrieving from rich text

I have a situation where I must use Windows API to retrieve text from a Rich Text Box in another program; I am wondering if there is any way to get the ...'rich text' from it, and not just the plain text.

In this example, ptrHandle is the RichText Control Handle.

if (ptrHandle == null)
    return null;

if (ptrHandle == IntPtr.Zero)
    return null;

IntPtr ptrLength =
    SendMessage(ptrHandle, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);

var nLen = ptrLength.ToInt32();

if (nLen <= 0)
    return null;
var strBuffer = new System.Text.StringBuilder(nLen + 1);

SendMessage(ptrHandle, WM_GETTEXT, new IntPtr(nLen + 1), strBuffer);

This is all done in C#. It gets the text out just fine, but it's stripped of formatting, and such. I was hoping I could retrieve all of that as well.

Upvotes: 1

Views: 372

Answers (1)

Hans Passant
Hans Passant

Reputation: 941545

Good news: EM_STREAMOUT helps you retrieve the RTF, the kind that has the formatting. Bad news: you cannot make that work without injecting a DLL into the process since it requires a callback. You can't make that work in C#, native C/C++ is required. I know, not helpful.

Upvotes: 1

Related Questions