Mask ChtmlViewWebBrower control Right-click Menu Tips Collection

zhaozj2021-02-16  51

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 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;}

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

New Post(0)