2

The GetRTF() method below sort of works, but it retrieves only metadata:

    public string GetRTF(IntPtr handle)
    {
        string result = String.Empty;
        using (System.IO.MemoryStream stream = new MemoryStream())
        {
            EDITSTREAM editStream = new EDITSTREAM();
            editStream.pfnCallback = new EditStreamCallback(EditStreamProc);
            editStream.dwCookie = stream;

            SendMessage(handle, EM_STREAMOUT, SF_RTF, ref 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 struct EDITSTREAM
    {
        public MemoryStream dwCookie;
        public int dwError;
        public EditStreamCallback pfnCallback;
    }

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

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

So when I call GetRTF() with the handle of the Rich Text Control, the return value is:

     {\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fprq2\fcharset0 Tahoma;}}{\colortbl ;\red59\green59\blue59;}{\*\generator Riched20 14.0.6015.1000;}{\*\mmathPr\mwrapIndent1440}\viewkind4\uc1\pard\cf1\f0\fs17{\pict\wmetafile0}}

But that's not the text displayed by the Rich Text Control (it's just an e-mail address).

What's the proper way to retrieve the data I'm looking for?

John Smith
  • 4,416
  • 7
  • 41
  • 56

1 Answers1

1

Your code already retrieves all the data. That is the RTF representation of the contents of the control. There's no text because your control doesn't have any text in it. It appears to contain only a metafile vector image.

If you sent that control a WM_GETTEXT message to obtain plain text, then you'd get back nothing. Because the control doesn't contain text, only an image.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I identified the control using Spy++ and there is an e-mail address there, which can be selected by the user like any other text. To be more specific, that's the e-mail address next to the "From:" label in the Reading Pane of Outlook 2007. (I know that the Outlook API can be used to retrieve that data, but it won't help in this case because I'm working with multiple windows and the idea is to identify each window based on the sender's e-mail address.) – John Smith Nov 23 '12 at 19:24
  • When I send the WM_GETTEXT message, I do get something, a space. So are you saying that that e-mail address is really just a sequence of images which from the user's point of view look like text elements? Do you have any idea as to how I might go about retrieving that data? I'm pretty sure there has to be a way. – John Smith Nov 23 '12 at 19:25
  • You send `WM_GETTEXT` and get back just a space. Which is what I said. Your RTF contains this: `{\pict\wmetafile0}` which is the metafile image. – David Heffernan Nov 23 '12 at 19:26
  • It also contains this: {\fonttbl{\f0\fnil\fprq2\fcharset0 Tahoma;}}, which looks like information pertinent to text. – John Smith Nov 23 '12 at 19:32
  • That's the font used for your space character!! – David Heffernan Nov 23 '12 at 19:36
  • well, I made an experiment, using a different Rich Text Control. This is what I retrieved: {\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fprq2\fcharset0 Segoe UI;}} {\colortbl ;\red59\green59\blue59;} {\*\generator Riched20 14.0.6015.1000;}{\*\mmathPr\mwrapIndent1440}\viewkind4\uc1 \pard\cf1\f0\fs17 Mon 5/14/2012 1:24 AM}. And yes, that Rich Text Control is supposed to display the date and time when the e-mail was received. What do you suggest I do? As you said, the text is actually an image and the text that image represents cannot be easily read. – John Smith Nov 23 '12 at 23:33
  • I have noticed however, that if I drag & drop the image/text onto another rich text control, the text is copied fine. The text is also copied fine when I right click on the image/text and select copy. I would hate to use the clipboard for this kind of task, but if that's the only way to go I don't mind. Any idea as to how I could accomplish this? – John Smith Nov 23 '12 at 23:35
  • I don't know. I don't know the details of your scenario. I don't have Outlook. I just tried to answer the question you asked. – David Heffernan Nov 23 '12 at 23:39