Deep customization of WebBrowser controls in MFC applications (RPM)

Source: Internet
Author: User
Tags bool contains numeric value resource client
Web|webbrowser control as shown in the VC + + 6.0 Demo program Mfcie, it is easy to use the application Wizard to create an MFC application with Web navigation capabilities. Based on this, this paper discusses two problems: the realization of the resource self contained, the context menu and the complete control of the shortcut key.
I. Resource self-contained and RES agreement

The so-called resources are self-contained, meaning that in the final product HTML documents (including related resources such as scripts, pictures) become part of the execution file, like other resources (such as application icons), rather than distributed as separate files.

Resource self-contained not only makes the product distribution more convenient, but also hides more realization details in front of the end users, which has certain practical significance.

MFC class CHtmlView not only WebBrowser all the functions of the control (such as navigation, user interface configuration, etc.) into C + + method and MFC event mapping, but also provides many "original" features. One of these is the ability to read Web pages from the application that contains it. The main method for accomplishing this task is chtmlview::loadfromresource (), whose prototype is as follows:

BOOL LoadFromResource (LPCTSTR lpszresource); To specify a resource ID with a string
BOOL LoadFromResource (UINT nres); Specify a resource ID with a number
LoadFromResource () relies on the res://protocol (ie, when the server specified by the URL cannot be found, the error prompt page displayed is drawn using the RES protocol.) The URL of the RES protocol differs from the URL format used for common HTTP or FTP protocols, and its complete syntax is:

res://resource_file[/resource_type]/resource_id
Where Resource_file is the name of the executable file that contains the target resource. Resource_type is a resource type, it may be represented by a number, or it may be represented by a string. When a resource is a custom or not directly supported type, a string indicating the resource type (such as gif,jpeg) can be used in the. RC file, which is often represented as a numeric value when the resource is a known type (such as a bitmap of 2,html 23). A complete list of known resource types can be found in Winuser.h, which is a set of Rt_ constants.

Chtmlview::loadfromresource () always assumes that the target resource is in the current module as the execution file. But in practical applications, we tend to classify resources into different files. To resolve this problem, you can define a derived class Chtmlviewex override this method, and simply modify the original method to introduce the module parameters:

"Listing 1"
BOOL Chtmlviewex::loadfromresource (LPCTSTR lpszmodule, LPCTSTR lpszresource)
{
CString Strresourceurl;
Strresourceurl.format (_t ("res://%s/%s"), Lpszmodule, Lpszresource);
Navigate (strresourceurl, 0, 0, 0);
return TRUE;
}
Second, context menu and shortcut key control

In practice, to prevent users from viewing page source files or to prevent users from opening another window of the current page with a shortcut key, we often customize the context menu provided by the WebBrowser control (right-click on the page) or shortcut keys (such as CTRL + N) to open a new window.

Both the WebBrowser control and the CHtmlView class do not directly provide a way to customize the above operation, and therefore must be accomplished by implementing the IDocHostUIHandler interface. In this interface, the methods for implementing context menus and shortcut key controls are ShowContextMenu () and TranslateAccelerator () respectively.

Because it is more convenient to use the MFC encapsulation class CHtmlView than to apply the WebBrowser control directly, you can consider integrating the custom interface support functionality into the MFC framework. The basic ideas of implementation are as follows:

Create a new OLE client station and implement the interface in it Idocuihandler
Replace the default configuration with a new management class in InitInstance () to introduce the client station
Based on the above ideas, we can create derived class Ccustomwebbrowsersite from COleControlSite and implement IDocHostUIHandler in a derived class. COleControlSite is defined in VC + + Mfcsrcoccimpl.h to encapsulate the control client station. The new customer station is defined as:

"Listing 2"
Class Ccustomwebbrowsersite:public COleControlSite
{
Public
Ccustomwebbrowsersite (COleControlContainer *pcnt):
COleControlSite (pcnt) {}
Protected
Declare_interface_map ();
Begin_interface_part (Dochostuihandler, IDocHostUIHandler)
Stdmethodimp ShowContextMenu (DWORD, point*, iunknown*, idispatch*);
Stdmethodimp TranslateAccelerator (lpmsg, const guid*, DWORD);
STDMETHODIMP GetHostInfo (Dochostuiinfo);
STDMETHODIMP EnableModeless (BOOL);
STDMETHODIMP OnDocWindowActivate (BOOL);
STDMETHODIMP OnFrameWindowActivate (BOOL);
Stdmethodimp ResizeBorder (Lpcrect, ioleinplaceuiwindow*, BOOL);
Stdmethodimp GetOptionKeyPath (lpolestr*, DWORD);
STDMETHODIMP GetDropTarget (idroptarget*, idroptarget**);
STDMETHODIMP GetExternal (idispatch**);
STDMETHODIMP TranslateUrl (DWORD, olechar*, olechar**);
Stdmethodimp FilterDataObject (idataobject*, idataobject**);
Stdmethodimp ShowUI (DWORD, ioleinplaceactiveobject*,
iolecommandtarget*, ioleinplaceframe*, ioleinplaceuiwindow*);
STDMETHODIMP HideUI (void);
STDMETHODIMP UpdateUI (void);
End_interface_part (Dochostuihandler)
};
As described above, we are interested in the main methods of this interface are ShowContextMenu () and TranslateAccelerator () two. The implementation code for the ShowContextMenu () in the derived class Ccustomwebbrowsersite is shown as an example of a completely forbidden context menu:

"Listing 3"
STDMETHODIMP Ccustomwebbrowsersite::xdochostuihandler::showcontextmenu (
DWORD, point*, iunknown*, idispatch*)
{
Method_prologue (Ccustomwebbrowsersite, Dochostuihandler)
return S_OK; Prohibit menu display
}
In a similar way, you can close a shortcut key that is directly responsive to a control:

"Listing 4"
STDMETHODIMP Customwebbrowsersite::xdochostuihandler::translateaccelerator (lpmsg lpMsg,const GUID __RPC_FAR * pguidCmdGroup, DWORD nCmdID)
{
Method_prologue (Ccustomwebbrowsersite, Dochostuihandler)
return S_OK; Turn off shortcut keys
}
After the IDocHostUIHandler interface is implemented, we also need a management class Ccustomoccmanager to support Ccustomwebbrowsersite, The new management class Ccustomoccmanager is derived from COccManager and COccManager is also defined in Mfcsrcoccimpl.h:

"Listing 5"
Class Ccustomoccmanager:public COccManager
{
Public
Ccustomoccmanager () {}
colecontrolsite* createsite (colecontrolcontainer* PCC)
 



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.