首页 > 解决方案 > 检测可编辑 TWebBrowser 中的更改

问题描述

我正在加载一个 HTML 本地文件TWebBrowser,如下所示:

procedure TForm1.FormCreate(Sender: TObject);
begin
  WebBrowser1.Navigate('file:///C:\Tmp\input.html');
end;

TWebBrowser.OnDocumentComplete事件处理程序中,我使其可编辑:

procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject;
  const pDisp: IDispatch; const URL: OleVariant);
begin
  (WebBrowser1.Document as IHTMLDocument2).designMode := 'on';
end;

一旦用户通过TWebBrowser(即:他写了一些东西......)应用了任何更改,我需要立即得到通知,但我看不到任何OnChanged或类似的事件处理程序。


我尝试过捕获WM_PASTEWM_KEYDOWN但我的代码从未执行过:

  TMyWebBrowser = class(TWebBrowser)
  public
    procedure WM_Paste(var Message: TWMPaste); message WM_PASTE;
    procedure WM_KeyDown(var Message: TWMKeyDown); message WM_KEYDOWN;
  end;

...

procedure TMyWebBrowser.WM_Paste(var Message: TWMPaste);
begin
  inherited;
  ShowMessage('Paste');
end;

procedure TMyWebBrowser.WM_KEYDOWN(var Message: TWMKeyDown);
begin
  inherited;
  ShowMessage('KeyDown');
end;

我也尝试过设置WindowProc属性,但没有任何成功。

标签: delphidelphi-xe7twebbrowser

解决方案


要在设计模式下捕获对文档的更改,您应该使用其IMarkupContainer2接口来注册IHTMLChangeSinkviaRegisterForDirtyRange方法。这个过程非常简单——实现IHTMLChangeSink、获取IMarkupContainer2WebBrowser1.Document调用它的RegisterForDirtyRange方法,但有一个问题。

当您更改designModeof 时IHTMLDocument2TWebBrowser控件会重新加载当前文档,并且它会丢失所有已注册的更改接收器。因此,您应该在将文档置于设计模式后进行注册。之后,您会通过IHTMLChangeSink.Notify方法收到更改通知。

但还有另一个问题。由于进入设计模式会导致重新加载文档,进而导致将readyState文档的属性更改为'loading',然后连续更改为'complete'。您的更改接收器将收到这些readyState更改通知。注意进入设计模式后TWebBrowser.OnDocumentComplete 不调用。这就是为什么您应该忽略任何通知,直到文档在设计模式下完全重新加载。

另一个小麻烦是RegisterForDirtyRange创建了一个 cookie,您需要维护该 cookie 才能取消注册更改接收器。由于无论如何您都需要一个类来实现IHTMLChangeSink,它还可以封装设计模式状态并更改注册。

uses
  System.SysUtils, SHDocVw, MSHTML;

const
  DesignMode: array[Boolean] of string = ('off', 'on');

type
  TWebBrowserDesign = class(TInterfacedObject, IHTMLChangeSink)
  private
    FDirtyRangeCookie: LongWord;
    FDocumentComplete: Boolean;
    FHTMLDocument2: IHTMLDocument2;
    FMarkupContainer2: IMarkupContainer2;
    FOnChange: TProc;
    { IHTMLChangeSink }
    function Notify: HResult; stdcall;
  public
    constructor Create(WebBrowser: TWebBrowser; const AOnChange: TProc);
    destructor Destroy; override;
  end;

constructor TWebBrowserDesign.Create(WebBrowser: TWebBrowser; const AOnChange: TProc);
begin
  inherited Create;
  if not Assigned(WebBrowser) then
    raise Exception.Create('Web browser control missing.');
  if not Supports(WebBrowser.Document, IHTMLDocument2, FHTMLDocument2) then
    raise Exception.Create('No HTML document loaded.');
  FHTMLDocument2.designMode := DesignMode[True];
  if Supports(WebBrowser.Document, IMarkupContainer2, FMarkupContainer2) then
  begin
    if FMarkupContainer2.RegisterForDirtyRange(Self, FDirtyRangeCookie) <> S_OK then
      FDirtyRangeCookie := 0
    else
      _Release;
  end;
  FOnChange := AOnChange;
end;

destructor TWebBrowserDesign.Destroy;
begin
  if Assigned(FMarkupContainer2) and (FDirtyRangeCookie <> 0) then
    FMarkupContainer2.UnRegisterForDirtyRange(FDirtyRangeCookie);
  if Assigned(FHTMLDocument2) then
    FHTMLDocument2.designMode := DesignMode[False];
  inherited;
end;

function TWebBrowserDesign.Notify: HResult;
begin
  Result := S_OK;
  if not FDocumentComplete then
    FDocumentComplete := FHTMLDocument2.readyState = 'complete'
  else if Assigned(FOnChange) then
    FOnChange();
end;

_Release请注意注册更改接收器后的调用。这是为了“防止”标记容器持有对TWebBrowserDesign实例的强引用。这允许您使用TWebBrowserDesign实例的生命周期来控制设计模式:

type
  TForm1 = class(TForm)
    { ... }
  private
    FWebBrowserDesign: IInterface;
    { ... }
  end;

procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject;
  const pDisp: IDispatch; const URL: OleVariant);
