Link to home
Start Free TrialLog in
Avatar of kirezz
kirezz

asked on

How to disable popup menu on TWebBrowser component



1. Does anyone know how to disable popup menu on TWebBrowser component.
2. If its possible then how to add my own popup menu?

Question 1 is more important for me.

thanks in advance.
Avatar of inthe
inthe

Hi,
here a is sample to do both:

override popup menu and have own menu with copy and save

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,OleCtnrs, StdCtrls, OleCtrls, SHDocVw_TLB, Menus,
   OleConst,  ActiveX;

type
  TForm1 = class(TForm)
    Button1: TButton;
    PopupMenu1: TPopupMenu;
    OpenDialog1: TOpenDialog;
    Save1: TMenuItem;      {popupmenu savepage item}
    Copy1: TMenuItem;      
     SaveDialog1: TSaveDialog;
    WebBrowser1: TWebBrowser;
   procedure MessageLoop(var Msg:TMsg;var Handled:Boolean);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Save1Click(Sender: TObject);
     procedure Copy1Click(Sender: TObject);  
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.MessageLoop(var Msg: TMsg; var Handled: Boolean);
var
 mouse:Tpoint;
begin
if msg.hwnd = webbrowser1.Handle then
  begin
   if (Msg.Message = WM_RBUTTONDOWN) or (Msg.Message = WM_RBUTTONUP)
   then begin
    handled:=True;
     getcursorpos(Mouse);
    popupmenu1.popup(Mouse.x,Mouse.y);
   end
  else Handled := False; //not rbutton
 end
else
Handled := false; //not webbrowser msg
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
opendialog1.execute;
webbrowser1.oleobject.navigate(opendialog1.filename);

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Application.OnMessage := MessageLoop;
end;

procedure TForm1.Save1Click(Sender: TObject);
var
persist :ipersistfile;
begin
persist := (webbrowser1.document as ipersistfile);
persist.save('c:\a.html',false);
showmessage('page saved as c:\a.html');
end;

procedure TForm1.Copy1Click(Sender: TObject);
  var
    vInput, vOutput: OleVariant;
  begin
    WebBrowser1.ExecWB(OLECMDID_COPY, 0, vInput, vOutput);
  end;

end.
Avatar of kirezz

ASKER

Hi inthe,

i've checked your code but seems it doesn;t work properly.

Only diference from posted code is only that i dont have "SHDocVw_TLB.dcu" and instead of that i've put "SHDocVw" in uses clause. I don;t know if this is cause of problems.

list of problems i got:

1. when i initialy start prg i get empty webbrowser object and i can see my custom poput menu, but after i load other html page with button1click default menus are shown.

2. when twebbrowser is empty, if i choose "open" from my popup i got following error:
"trying to revoke a drop target that has not been registered"

3.when i try to click on "save" menu item i got some exception ("access violation at address...").

Do you know how to help me please?

thank you.
ASKER CERTIFIED SOLUTION
Avatar of inthe
inthe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial