Shield WebBrower Control Right-click Menu Tips Collection

xiaoxiao2021-03-06  30

1. Method often despised (PretranslateMessage)

I often see how to ask how to shield HTML's right-click menu, and I will answer the WM_RBUTTONDOWN message with the PretranslateMessage function, so someone always says how to make this method, @ _ ​​@, I really don't know why, but I think this is also a way. Yeah, and very simple, so it is listed :)

BOOL CPreTranslateMsgView :: PreTranslateMessage (MSG * pMsg) {if ((pMsg-> message == WM_RBUTTONDOWN) || (pMsg-> message == WM_RBUTTONDBLCLK)) {CPoint point (pMsg-> pt); ScreenToClient (& point);

IHTMLDocument2 * pdoc2 = NULL; IHTMLElement * pElement = NULL; IDispatch * pDisp = NULL; pDisp = GetHtmlDocument (); pDisp-> QueryInterface (IID_IHTMLDocument2, (void **) & pdoc2); pDisp-> Release ();

PDOC2-> ElementFromPoint (Point.x, Point.Y, & Pelement); pdoc2-> release (); if (pelement) {bstr id; pelement-> get_id (& id); pelement-> release (); cstring str = LPCTSTR) (_ BSTR_T) ID; if (str == "layer1") {cmenu menu; menu.loadmenu (idR_Menu1); cmenu * pmenu = menu.getsubmenu (0); pmenu-> TRACKPOPUPMENU (0, PMSG-> PT. X, PMSG-> Pt.y, this);}} return true; // If you want to completely shield, do not display any menus, return True directly, the above code demonstrates how to play yourself in the HTML Want to display menu} else returnch chtmlview :: pretranslateMessage (PMSG);

2. Treat WM_CONTEXTMENU messages through the window of the Subclass IE control

This is the method introduced by MSDN, 111222 translated the article, everyone goes to see: Mask ChtmlViewCwebBrower2 Right-click menu 111222 (translation). Http://www.9cbs.net/develop/read_article.asp?id=10427

3. Using the IDOCHOSTUIHANDLER interface (officially provided method) CDHTMLDialog in vc.net is this method, you can refer to this article you can implement: Display the webpage in the dialog, and block the pop-up menu of IE. Wuya Originally) http://www.9cbs.net/develop/read_article.asp?id=8813

4. Use the COM connection point mechanism to directly handle the event of HTML elements

Let's first see how to shield the right button in VB.

DIM WITHEVENTS M_DOC AS HTMLDocument

Private function m_doc_oncontextmenu () as boolean m_doc_oncontextMenu = false end function

Private sub webbrowser1_downloaduniccomplete () set m_doc = webbrowser1.document End Sub huh, is very simple? Processing the HTMLDocument's onContextMenu event, depending on the specific you need to pop up your own menu, then return TRUE or FALSE. Similarly, if we can respond to the HTML page element objects and make the power of the Mask Right-click menu, in response to the incident of the COM in VB, it is easy to get it with a WitHevents keyword. How to implement in VC? ? We know that COM is implemented by connection point mechanisms, and you have to implement a specific interface in the program, and transfer this interface to the COM object through a certain path and inform it to subscribe to the message. By handling HTMLDocument2's onContextMenu events to demonstrate how to implement shields in the MFC program, you can handle other elements such as DIV, Button to develop the appropriate right-click menu. By reviewing MSDN, you can know that the event to be responed to HTMLDocument2 must implement the HTMLDocumentEvents2 interface in the program. This interface is the DISPINTERFACE type, and the MFC wants to implement a IDispatch interface is simple from the ccmdtarget class and use declare_dispatch_map, begin_dispatch_map, DISP_FUNCTION_ID, END_DISPATCH_MAP, etc. Macro, etc. ), Implementation as follows:

1. Newly built dialog-based engineering is named HTMLDEMODIALOG, add Microsoft Web Browser controls in the main dialog and generate packaging classes. 2. Add #include in the main dialog class header file in CHtmlDemoDialogDlg.h IHTMLDocument2 defined interfaces // #include // defines a method DISPID class CCHtmlDemoDialogDlg HTMLDocumentEvents2 interface: public CDialog { ... DECLARE_DISPATCH_MAP () // declaration dispatch map table public: BOOL onHtmlContextMenu (IHTMLEventObj * pEvtObj); // event handler, the prototype can be described with reference to MSDN HTMLDocumentEvents2 on a DWORD m_dwCookie; // for marking a junction IHTMLDocument2 * PDOC2; // Want to handle the pointer of the COM object of the event ...} 2. In the dialog class implementation file chtmldemodialogdlg.cpp Add #include // Defined AFXCONNECTIONADVISE, AFXConnectionunadvise and other functions, etc. when to use the event to source ... // fill dispatch map table for invoke () call BEGIN_DISPATCH_MAP (CCHtmlDemoDialogDlg, CDialog) DISP_FUNCTION_ID (CCHtmlDemoDialogDlg, "oncontextmenu", DISPID_HTMLDOCUMENTEVENTS2_ONCONTEXTMENU, onHtmlContextMenu, VT_BOOL, VTS_DISPATCH) END_DISPATCH_MAP () .. .CWebBrowserdlg :: CWebBrowserdlg (CWND * PParent / * = null * /): cdialog (cdialog (cParent) {enableAutomation (); // must, otherwise, etc. will fail with getidispatch (). ... }

Bool cchtmldemodialogdlg :: onHtmlContextMenu (ihtmleventobj * pevtobj) {// After successful connection, each user right click to call this function, you can determine the current cursor position according to PevTobj, then determine whether it is your own pop-up menu, let IE pops up menu, still nothing ... Return False;}

void CCHtmlDemoDialogDlg :: OnDocumentCompleteExplorer1 (LPDISPATCH pDisp, VARIANT FAR * URL) {// DocumentComplete event WebBrowser control processing, and initializes pDoc2 pointer and connected to the event source HRESULT hr = m_wb.GetDocument () -> QueryInterface (IID_IHTMLDocument2, (void * *) & pdoc2); BOOL RET = AFXCONNECTIONADVISE (PDOC2, // Connectable object interface pointer DIID_HTMLDOCUMENTS2, // Connection interface ID getidispatch (false), // Embed an IDispatch implementation class an object instance m_xdispatch passed out FALSE , // Donod AddRef & m_dwcookie); // cookie to Break Connection Later ... if (re) {AFXMESSAGEBOX ("Successfully Mount");}} 3. Here, the basic steps are completed, if there is no What disaster occurs, you can see the message box of "Successful Mount", and click the right button in the IE control. Display the menu, disconnect the code as follows: AFXConnectionunadvise (pdoc2, diid_htmldocumentevents2, getidispatch (false), False, m_dwcookie); the rest of the work is handed over to you. (Reprinted)

转载请注明原文地址:https://www.9cbs.com/read-64952.html

New Post(0)