0. This document download (http://skinart.y365.com/9cbs/htmlcontextMenu.zip) (code is just a demonstration, no error checking, please note)
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 a method introduced by MSDN, 111222 translates the article, everyone goes to see: Shield ChtmlView / CWebBrower2 Right-click Menu Unofficial Method 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 functionprivate sub webbrowser1_downloadloadComplete () set m_doc = webbrowser1.document end subthasser, 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
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;}