OnShowContextMenu

 

Disable the Context Menu

Use property OnShowContextMenu to manipulate the context menu, that appears when you right-click the mouse.

To disable the context menu you only need to add one line of code:

Function TForm1.EmbeddedWB1ShowContextMenu(const dwID: Cardinal;
const ppt: PPoint; const pcmdtReserved: IUnknown;
const pdispReserved: IDispatch): HRESULT;
begin
    result := S_OK;
end;

This function is called every time the webbrowser needs to display the contextmenu, so you can disable and enable it as you like. To enable the context menu you just set result to S_FALSE;

In the following example a checkbox is used to disable/enable the contextmenu:

var
ContextMenu: Integer;

function TForm1.EmbeddedWB1ShowContextMenu(const dwID: Cardinal;
const ppt: PPoint; const pcmdtReserved: IUnknown;
const pdispReserved: IDispatch): HRESULT;
begin
  result := ContextMenu;
end;

procedure TForm1.CheckBox1Click(Sender: TObject);
begin
  if Checkbox1.checked then 
        Contextmenu := S_FALSE 
  else 
     ContextMenu := S_OK;
end;

 

Replace the context menu

ppt gives you the coordinates for the cursor-position when you right-click the mouse. You can use this when you want to replace the default context menu with your own popup-menu.


function TForm1.EmbeddedWB1ShowContextMenu(const dwID: Cardinal;
const ppt: PPoint; const pcmdtReserved: IUnknown;
const pdispReserved: IDispatch): HRESULT;
begin
  PopupMenu1.Popup(ppt.x, ppt.y); //show our own contextmenu
  result := S_OK;    // Don't show default menu
end;end;

 

Determining what context menu is about to be displayed

dwID holds information about the type of context menu your webbrowser wants to display. According to Microsoft these values may change in the future so be careful:

 

 
CONTEXT_MENU_DEFAULT = 0;
CONTEXT_MENU_IMAGE = 1;
CONTEXT_MENU_CONTROL = 2;
CONTEXT_MENU_TABLE = 3;
CONTEXT_MENU_TEXTSELECT = 4;
CONTEXT_MENU_ANCHOR = 5;
CONTEXT_MENU_UNKNOWN = 6;
CONTEXT_MENU_IMGDYNSRC = 7;
CONTEXT_MENU_IMGART = 8;
CONTEXT_MENU_DEBUG = 9;

In the following snippet we use this information to allow the context menu to be displayed only if mouse is over an image: 


function TForm1.EmbeddedWB1ShowContextMenu(const dwID: Cardinal;
const ppt: PPoint; const pcmdtReserved: IUnknown;
const pdispReserved: IDispatch): HRESULT;
begin
  If dwID=1                 //mouse is over an image
 
then Result:=S_FALSE      //show default menu
 
else Result:=S_OK;        //don't show contextmenu
end;

 

Adding entries to the standard context menu

You can also add items to the standard context menu instead of just removing or replacing it. To do this you need to write to the registry, but in EmbeddedWb you can do this in a very safe way. In the following example the menu-extension is added to the registry when the context-menu is activated and removed again when the context menu is no longer visible.

 



var
CtxMenuAdded: Boolean;

procedure AddExtMenuItem(MenuText, Url: string; Context, Flags: DWORD);
var
reg: TRegistry;
begin
  if not CtxMenuAdded then begin
 
Reg := TRegistry.Create;
  with Reg do begin
   
RootKey := HKEY_CURRENT_USER;
    OpenKey('\Software\Microsoft\Internet Explorer\MenuExt\' + MenuText, True);
    WriteString('', Url);
    WriteBinaryData('Context', Context, SizeOf(Context));
    WriteBinaryData('Flags', Flags, SizeOf(Flags));
    CloseKey;
    Free;
    end;
 
CtxMenuAdded := True;
  end;
end;

function RemoveExtMenuItem(MenuText: string): Boolean;
var
reg: TRegistry;
begin
  if
CtxMenuAdded then begin
    Reg := TRegistry.Create;
    with Reg do begin
   
RootKey := HKEY_CURRENT_USER;
    Result := DeleteKey('\Software\Microsoft\Internet Explorer\MenuExt\' + MenuText);
    Free;
    end;
  CtxMenuAdded := False;
  end;
end;


function TForm1.EmbeddedWB1ShowContextMenu(const dwID: Cardinal;
const ppt: PPoint; const pcmdtReserved: IUnknown;
const pdispReserved: IDispatch): HRESULT;
begin
  AddExtmenuItem('&Highlight', 'highlight.htm', 11, 0);
end;


function TForm1.EmbeddedWB1EnableModeless(const fEnable: LongBool): HRESULT;
begin
if fEnable and CtxMenuAdded then  RemoveExtMenuItem('&Highlight');
end;

 

Procedure AddExtMenu   adds entry to the contextmenu. Menutext is the text to appear in the menu. Url is the location and name of the script-file to get called when the menu-item is clicked. In Context  you select which of the default menus your entry should appear in: 

Context Value
Default 1
Images 2
Controls 4
Tables 8
Text selection 10
Anchor 11
Unknown 12

In our example context is 11 because we want the entry added to the default menu (1) and to the text-selection menu (10). 

We want to call a script-file that highlights selected text. It looks like this:

<HTML>
<SCRIPT LANGUAGE="JavaScript" defer> 
var parentwin = external.menuArguments;
var doc = parentwin.document;
var sel = doc.selection;
var rng = sel.createRange();
var str = new String(rng.text); 
rng.execCommand("BackColor",0,"YELLOW");

</SCRIPT>
</HTML>

OnEnableModeless is always called with parameter fEnable=TRUE when context menu disappears, so if you want to make sure the registry entry is removed, you should place RemoveExtMenuItem here.