Controls the appearance and behavior of the webbrowser Control

Source: Internet
Author: User

On csdn, we often see the following two problems:
1. In the MFC application, if a webbrowser control (including chtmlview) is created, how can I disable the three-dimensional border of the control?
2. In the MFC application, if a webbrowser control (including chtmlview) is created, how can I disable the scroll bar of the control?

In fact, the solutions to these two problems are the same. Let's start from the beginning.

From Internet Explorer 4.0, the container of the webbrowser control can use the idochostuihandler interface to customize the appearance and some behaviors of the webbrowser control. Both of the above problems can be achieved through the implementation of this interface. However, it is very painful for programmers to implement the container of the webbrowser control by themselves, in addition, the chtmlview in MFC and the packaging class generated after the webbrowser control is inserted in IDE already have complete function encapsulation, so few people are willing to develop their own containers from the beginning. Unfortunately, the MFC 6.0 version released with Visual C ++ 4.2 does not support the idochostuihandler interface (by the way, starting with Visual C ++ 7.0, MFC supports this interface), so these problems are highlighted in Visual C ++ 6.0.

To solve this problem, Lao Han specially wrote a class to complete the work (see the code below ). The class name is cwebuicontroller. It is easy to use. The dialog box contains the webbrowser control as an example:

Assume that the variable corresponding to the webbrowser control in the dialog box is m_webnavigator. The following steps are required:

1. Add cwebuicontroller m_webuictrl to the dialog box class;

2. Add the following code to the oninitdialog function in the dialog box:

