首页  编辑  

禁止/替换WebBrowser的右键菜单

Tags: /超级猛料/OS.操作系统/IE.扩展/TWebBrowser/输入/   Date Created:

disable the Context Menu in a TWebbrowser?

var

 HookID: THandle;

function MouseProc(nCode: Integer; wParam, lParam: Longint): Longint; stdcall;

var

 szClassName: array[0..255] of Char;

MausPos: TPoint;

 X, Y: Integer;

const

 ie_name = 'Internet Explorer_Server';

begin

GetCursorPos(MausPos);

 x := MausPos.x;

 y := MausPos.y;

 case nCode < 0 of

   True:

     Result := CallNextHookEx(HookID, nCode, wParam, lParam)

     else

       case wParam of

         WM_RBUTTONDOWN,

         WM_RBUTTONUP:

           begin

             GetClassName(PMOUSEHOOKSTRUCT(lParam)^.HWND, szClassName, SizeOf(szClassName));

             if lstrcmp(@szClassName[0], @ie_name[1]) = 0 then

        begin

               Result := HC_SKIP ; ///

          Form1.PopupMenu1.Popup(X,Y);

        end

             else

               Result := CallNextHookEx(HookID, nCode, wParam, lParam);

           end

           else

             Result := CallNextHookEx(HookID, nCode, wParam, lParam);

       end;

 end;

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

 HookID := SetWindowsHookEx(WH_MOUSE, MouseProc, 0, GetCurrentThreadId());

end;

procedure TForm1.FormDestroy(Sender: TObject);

begin

 if HookID <> 0 then

   UnHookWindowsHookEx(HookID);

end;

procedure TForm1.Button1Click(Sender: TObject);

begin

 Webbrowser1.Navigate('http://www.google.com');

end;

---------------------------------------

uses

 MSHTML_TLB;

function TForm1.WebBrowser1ShowContextMenu(const dwID: Cardinal;

 const ppt: PPoint; const pcmdtReserved: IInterface;

 const pdispReserved: IDispatch): HRESULT;

var

 myHTMLWindow2: IHTMLWindow2;

 oHTMLEvent: IHTMLEventObj;

 oHTMLDoc2: IHTMLDocument2;

begin

 //next block gives access to the event object fired when the user

 //right-clicks inside the webBrowser control

 oHTMLDoc2 := EmbeddedWB1.Document as IHTMLDocument2; //interface to the displayed doc

 myHTMLWindow2 := oHTMLDoc2.parentWindow;

 //interface to the window that handles the events

 oHTMLEvent := myHTMLWindow2.event; //HERE is the event object

 if oHTMLEvent.ctrlKey = system.False then

 begin //if the Ctrl key is NOT pressed during right-click..

   oHTMLEvent.returnValue := False; //...kills the default pop-up of IE

   Result := 0;

   //open my PopUp nemu at the absolute X and Y coordinates of the mouse

   PopupMenu1.Popup(Mouse.CursorPos.X, Mouse.CursorPos.Y);

 end;

end;

procedure TForm1.Button1Click(Sender: TObject);

begin

 EmbeddedWB1.Navigate('www.swissdelphicenter.com');

end;