How to disable the context menu for an HTML page

Source: Internet
Author: User
Tags knowledge base

Ask a question:

VC Knowledge Base "online magazine," The sixth issue of an article "VC6 Using CHtmlView in dialog box control to display HTML files," many readers wrote that they like this feature. But the drawback is that clicking the right mouse button on the HTML page of the dialog box pops up the context menu. So you can see the source code of the page as you would in IE. In order to prevent users from viewing the HTML code, one has tried to overload the Wm_contextmenu in a Chtmlctrl-derived window, or to disable contextual menus and pop-up menus in the CHtmlView and Chtmlctrl classes, both of which are unsuccessful. So how do you disable this context menu for HTML? This article in order to solve this problem in different ways to improve the last program.

Answer:

The Chtmlctrl class can convert CHtmlView to controls that are used in any window. I use it to write a program called abouthtml, this program implements an HTML dialog box. But the right mouse button is ignored, so clicking the right mouse button in the HTML dialog box pops up the standard browser context menu (figure I), which may be superfluous for some people.


Figure I don't want context menu

In fact, to solve this problem there is a very simple way, it is easy, even without writing any C + + code! Just add one line of instructions to the HTML page:

//

//

This instruction tells the browser not to display the context menu. It can also be written as follows:

//

Oncontextmenu= "Showmymenu (); return False "

//

Showmymenu is a JavaScript process that displays custom menus. One of the example codes in this article ABOUTHTML1 uses is oncontextmenu. The source code can be downloaded at the beginning of this article.

Because the VC knowledge Base is a Web site about C + + and Visual C + +, it has nothing to do with scripting languages such as JavaScript. So we're going to do the same thing in a slightly more complicated way, which is to do it in C + +. For this reason, the normal C + + method is to implement the IDocHostUIHandler interface, but also to do a lot of things. See the documentation for why you want to implement it. The idea of dealing with this problem with Wm_contextmenu or wm_rbuttondown is really the way windows usually do things. But the problem is that the Chtmlctrl window is not a real input window. There are many kinds of windows, just use Spy + + tool to look at our example program to know how many kinds of windows will appear in front of you. As shown in figure II, the browser window has a level three parent/child window on the actual input window.

Dialog

afxframeorview42d//Chtmlctrl

Shell Embedding

Shell DocObject View

Internet Explorer_server

It is an Internet Explorer_server server window that receives input, and if you want to intercept the WM_CONTEXTMENU message, you must subclass the window. In MFC, this means you have to get the HWND and invoke SubclassWindow. Remember, this is an extraordinary way, and Microsoft's guys are definitely banning it, but I wrote another version based on the original program ABOUTHTML2, I did.

Figure II Parent/child relationship in Spy + +

There are a number of ways to get this mysterious Internet Explorer_server hwnd. But FindWindow not, because it only gets the top-level window. Since this server window is a browser's great-grandson (Great-grandchild) and has no siblings at all levels, the following algorithms are set up:

Static HWND Getlastchild (HWND hwndparent)

{

HWND hwnd = hwndparent;

while (TRUE) {

hwnd Hwndchild =:: GetWindow (hwnd, gw_child);

if (hwndchild==null)

return hwnd;

hwnd = Hwndchild;

}

return NULL;

}

This function assumes that only the list inheritance chain, like a window in a browser--that is, each parent window must have a child window--and getting the end (or smallest) child window is the Internet explorer_server window. Once you get the HWND, the rest of the thing is to write a new MFC class to subclass it.

Class Cmyiewnd:public CWnd {

Public

afx_msg void OnContextMenu (cwnd* pwnd, CPoint pos) {}

Declare_message_map ();

};

This class overloads Wm_contextmenu and does nothing else: OnContextMenu is an empty function, the returned object does not display the menu, nor does it call the base class (CWnd) method. When using Cmyiewnd, add an instance to the Cmyhtmlctrl:

//

Class Cmyhtmlctrl:public Chtmlctrl {

Protected

Cmyiewnd M_myiewnd;

};

//

The most critical step in connecting all of this is calling SubclassWindow. But where to call and when to call it? The best time to do this is after the browser loads the page.

void Cmyhtmlctrl::onnavigatecomplete2 (LPCTSTR strurl)

{

if (!m_myiewnd.m_hwnd) {

HWND hwnd = Getlastchild (m_hwnd);

M_myiewnd.subclasswindow (HWND);

}

}

The specific process is this: when the user opens the About dialog box, the dialog box creates a Chtmlctrl window to open the document, and when the browser opens the document, it sends a notification that MFC directs this notice to OnNavigateComplete2. Cmyhtmlctrl::onnavigatecomplete2 calls Getlastchild to get the "real" input window and subclass it. All messages will then go through the Cmyiewnd class to the Internet Explorer_server, including Wm_contextmenu. Note here that IE's HWND can be modified, so if you want to do something else besides the "about" dialog box, you have to do the Unsubclass (and Resubclass) processing of the HWND.

There are two important things to note about using this technique. First, it is very powerful, because you subclass the "real" IE window, you can do almost anything. Second, if you don't use it carefully, it's going to be the worst and worst thing that can happen. Once you've managed the Explorer window in this way, you're putting all bets in. Remember not to play with the browser in an improper way, but to customize it through the formal interface (IDocHostUIHandler)! Otherwise the consequences would be disastrous.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.