Delphi TWebBrowser作为HTML编辑器 - 获取字体属性

时间:2015-12-21 15:36:04

标签: html delphi delphi-7 twebbrowser

我在Delphi7中使用TWebBrowser作为HTML编辑器,在OnDocumentComplete中将它的designMode设置为'on'。

我知道如何更改粗体,斜体,字体,颜色,对齐等字体属性。我正在使用带参数的exeCommand

var
  htmlDoc: HTMLDocument;
  parameter: OleVariant;
begin
  (wbEditor.Document as IHTMLDocument2).ParentWindow.Focus;
  htmlDoc := wbEditor.document as HTMLDocument;
  htmlDoc.execCommand('bold', false, parameter);
  (wbEditor.Document as IHTMLDocument2).ParentWindow.Focus;
end;

问题是当我在文本中更改光标位置时如何读取'粗体'和其他属性。

让我的文字就像' foo 吧'。当我将光标定位在FOO时,我想检查一个'粗体按钮',但是当我将它定位在BAR时,我不想检查。

???

1 个答案:

答案 0 :(得分:2)

Hej我自己找到了一个解决方法,使用了TEmbeddedWB而不是TWebBrowser,并在下面的代码中使用了OnClock和OnKeyDown事件

var
  doc: IHTMLDocument2;
  sel: IHTMLSelectionObject;
  range: IHTMLTxtRange;
begin
  doc := wb1.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;

        Caption := Range.queryCommandValue('justifyCenter');
      end;
    end;
  end;
end;

谢谢你!