【问题标题】:what's the proper way to retrieve text from a Rich Text Control using the Windows API?使用 Windows API 从富文本控件中检索文本的正确方法是什么?
【发布时间】:2012-11-23 14:01:32
【问题描述】:

下面的 GetRTF() 方法可以工作,但它只检索元数据:

    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;

所以当我用富文本控件的句柄调用GetRTF()时,返回值为:

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

但这不是富文本控件显示的文本(它只是一个电子邮件地址)。

检索我要查找的数据的正确方法是什么?

【问题讨论】:

    标签: c# .net winapi pinvoke sendmessage


    【解决方案1】:

    您的代码已经检索到所有数据。那是控件内容的 RTF 表示。没有文本,因为您的控件中没有任何文本。它似乎只包含一个元文件矢量图像。

    如果您向该控件发送WM_GETTEXT 消息以获取纯文本,那么您将一无所获。因为控件不包含文本,只有图像。

    【讨论】:

    • 我使用 Spy++ 识别了控件,并且那里有一个电子邮件地址,用户可以像任何其他文本一样选择该地址。更具体地说,这是 Outlook 2007 阅读窗格中“发件人:”标签旁边的电子邮件地址。(我知道 Outlook API 可用于检索该数据,但在此方面无济于事案例,因为我正在使用多个窗口,其想法是根据发件人的电子邮件地址识别每个窗口。)
    • 当我发送 WM_GETTEXT 消息时,我确实得到了一些东西,一个空格。那么您是说该电子邮件地址实际上只是一系列图像,从用户的角度来看,它们看起来像文本元素?您对我如何检索这些数据有任何想法吗?我很确定一定有办法。
    • 您发送WM_GETTEXT 并返回一个空格。这就是我所说的。您的 RTF 包含以下内容:{\pict\wmetafile0} 这是元文件图像。
    • 它还包含这个:{\fonttbl{\f0\fnil\fprq2\fcharset0 Tahoma;}},看起来像是与文本相关的信息。
    • 这是你的空格字符的字体!!
    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多