EmbeddedWB inserting HTML at cursor position

881 views Asked by At

I use TEmbeddedWB and TEditDesigner to add HTML to page which is in edit mode. It is as simple as EditDesigner->InsertHTML("1234");.

The problem is because the HTML is not always inserted at the cursor position. If I place this code on a button it works just fine, HTML is inserted at cursor position. But if I place it in CMDialogKey event which intercepts CM_DIALOGKEY message to intercept TAB key it always inserts it at the beginning of the page. If text is selected, then it inserts at selected text position, overwriting it (as it should). But if it is not selected it inserts it at the beginning of HTML.

Here is a function which inserts HTML in TEmbeddedWB:

procedure TEditDesigner.InsertHTML(HTML: string);
var
  Sel: IHTMLSelectionObject;
  Range: IHTMLTxtRange;
  Doc: IHTMLDocument2;
begin
  if FEnable and Assigned(FEmbeddedWB) and (not (csDesigning in ComponentState)) then
  begin
    Doc := FEmbeddedWB.Doc2;
    if Assigned(Doc) then
    begin
      Sel := Doc.selection;
      if Assigned(Sel) then
      begin
        if (Sel.type_ = 'None') or (Sel.type_ = 'Text') then
        begin
          Range := Sel.createRange as IHTMLTxtRange;
          Range.pasteHTML(HTML);
        end;
      end;
    end;
  end;
end;

How can I use it to insert HTML at the cursor position all the time?

0

There are 0 answers