【问题标题】:IHTMLDocument2 and Internet Explorer 11 changes on Windows 7Windows 7 上的 IHTMLDocument2 和 Internet Explorer 11 更改
【发布时间】:2026-01-30 07:30:01
【问题描述】:

我使用TWebBrowser 在我的应用程序中安装 HTML 编辑器,当然这取决于安装的 Internet Explorer 版本。在 Windows 7 上安装全新的 Internet Explorer 11 后,我注意到我的编辑器发生了变化。段落似乎不再具有相同的 HTML 代码。

在我按下回车键之前生成的 HTML:

<P>&nbsp;</P>

现在生成的 HTML:

<P><BR></P>

这在我的编辑器中给了我额外的行,看起来不正确。 &lt;P&gt; 本身有新的一行,&lt;BR&gt; 在这里完全没用。

有没有办法在编辑模式下告诉 MSHTML/TWebBrowser 控件在按下回车键时使用哪个标记?例如,我看到一些 MS 程序生成:

<div><font></font></div>

当你按下回车键进入新行时。

另外(如果它是相关的) - 有没有办法控制当我使用命令设置例如字体大小时将使用哪个标记(而不是过时的 size=1 到 size=7 可能有 CSS 像“字体大小:10px")

欢迎使用 Delphi 和 C++ Builder 中的代码示例。

【问题讨论】:

    标签: delphi webbrowser-control c++builder twebbrowser


    【解决方案1】:

    使用 bcbhtml : 首先将 html.cpp 添加到您的项目中并包含“html.h”:

    #include "html.h"
    

    在全局范围内定义文档变量:

    THTMLDocument document;
    
    __fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
    {
        WebBrowser1->Navigate("about:<div contenteditable=true>Type here</div>"); // example editable region
    }
    
    void __fastcall TForm1::WebBrowser1DocumentComplete(TObject *ASender, const IDispatch *pDisp,
              const OleVariant &URL)
    {
        document.documentFromVariant(WebBrowser1->Document);
        document.onkeydown = &onkeydown;
    }
    
    void TForm1::onkeydown()
    {
        EventObj event = document.parentWindow.event;
        if(event.keyCode == VK_RETURN)
        {
            document.selection.createRange().pasteHTML("<P>&nbsp;</P>"); // You can put every html you like per every key code
            event.returnValue = false; // blocks default html which will be generated
        }
    }
    

    您可以从here 下载这个很棒的包装器 (bcbhtml)。

    【讨论】:

      最近更新 更多