本文介绍了Windows 7上的IHTMLDocument2和Internet Explorer 11更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use TWebBrowser to have HTML editor in my application and of course it depends on version of Internet Explorer installed. I noticed after installation of the brand new Internet Explorer 11 on Windows 7 that my editor has changed. Paragraphs no longer seem to have same HTML code.

在我按Enter键之前生成的HTML:

HTML generated before when I pressed enter key:

<P>&nbsp;</P>

现在生成的HTML:

<P><BR></P>

这在编辑器中给了我额外的一行,看起来不太正确.< P> 本身换行了,< BR> 在这里完全没用.

This gives me additional line in my editor which doesn't look right. <P> itself has a new line, <BR> is completely useless here.

有没有一种方法可以告诉MSHTML/TWebBrowser控件在编辑模式下按下Enter键时要使用哪个标记?例如,我已经看到一些MS程序会生成:

Is there a way to tell MSHTML/TWebBrowser control in edit mode which markup to use when enter key is pressed? For example, I've seen that some MS programs generate:

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

按Enter进入新行.

When you press enter to get to new line.

(如果有关系的话)-当我使用命令设置字体大小时,是否有一种方法可以控制使用哪个标记(而不是将过时的size = 1改为size = 7以使CSS像"font-size:10px)

Also (if it is related) - is there a way to control which markup will be used when I use commands to set for example font-size (instead of obsolete size=1 to size=7 to have maybe CSS like "font-size:10px")

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

Code samples in Delphi and C++ Builder welcome.

推荐答案

使用 bcbhtml :首先将html.cpp添加到您的项目中,并添加"html.h":

Using bcbhtml :First add html.cpp to your project and include "html.h":

#include "html.h"

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

define document variable in global scope:

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

您可以从此处下载此出色的包装(bcbhtml).

You can download this great wrapper (bcbhtml) from here.

这篇关于Windows 7上的IHTMLDocument2和Internet Explorer 11更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 23:14
查看更多