关于无窗口富编辑控件

无窗口 Rich Edit 控件也称为文本服务对象,是一种提供 Rich Edit 控件功能而不提供窗口的对象。 为了提供窗口的功能,例如接收消息的能力和可以在其中绘制的设备上下文,无窗口 Rich Edit 控件使用一对接口:ITextServicesITextHost

若要创建无窗口 Rich Edit 控件,请使用指向 ITextHost 接口实现的指针调用 CreateTextServices 函数。 CreateTextServices 返回一个 IUnknown 指针,你可以查询该指针以检索指向无窗口控件的 ITextServices 实现的指针。

Msftedit.dll 导出名为 IID_ITextServices 的接口标识符 (IID),该标识符可用于查询 ITextServices 接口的 IUnknown 指针。 以下示例演示如何检索 IID_ITextServices 并使用它来获取 ITextServices 接口。

    .
    .
    .
    HRESULT hr;
    IUnknown* pUnk = NULL;
    ITextServices* pTextServices =  NULL;
    
    // Create an instance of the application-defined object that implements the 
    // ITextHost interface.
    TextHost* pTextHost = new TextHost();
    if (pTextHost == NULL) 
        goto errorHandler;

    // Create an instance of the text services object.
    hr = CreateTextServices(NULL, pTextHost, &pUnk);
    if (FAILED(hr))
        goto errorHandler;
        
    // Retrieve the IID_ITextServices interface identifier from 
    // Msftedit.dll. The hmodRichEdit parameter is a handle to the 
    // Msftedit.dll module retrieved by a previous call to the 
    // GetModuleHandle function.
    IID* pIID_ITS = (IID*) (VOID*) GetProcAddress(hmodRichEdit, 
        "IID_ITextServices");
               
    // Retrieve the ITextServices interface.    
    hr = pUnk->QueryInterface(*pIID_ITS, (void **)&pTextServices);
    if (FAILED(hr))
        goto errorHandler;
     .
     . 
     .   
     

Msftedit.dll 还导出一个名为 IID_ITextHost 的接口标识符 (IID),该标识符可用于以类似的方式查询ITextHost 接口。

ITextHost 接口具有无窗口控件调用的方法,用于检索有关窗口的信息。 例如,文本服务对象调用 TxGetDC 方法以检索可绘制到的设备上下文。 无窗口控件调用 TxNotify 方法将通知(如富编辑通知消息)发送到文本主机。 文本服务对象调用其他 ITextHost方法,以请求文本主机执行其他与窗口相关的服务。 例如,TxInvalidateRect 方法请求文本主机向窗口的更新区域添加矩形。

标准的 Rich Edit 控件有一个窗口过程,用于处理系统消息和来自应用程序的消息。 可以使用控件的窗口句柄发送消息来执行文本编辑和其他操作。 但是,无窗口 Rich Edit 控件没有用于接收和处理消息的窗口过程。 而是提供 ITextServices 接口。 若要将消息发送到无窗口 Rich Edit 控件,请调用 TxSendMessage 方法。 可以使用此方法发送任何富编辑消息,或传递影响控件的其他消息,例如鼠标或键盘输入的系统消息。

还可以创建文本服务对象作为 COM 聚合对象的一部分。 这样可以轻松地使用无窗口组件对象模型 (COM) 对象聚合文本服务对象。