begin
  { enter design mode }
  FWebBrowserDesign := TWebBrowserDesign.Create(WebBrowser1, procedure
    begin
      ButtonSave.Enabled := True;
    end);
end;

procedure TForm1.ButtonSave(Sender: TObject);
begin
  { exit design mode }
  FWebBrowserDesign := nil;
  ButtonSave.Enabled := False;
end;

或者,您可以将更改接收器实现为组件。

type
  TWebBrowserDesign = class(TComponent, IHTMLChangeSink)
  private
    FDirtyRangeCookie: LongWord;
    FDocumentComplete: Boolean;
    FHTMLDocument2: IHTMLDocument2;
    FMarkupContainer2: IMarkupContainer2;
    FOnChange: TNotifyEvent;
    FWebBrowser: TWebBrowser;
    procedure EnterDesignMode;
    procedure ExitDesignMode;
    function GetActive: Boolean;
    procedure SetActive(const Value: Boolean);
    procedure SetWebBrowser(const Value: TWebBrowser);
    { IHTMLChangeSink }
    function Notify: HResult; stdcall;
  protected
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  public
    destructor Destroy; override;
  published
    property Active: Boolean read GetActive write SetActive;
    property OnChange: TNotifyEvent read FOnChange write FOnChange;
    property WebBrowser: TWebBrowser read FWebBrowser write SetWebBrowser;
  end;

destructor TWebBrowserDesign.Destroy;
begin
  ExitDesignMode;
  inherited;
end;

procedure TWebBrowserDesign.EnterDesignMode;
begin
  if not Assigned(FWebBrowser) then
    raise Exception.Create('Web browser control missing.');
  if not Supports(FWebBrowser.Document, IHTMLDocument2, FHTMLDocument2) then
    raise Exception.Create('No HTML document loaded.');
  try
    FHTMLDocument2.designMode := DesignMode[True];
    if Supports(FWebBrowser.Document, IMarkupContainer2, FMarkupContainer2) then
    begin
      if FMarkupContainer2.RegisterForDirtyRange(Self, FDirtyRangeCookie) <> S_OK then
        FDirtyRangeCookie := 0;
    end;
  except
    ExitDesignMode;
    raise;
  end;
end;

procedure TWebBrowserDesign.ExitDesignMode;
begin
  if Assigned(FMarkupContainer2) then
  begin
    if FDirtyRangeCookie <> 0 then
    begin
      FMarkupContainer2.UnRegisterForDirtyRange(FDirtyRangeCookie);
      FDirtyRangeCookie := 0;
    end;
    FMarkupContainer2 := nil;
  end;
  if Assigned(FHTMLDocument2) then
  begin
    FHTMLDocument2.designMode := DesignMode[False];
    if not (csDestroying in ComponentState) then
      FHTMLDocument2 := nil; { causes AV when its hosting TWebBrowser component is destroying; I didn't dig into details }
  end;
  FDocumentComplete := False;
end;

function TWebBrowserDesign.GetActive: Boolean;
begin
  Result := Assigned(FHTMLDocument2);
end;

procedure TWebBrowserDesign.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited;
  if (Operation = opRemove) and (AComponent = FWebBrowser) then
    WebBrowser := nil;
end;

function TWebBrowserDesign.Notify: HResult;
begin
  Result := S_OK;
  if not FDocumentComplete then
    FDocumentComplete := FHTMLDocument2.readyState = 'complete'
  else if Assigned(FOnChange) then
    FOnChange(Self);
end;

procedure TWebBrowserDesign.SetActive(const Value: Boolean);
begin
  if Active <> Value then
  begin
    if Value then
      EnterDesignMode
    else
      ExitDesignMode;
  end;
end;

procedure TWebBrowserDesign.SetWebBrowser(const Value: TWebBrowser);
begin
  if Assigned(FWebBrowser) then
  begin
    ExitDesignMode;
    FWebBrowser.RemoveFreeNotification(Self);
  end;
  FWebBrowser := Value;
  if Assigned(FWebBrowser) then
    FWebBrowser.FreeNotification(Self);
end;

如果您将这样的组件放在设计时包中并在 IDE 中注册它,那么您将能够链接该组件TWebBrowser并在表单设计器中分配OnChange事件处理程序。使用Active代码中的属性进入/退出设计模式。

type
  TForm1 = class(TForm)
    { ... }
    WebBrowserDesign1: TWebBrowserDesign;
    { ... }
  end;

procedure WebBrowserDesign1Change(Sender: TObject);
begin
  ButtonSave.Enabled := True;
end;

procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject;
  const pDisp: IDispatch; const URL: OleVariant);
begin
  { enter design mode }
  WebBrowserDesign1.Active := True;
end;

procedure TForm1.ButtonSave(Sender: TObject);
begin
  { exit design mode }
  WebBrowserDesign1.Active := False;
  ButtonSave.Enabled := False;
end;

注意:关于 C#/WinForms 已经提出了类似的问题 -如何检测 WebBrowser 控件的内容何时更改(在设计模式下)?

最后一点:我不相信在更改后启用保存按钮是最好的 UX 设计。如果您认为上面的代码值得实现您的目标,请继续。这只是一个概念证明,代码尚未经过彻底测试。需要您自担风险使用它。


推荐阅读