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

Delphi TWebBrowser as HTML Editor - get font properties

我在 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;

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

假设我的文本类似于“foo bar”。我想在将光标置于 FOO 时检查 'Bold button',但在将其置于 BAR 时取消选中。

???

嘿,我自己找到了一个解决方法,使用 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;

谢谢自己!!