我使用TWebBrowser
在我的应用程序中安装HTML编辑器,当然这取决于安装的Internet Explorer版本。在Windows 7上安装全新的Internet Explorer 11后,我注意到我的编辑器发生了变化。段落似乎不再具有相同的 HTML 代码。
之前按回车键时生成的 HTML:
<P> </P>
现在生成的 HTML:
<P><BR></P>
这在我的编辑器中给了我额外的行,看起来不正确。 <P>
本身有一条新线,<BR>
在这里完全没用。
有没有办法告诉编辑模式下的MSHTML/TWebBrowser控件在按下回车键时使用哪个标记?例如,我看到一些 MS 程序生成:
<div><font></font></div>
当您按回车键进入新行时。
另外(如果它是相关的) - 当我使用命令设置例如字体大小时,有没有办法控制将使用哪个标记(而不是过时的 size=1 到 size=7 以获得像"font-size:10px"这样的 CSS)
欢迎使用 Delphi 和 C++ Builder 中的代码示例。
使用 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> </P>"); // You can put every html you like per every key code
event.returnValue = false; // blocks default html which will be generated
}
}
你可以从这里下载这个伟大的包装器(bcbhtml)。