Mask ChtmlViewWebBrower control Right-click Menu Tips Collection

zhaozj2021-02-16  129

? 0. This document download (http://skinart.y365.com/9cbs/htmlcontextMenu.zip) (code is just a demonstration, no error check, please note) 1. Method for contempt (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 also listed :) Bool cpretranslateMsgview :: PretranslateMsGView :: PretranslateMsSGView :: PretranslateMessage (MSG * PMSG) {? If ((PMSG-> Message == WM_RBUTTONDOWN) || (PMSG-> message == WM_RBUTTONDBLCLK)) {? ? ?? screenToClient (& point); ?? htmldocument2 * pdoc2 = null; ?? htmlelement * 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, Returned directly True is in line with these code demonstrates how to play the element of a specific ID in HTML pop up the menu you want to display ??} else ?? Return ChtmlView :: PretranslateMsSage (PMSG);} 2. Window of the Subclass IE control and Handling WM_CONTEXTMENU message? This is a method introduced by MSDN, 111222 translates the article, everyone goes to see:? Mask 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, I want to implement it yourself. This article:? Display the webpage in the dialog, and block the pop-up menu of IE ??? Wuya (original) http://www.9cbs.net/develop/read_article.asp?id=8813 4. Utilization COM connection point mechanism, directly handle events of HTML elements? Let's first look at how to shield off in VB. How do you do it: • DIM WITHEVENTS M_DOC AS HTMLDOCUMENT? Private function m_doc_oncontextMenu () as boolean ??? ?? m_doc_oncontextMenu = False? End funse? Private sub webbrowser1_download limited () ??? ?? set m_doc = webbrowser1.document? End Sub ?? Oh, is it 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. ? In the same way, if we can respond to the HTML page element object, we can achieve the purpose of the Mask Right-click menu, in response to the event in VB is encapsulated, easy to use a withevents keyword. How to achieve it in VC? ? ? We know that COM is implemented by connection point mechanism, you have to implement a specific interface in the program, and transfer the pointer of 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. • You can know the HTMLDocument2 interface by reviewing the MSDN, you must implement the HTMLDocumentEvents2 interface in the program. This interface is the DISPINTERFACE type, and the MFC wants to implement a IDispatch interface is to derive a new class from the ccmdtarget class and use declare_dispatch_map, begin_dispatch_map Macro such as DISP_FUNCTION_ID, END_DISPATCH_MAP, etc., because chtmlView or cdialog is indirectly derived from ccmdTarget, so it is possible to implement this interface directly, add IE controls as an example of dialog box (slightly modified) ChtmlView), implementation as follows:? 1. New dialog box is named HTMLDEMODIALOG, add the Microsoft Web Browser control in the main dialog and generate packaging classes. ? 2. Add ?? # includeOcument2 and other interfaces in the main dialog class header file chtmldemodialogdlg.h ?? # ketom

?? # include

/ / Define how to interface HTMLDocumenTevents2 DISPID

?

?? Class Cchtmldemodialogdlg: Public CDialog

?? {

?? ...

??? Declare_dispatch_map () // Declaration Dispatch Map table

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

?? public:

??? Bool onhtmlContextMenu (Ihtmleventobj * pevtobj); ??? // Event handler, prototype can refer to the description of HTMLDocumentEvents2 in MSDN

??? DWORD M_DWCOOKIE;

??? // Tag a connection point

??? htmldocument2 * pdoc2;

??? // want to handle the pointer to the COM object of the event

?? ...

??}

? 2. Add in the dialog box to implement file chtmldemodialogdlg.cpp

?? # include

// Define functions such as AfxConnectionAdvise, AFXConnectionunadvise, etc., when they are connected to the event source.

?? ...

?? // Fill the Dispatch Map table for invoke () call

?? begin_dispatch_map (cxtmldemodialogdlg, 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 use GetIndispatch I will fail. ??? ... ??} ?? Bool cchtmldemodialogdlg :: onhtmlcontextMenu (htmleventobj * pevtobj) ?? {???? // After successful connection, each user right click will call This function, you can determine the current cursor position according to PevTobj, then determine whether you pop up the menu, let IE pop up menu, still don't do anything ... ??? Return false; ??} ?? void cchtmldemodialogdlg :: OndocumentCompleteExplorer1 (LPDISPATCH PDISP, VARIANT FAR * URL)? {??? // Handling the DocumentComplete event of the WebBrowser control, and initializes the PDOC2 pointer and connects to the event source ??? HRESULT HR = m_wb.getdocument () -> queryinterface (IID_IHTMLDocument2, (void **) & pdoc2); ??? Bool Ret = AFXConnectionAdvise (???? pdoc2, ?? // Interface pointer to which can be connected to the object ???? DIID_HTMLDOCUMENTS2,? // Connection interface ID ???? GetIdispatch FALSE),? // A object instance m_xdispatch that implements the embeddispatch implementation class ???? false, ???? // Donod AddRef ???? & m_dwcookie); ?? // cookie to Break Connection Later ... ??? f i i ("Successfully Mount"); ???} ??}? 3. Here, the basic steps are completed, if there is no disaster after running If you happen, you can see the message box of "Successful Mount", and click the right button in the IE control, do not pop up the menu, disconnect the code connection code as follows: ?? AfxConnectionunadvise (pdoc2, ???????? DIID_HTMLDOCUM Entevents2, ????????? getidispatch (false),? ?????????? false, ????????? m_dwcookie); the rest of the good work is handed over to you .

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

New Post(0)