Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top