M_webadctrl = NULL; <br/> lpunknown punk1 = m_web.getcontrolunknown (); <br/> If (punk1! = NULL) <br/>{< br/> iwebbrowser2 * pwb2 = NULL; <br/> hresult hR = punk1-> QueryInterface (iid_iwebbrowser2, (void **) & pwb2); <br/> If (succeeded (HR) & pwb2! = NULL) <br/>{< br/> If (! M_webadctrl) <br/>{< br/> m_webadctrl = new cwebuicontroller (); <br/>}< br/> m_webadctrl-> addref (); </P> <p> m_webadctrl-> enable3dborder (true); // This code disables the 3D border <br/> m_webadctrl-> enablescrollbar (false ); // This code disables the scroll bar <br/> m_webadctrl-> setwebbrowser (pwb2); <br/> m_webadctrl-> release (); <br/> pwb2-> release (); <br/>}< br/>}

 

3. Add the following code to the ondestroy function in the dialog box:

M_webadctrl-> setwebbrowser (null );

 

You can also call enable3dborder () or enablescrollbar () at runtime. After the call is complete, call the refresh function of the webbrowser control.

This class is implemented in an independent header file webuicontroller. h, the complete source code is as follows; from the source code, it is not difficult to see that such functions can be easily expanded, such as controlling the display of associated menus.

Webuicontroller. H source code: </P> <p> // Class Name: cwebuicontroller <br/> // Author: Dandy Cheung <br/> // Email: dandycheung@21cn.com <br/>/date: </P> <p> # ifndef _ webuicontroller_h __< BR/> # DEFINE _ webuicontroller_h __</P> <p> # If _ msc_ver> 1000 <br/> # pragma once <br/> # endif // _ msc_ver> 1000 </P> <p> # include <mshtmhst. h> <br/> # include <exdisp. h> <br/> # include <exdispid. h> </P> <p> inline <br/> hresul T _ coadvise (iunknown * punkcp, iunknown * punk, const IID & IID, lpdword PDW) <br/>{< br/> iconnectionpointcontainer * pcpc = NULL; <br/> iconnectionpoint * PCP = NULL; <br/> hresult hres = punkcp-> QueryInterface (iid_iconnectionpointcontainer, (void **) & pcpc ); <br/> If (succeeded (hres) & pcpc! = NULL) <br/>{< br/> hres = pcpc-> findconnectionpoint (IID, & PCP); <br/> If (succeeded (hres) & PCP! = NULL) <br/>{< br/> hres = PCP-> advise (punk, PDW); <br/> PCP-> release (); <br/>}</P> <p> pcpc-> release (); <br/>}</P> <p> return hres; <br/>}</P> <p> inline <br/> hresult _ counadvise (iunknown * punkcp, const IID & IID, dword dw) <br/> {<br/> iconnectionpointcontainer * pcpc = NULL; <br/> iconnectionpoint * PCP = NULL; <br/> hresult hres = punkcp-> QueryInterface (iid_iconnectionpointcontainer, (void **) & pcpc ); <Br/> If (succeeded (hres) & pcpc! = NULL) <br/>{< br/> hres = pcpc-> findconnectionpoint (IID, & PCP); <br/> If (succeeded (hres) & PCP! = NULL) <br/>{< br/> hres = PCP-> unadvise (DW); <br/> PCP-> release (); <br/>}</P> <p> pcpc-> release (); <br/>}</P> <p> return hres; <br/>}</P> <p> class cwebuicontroller: Public dwebbrowserevents2, public idochostuihandler <br/>{< br/> ulong m_urefcount; </P> <p> iwebbrowser2 * m_pwebbrowser2; <br/> DWORD m_dwcookie; </P> <p> bool m_benable3dborder; <br/> bool m_benablescrollbar; </P> <p> Public: <br/> cwebui Controller (): m_urefcount (0), m_pwebbrowser2 (null), m_dwcookie (0) <br/>{< br/> m_benable3dborder = true; <br/> m_benablescrollbar = true; <br/>}</P> <p> virtual ~ Cwebuicontroller () <br/>{< br/>}</P> <p> protected: <br/> // iunknown methods <br/> stdmethod (QueryInterface) (refiid riid, void ** ppvobject) <br/>{< br/> * ppvobject = NULL; </P> <p> If (is1_guid (riid, diid_dwebbrowserevents2) | <br/> isequalguid (riid, iid_idispatch) <br/>{< br/> * ppvobject = (dwebbrowserevents2 *) This; <br/> addref (); <br/> return s_ OK; <br/>}< br/> else if (isequalguid (riid, II D_idochostuihandler) | <br/> isequalguid (riid, iid_iunknown) <br/>{< br/> * ppvobject = (idochostuihandler *) This; <br/> addref (); <br/> return s_ OK; <br/>}</P> <p> return e_nointerface; <br/>}</P> <p> stdmethod _ (ulong, addref) (void) <br/>{< br/> m_urefcount ++; <br/> return m_urefcount; <br/>}</P> <p> stdmethod _ (ulong, release) (void) <br/>{< br/> m_urefcount --; <br/> ulong urefcount = m_urefcou NT; <br/> If (urefcount = 0) <br/> Delete this; </P> <p> return urefcount; <br/>}</P> <p> // idispatch methods <br/> stdmethod (gettypeinfocount) (unsigned int far * pctinfo) <br/>{< br/> return e_notimpl; <br/>}</P> <p> stdmethod (gettypeinfo) (uint itinfo, lcid, itypeinfo ** pptinfo) <br/>{< br/> return e_notimpl; <br/>}</P> <p> stdmethod (getidsofnames) (refiid riid, olechar far * rgsznames, <B R/> unsigned int cnames, lcid, dispid far * rgdispid) <br/>{< br/> return e_notimpl; <br/>}</P> <p> stdmethod (invoke) (dispid dispidmember, refiid riid, lcid, word wflags, <br/> dispparams * pdispparams, variant * pvarresult, <br/> raise info * p1_info, uint * puargerr) <br/>{< br/> If (! Pdispparams) <br/> return e_invalidarg; </P> <p> switch (dispidmember) <br/> {<br/> // The parameters for this dispid are as follows: <br/> // [0]: URL to navigate to-vt_byref | vt_variant <br/> // [1]: an object that evaluates to the top-level or frame <br/> // webbrowser object corresponding to the event. <br/> case dispid_navigatecomplete2: </P> <p> // <br/> // The idochostuihandler Associa Tion must be set <br/> // up every time we navigate to a new page. <br/> // <br/> If (pdispparams-> cargs> = 2 & pdispparams-> rgvarg [1]. vt = vt_dispatch) <br/> setcustomdoc (pdispparams-> rgvarg [1]. pdispval); <br/> else <br/> return e_invalidarg; </P> <p> break; </P> <p> default: <br/> break; <br/>}</P> <p> return s_ OK; <br/>}</P> <p> // idochostuihandler methods <br/> protected: <br/> stdmethod (showcont Extmenu) (DWORD dwid, point far * PPT, iunknown far * pcmdtreserved, <br/> idispatch far * pdispreserved) <br/>{< br/> return e_notimpl; <br/>}</P> <p> stdmethod (gethostinfo) (dochostuiinfo far * pinfo) <br/>{< br/> If (pinfo! = NULL) <br/>{< br/> pinfo-> dwflags | = (m_benable3dborder? 0: dochostuiflag_no3dborder); <br/> pinfo-> dwflags | = (m_benablescrollbar? 0: dochostuiflag_scroll_no); <br/>}</P> <p> return s_ OK; <br/>}</P> <p> stdmethod (showui) (DWORD dwid, ioleinplaceactiveobject far * pactiveobject, <br/> iolecommandtarget far * pcommandtarget, <br/> ioleinplaceframe far * pframe, <br/> ioleinplaceuiwindow far * pdoc) <br/>{< br/> return e_notimpl; <br/>}</P> <p> stdmethod (hideui) (void) <br/>{< br/> return e_notimpl; <br/>}</P> <p> stdmethod (updateu I) (void) <br/>{< br/> return e_notimpl; <br/>}</P> <p> stdmethod (enablemodeless) (bool fenable) <br/>{< br/> return e_notimpl; <br/>}</P> <p> stdmethod (ondocwindowactivate) (bool factivate) <br/>{< br/> return e_notimpl; <br/>}</P> <p> stdmethod (onframewindowactivate) (bool factivate) <br/>{< br/> return e_notimpl; <br/>}</P> <p> stdmethod (resizeborder) (lpcrect prcborder, ioleinplaceuiwindow far * Puiwindow, <br/> bool framewindow) <br/>{< br/> return e_notimpl; <br/>}</P> <p> stdmethod (translateaccelerator) (lpmsg, const guid far * pguid1_group, <br/> DWORD n1_id) <br/>{< br/> return e_notimpl; <br/>}</P> <p> stdmethod (getoptionkeypath) (lpolestr far * pchkey, dword dw) <br/>{< br/> return e_notimpl; <br/>}</P> <p> stdmethod (getdroptarget) (idroptarget * pdroptarget, <br/> idroptarge T ** ppdroptarget) <br/>{< br/> return e_notimpl; <br/>}</P> <p> stdmethod (getexternal) (idispatch ** ppdispatch) <br/>{< br/> return e_notimpl; <br/>}</P> <p> stdmethod (translateurl) (DWORD dwtranslate, olechar * pchillin, <br/> olechar ** ppchurchill) <br/>{< br/> return e_notimpl; <br/>}</P> <p> stdmethod (filterdataobject) (idataobject * PDO, idataobject ** ppdoret) <br/>{< br/> return e_notimpl; <br />}</P> <p> Public: <br/> hresult setwebbrowser (iwebbrowser2 * pwebbrowser2) <br/>{< br/> // unadvise the event sink, if there was a <br/> // previous reference to the webbrowser control. <br/> If (m_pwebbrowser2) <br/>{< br/> _ counadvise (m_pwebbrowser2, diid_dwebbrowserevents2, m_dwcookie); <br/> m_dwcookie = 0; <br/> // _ counadvise has been release/m_pwebbrowser2-> release (); <br/>}</P> <p> m_pwebbr Owser2 = pwebbrowser2; <br/> If (pwebbrowser2 = NULL) <br/> return s_ OK; </P> <p> m_pwebbrowser2-> addref (); </P> <p> return _ coadvise (m_pwebbrowser2, (idispatch *) This, diid_dwebbrowserevents2, & m_dwcookie ); <br/>}</P> <p> void enable3dborder (bool benable = true) <br/>{< br/> m_benable3dborder = benable; <br/>}</P> <p> void enablescrollbar (bool benable = true) <br/>{< br/> m_benablescrollbar = benabl E; <br/>}</P> <p> PRIVATE: <br/> void setcustomdoc (lpdispatch lpdisp) <br/>{< br/> If (lpdisp = NULL) <br/> return; </P> <p> iwebbrowser2 * pwebbrowser2 = NULL; <br/> hresult hR = lpdisp-> QueryInterface (iid_iwebbrowser2, (void **) & pwebbrowser2); </P> <p> If (succeeded (HR) & pwebbrowser2) <br/>{< br/> idispatch * pdoc = NULL; <br/> hR = pwebbrowser2-> get_document (& pdoc ); </P> <p> If (succeeded (HR) & pdoc) <BR/>{ <br/> icustomdoc * pcustdoc = NULL; <br/> hR = pdoc-> QueryInterface (iid_icustomdoc, (void **) & pcustdoc ); <br/> If (succeeded (HR) & pcustdoc! = NULL) <br/>{< br/> pcustdoc-> setuihandler (this); <br/> pcustdoc-> release (); <br/>}</P> <p> pdoc-> release (); <br/>}</P> <p> pwebbrowser2-> release (); <br/>}< br/>}; </P> <p> # endif/_ webuicontroller_h__< br/>
After the above implementation, if the setting does not take effect after opening the web page, you should call the Web Control refresh function to refresh the page! Another point to open the page should be placed after the settings!

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.