IHTMLDocument2 and Internet Explorer 11 changes on Windows 7

纵然是瞬间 提交于 2021-01-28 00:15:57

问题


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.

HTML generated before when I pressed enter key:

<P>&nbsp;</P>

HTML generated now:

<P><BR></P>

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

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>

When you press enter to get to new line.

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")

Code samples in Delphi and C++ Builder welcome.


回答1:


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

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



来源:https://stackoverflow.com/questions/19928458/ihtmldocument2-and-internet-explorer-11-changes-on-windows-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!