如何禁用 TWebBrowser 上下文菜单?

时间:2021-07-20 08:51:25

标签: delphi delphi-xe7 twebbrowser

我有一个框架,其中包含一个 TWebBrowser 组件并被我的一些应用程序使用,我需要禁用 TWebBrowser 的默认弹出菜单。

Picture of the default popup menu

我找到了一个适用于应用程序级别的解决方案,通过这种方式使用 TApplicationEvents 组件及其 OnMessage 事件处理程序:

procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
  var Handled: Boolean);
begin
  if (Msg.Message = WM_RBUTTONDOWN) or (Msg.Message = WM_RBUTTONDBLCLK) then
  begin
    if IsChild(WebBrowser1.Handle, Msg.hwnd) then
    begin
      Handled := True;
    end;
  end;
end;

我正在寻找一种在框架/TWebBrowser 级别工作的解决方案,而无需在应用程序级别添加代码。

我已尝试分配 TWebBrowserTPopupMenu 属性,但它仅在 WebBrowser 上加载页面之前有效。

我已尝试分配 TWebBrowserWindowProc,但在 WebBrowser 中加载页面后,不再执行代码。

  private
    FPrevBrowWindowProc : TWndMethod;
    procedure BrowWindowProc(var AMessage: TMessage);

...

procedure TFrame1.BrowWindowProc(var AMessage: TMessage);
begin
  if(AMessage.Msg = WM_RBUTTONDOWN) or (AMessage.Msg = WM_RBUTTONDBLCLK) then 
    Exit;

  if(Assigned(FPrevBrowWindowProc))
  then FPrevBrowWindowProc(AMessage);
end;

constructor TFrame1.Create(AOwner : TComponent);
begin
  inherited;

  FPrevBrowWindowProc := WebBrowser1.WindowProc;
  VS_Brow.WindowProc := BrowWindowProc;
end;

0 个答案:

没有答案