Google Answers Logo
View Question
 
Q: C++/MFC Question: How to add browser events to a CDHtmlDialog Application? ( No Answer,   4 Comments )
Question  
Subject: C++/MFC Question: How to add browser events to a CDHtmlDialog Application?
Category: Computers > Programming
Asked by: jimntexas-ga
List Price: $25.00
Posted: 15 Jan 2004 11:00 PST
Expires: 14 Feb 2004 11:00 PST
Question ID: 296817
This question is for experts in computer programming using C++ and the
Microsoft Foundation Classes.

I am writing an MFC dialog application using Visual Studio .Net 2003. 
My dialog  class is derived from MFC's CDHtmlDialog class (set up
using the application wizard).  What I need to be able to do is add
event handlers for the NewWindow2 browser event and other similar
browser events not supported by CDHtmlDialog.  My short term problem
is to stop the program from responding to a 'control-N' keyboard input
from the user.  My longer term goal is to add to my CHtmlDialog
derived class many of the event handlers found in CHtmlView.

In the code for CHtmlDialog ( see dlgdhtml.cpp in the MFC source
files) there is an event sink map for three browser events as follows:

///////////

BEGIN_EVENTSINK_MAP(CDHtmlDialog, CDialog)

ON_EVENT(CDHtmlDialog, AFX_IDC_BROWSER, 252 /* NavigateComplete2 */,
_OnNavigateComplete2, VTS_DISPATCH VTS_PVARIANT)

ON_EVENT(CDHtmlDialog, AFX_IDC_BROWSER, 250 /* BeforeNavigate2 */,
_OnBeforeNavigate2, VTS_DISPATCH VTS_PVARIANT VTS_PVARIANT
VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PBOOL)

ON_EVENT(CDHtmlDialog, AFX_IDC_BROWSER, 259 /* DocumentComplete */,
_OnDocumentComplete, VTS_DISPATCH VTS_PVARIANT)

END_EVENTSINK_MAP()

/////////////

If you look at the event sink map for CHtmlView (located in
viewhtml.cpp) you'll see that they have event handlers not only for
OnNewWindow2, but for many other browser events beyond the three
supported by CHtmlDialog.

How do I add  similar browser event handlers to my CHtmlDialog derived
class to handle the OnNewWindow2 and other similar browser events?
Answer  
There is no answer at this time.

Comments  
Subject: Re: C++/MFC Question: How to add browser events to a CDHtmlDialog Application?
From: lev82-ga on 17 Jan 2004 12:51 PST
 
What you do to add events to your class (say "CMyDialog") is to
mimic/copy code from CHtmlView. So to handle OnNewWindow2 event, this
is what you do:

mydialog.h
===========

class CMyDialog: public CDHtmlDialog
{
....
protected:
   virtual void OnNewWindow2(LPDISPATCH* ppDisp, BOOL* Cancel);
....
   DECLARE_EVENTSINK_MAP()
};

mydialog.cpp
============

BEGIN_EVENTSINK_MAP(CHtmlDlgDlg, CDHtmlDialog)
	ON_EVENT(CHtmlDlgDlg, AFX_IDC_BROWSER, DISPID_NEWWINDOW2,
OnNewWindow2, VTS_PDISPATCH VTS_PBOOL)
END_EVENTSINK_MAP()

.....

void CMyDialog::OnNewWindow( LPDISPATCH* ppDisp, BOOL* bCancel )
{
	// cancel New Windows creation
	*bCancel = TRUE;

	// user will override to handle this notification
	UNUSED_ALWAYS(ppDisp);
}


You can follow similar steps and copy other events over from
CHtmlView. All you have to do is to change a few names so it applies
to your dialog. For example, notice that in your dialog browser has ID
AFX_IDC_BROWSER, and in the view it is AFX_IDW_PANE_FIRST.

However, your "short-term" problem is not as easy to solve, because
it's a little different from others. Even though you add the handler
for DISPID_NEWWINDOW2 event, you will not be able to detect Ctrl-N
click. Check out <a href="http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/webbrowser/reference/ifaces/dwebbrowserevents2/newwindow2.asp">this</a>
MSDN site for list of things that do cause this event. Unfortunately
Ctrl-N is not part of this list.

You would have to resort to a trick with subclassing. Problem with MFC
subclassing is that only 1 class can be in permanent map. A little
class on Codeproject called <a
href="http://www.codeproject.com/cpp/chookwnd.asp?print=true">CHookWnd</a>
attempts to solve this problem. Have a look at it, it might help
trapping the keyboard messages. Unfortunately I was not successful at
it given time I spent trying.

Hope this helps.
Subject: Re: C++/MFC Question: How to add browser events to a CDHtmlDialog Application?
From: jimntexas-ga on 18 Jan 2004 21:18 PST
 
Lev82, I tried exactly what you suggest.  It compiles, but the message
handler isn't ever called.

I'm going to be working with that approach, but I don't think the
event map macros work in such a way that you can just add.

I'm even thinking of just copying the whole CDHtmlDialog source code,
adding the event handlers I want, and then trying to rebuild it.

What I really want to do is to >understand< how to process events from
the browser object in my CDHtmlDialog application.
Subject: Re: C++/MFC Question: How to add browser events to a CDHtmlDialog Application?
From: lev82-ga on 30 Jan 2004 20:10 PST
 
What do you mean it does not get called? Did you try right-clicking a
link and selecting "Open in New Window" - this should trigger it.

Btw, there is a typo in my code i just noticed:
"BEGIN_EVENTSINK_MAP(CMyDialog, CDHtmlDialog)", but you probably
figured that out already...
Subject: Re: C++/MFC Question: How to add browser events to a CDHtmlDialog Application?
From: jimntexas-ga on 01 Feb 2004 21:35 PST
 
"What do you mean it does not get called? Did you try right-clicking a
link and selecting "Open in New Window" - this should trigger it.

Btw, there is a typo in my code i just noticed:
"BEGIN_EVENTSINK_MAP(CMyDialog, CDHtmlDialog)", but you probably
figured that out already..."

My app isn't a browser, so I don't allow right clicks and have no
links.  What does happen is that when the user presses control-N an
instance of internet explorer is instantiated containing the html that
forms the framework of my gui. So it's pretty certain that the
IWebrowser2::NewWindow event is being fired somewhere.

I also thought that if I just copied the CHtmlView newbrowser event
code the event would be handled.  Alas, that flat doesn't work.  The
copied newbrowser handler override is not called in response control-N
as it should be.  So there is more to it than just copying the code
from CHtmlView.

I feel a little better about it since I posted my question because I
notice that  Microsoft Media Player exhibts the same behavoir in some
installations.

I'd like to know how to intercept the newbrowser event in CDHtmlDialog.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy