Click here to Skip to main content
16,011,805 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralDAO Databases Pin
13-Mar-01 4:20
suss13-Mar-01 4:20 
GeneralRe: DAO Databases Pin
13-Mar-01 5:50
suss13-Mar-01 5:50 
QuestionHow to make one code with C & C++ Pin
13-Mar-01 3:12
suss13-Mar-01 3:12 
AnswerRe: How to make one code with C & C++ Pin
Christian Graus13-Mar-01 9:31
protectorChristian Graus13-Mar-01 9:31 
GeneralRe: How to make one code with C & C++ Pin
Tim Deveaux13-Mar-01 10:02
Tim Deveaux13-Mar-01 10:02 
GeneralAutomating Internet Explorer Pin
13-Mar-01 1:46
suss13-Mar-01 1:46 
GeneralAutomating Internet Explorer Pin
13-Mar-01 1:46
suss13-Mar-01 1:46 
GeneralRe: Automating Internet Explorer Pin
Sardaukar13-Mar-01 3:21
Sardaukar13-Mar-01 3:21 
You have to implement an object that intercept the events from IWebBrowser2. This interface is DWebBrowseEvents(2). This object - I call it sink dispatcher - must implement the interface IDocHostUIHandler. This interface has a method called TranslateAccelerator. Implement this interface leaving all other interface specific method to return E_NOTIMPL ( = "do default") and TranslateAccelerator returning S_OK ( = "let me do it, do NOT do the default"). For disabling popup menus, you have also to implement the ShowContextMenu method (of the same interface) by simply returning S_OK instead of E_NOTIMPL that will let IE to handle the context menu. But first of all you have to get the IWebBrowser2 object (this is simple, since you are telling that the project is MFC) and to call Advise to let your dispatcher object to do the job (that is, in more cases, to return E_NOTIMPL Smile | :) ).

So: I suppose you have a dialog where the WebBrowser control is incapsulated. I can tell you how to eliminate also the resources and to create dynamically the whole stuff.
1) Create a window. [optional, but maybe you want a toolbar, a status bar etc.]
2) Create a dialog (encapsulated in that window, if you did (1)).
3) Implement an interface derived from DWebBrowserEvents2 (let's call it ISinkDispatcher);
4) Implement an interface derived from IOleClientSite, IOleInPlaceSite (optional) and IDocHostUIHandler (for you, mandatory);
5) in OnInitDialog of your dialog, do this (the OLE API way, I don't use MFC a lot):
BOOL 
WebBrowser_OnInitBrowser(HWND hWnd)
{
	IStorage *pStorage = (IStorage *)NULL;
	ILockBytes *pLockBytes = (ILockBytes *)NULL;
	IWebOleClientSite *pWebOleClientSite = (IWebOleClientSite *)NULL;
	IOleObject *pOleObject = (IOleObject *)NULL;

	pWebOleClientSite = new IWebOleClientSite(hWnd);

	LPWEBDIALOGDATA lpWebDlgData = (LPWEBDIALOGDATA)GetWindowLong(hWnd, DWL_USER);
	lpWebDlgData->pWebOleClientSite = pWebOleClientSite;

	CreateILockBytesOnHGlobal(NULL, TRUE, &pLockBytes);
	StgCreateDocfileOnILockBytes(pLockBytes, 
		STGM_SHARE_EXCLUSIVE | STGM_CREATE | STGM_READWRITE, 0, &pStorage);
	OleCreate(CLSID_WebBrowser, IID_IOleObject, OLERENDER_DRAW, NULL, 
		pWebOleClientSite, pStorage, (LPVOID *)&pOleObject);

        // if you need them
	pWebOleClientSite->m_pIOleObject = pOleObject;
	pWebOleClientSite->m_hWndOwner	 = hWnd;
	
	pWebOleClientSite->ConnectEvents();

	pWebOleClientSite->Show();
	
	pOleObject->Release();
	pStorage->Release();
	pLockBytes->Release();

	return TRUE;
}

(The error handling is for you).
6) ConnectEvents should get the IConnectionPointContainer interface of the IWebBrowser2 object, and to FindConnectionPoint with iid = DIID_DWebBrowserEvents2. If succeeded (= have IE installed Smile | :) ) you can create here your ISinkDiapatcher object and call IConnectionPoint's Advise passing your dispatcher. Something like this:
HRESULT 
IWebOleClientSite::ConnectEvents()
{
	HRESULT hr = E_NOINTERFACE;

	IWebBrowser2 *pWebBrowser = (IWebBrowser2 *)NULL;
	hr = m_pIOleObject->QueryInterface(IID_IWebBrowser2, (LPVOID *)&pWebBrowser);
	if(SUCCEEDED(hr))
	{
		hr = pWebBrowser->put_TheaterMode((VARIANT_BOOL)1);
		hr = pWebBrowser->put_RegisterAsBrowser((VARIANT_BOOL)1);

		IConnectionPointContainer *pCPI = (IConnectionPointContainer *)NULL;
		hr = pWebBrowser->QueryInterface(IID_IConnectionPointContainer , (LPVOID *)&pCPI);
		if(SUCCEEDED(hr))
		{
			hr = pCPI->FindConnectionPoint(DIID_DWebBrowserEvents2, &m_pConnectionPoint);
			if(SUCCEEDED(hr))
			{
				m_pSinkDispatcher = new ISinkDispatcher;
				hr = m_pConnectionPoint->Advise(reinterpret_cast<IDispatch*>(m_pSinkDispatcher), &m_dwCookie);
				if(SUCCEEDED(hr))
				{
					m_pSinkDispatcher->SetClientSite(this);
				}
			}

			pCPI->Release();
		}

		pWebBrowser->Release();
	};

	return hr;
}

(Don't forget: if you have a ConnectEvents, do also a DisconnectEvents on dialog close).

In few moments I'll post a sample about this on CodeTools. If these lines above are quite complicated (and I agree), maybe reading sample, building and debugging on your own can be more helpful that these aglomerated lines...

All the best,
Sardaukar
GeneralCursor shape Pin
13-Mar-01 1:42
suss13-Mar-01 1:42 
GeneralRe: Cursor shape Pin
Christian Graus13-Mar-01 9:38
protectorChristian Graus13-Mar-01 9:38 
GeneralOutlook Express Pin
12-Mar-01 23:51
suss12-Mar-01 23:51 
GeneralRe: Outlook Express Pin
Erik Funkenbusch13-Mar-01 12:54
Erik Funkenbusch13-Mar-01 12:54 
GeneralRe: Outlook Express Pin
14-Mar-01 2:46
suss14-Mar-01 2:46 
GeneralRe: Outlook Express Pin
Erik Funkenbusch14-Mar-01 11:52
Erik Funkenbusch14-Mar-01 11:52 
GeneralRe: Outlook Express Pin
Amit Dey27-Aug-02 12:01
Amit Dey27-Aug-02 12:01 
Generalsocket and timeout Pin
Eric MILLAUD12-Mar-01 23:21
Eric MILLAUD12-Mar-01 23:21 
GeneralRe: socket and timeout Pin
orcun colak13-Mar-01 4:32
orcun colak13-Mar-01 4:32 
GeneralRe: socket and timeout Pin
13-Mar-01 5:56
suss13-Mar-01 5:56 
GeneralRe: socket and timeout Pin
13-Mar-01 5:48
suss13-Mar-01 5:48 
GeneralRe: socket and timeout Pin
Eric MILLAUD13-Mar-01 21:26
Eric MILLAUD13-Mar-01 21:26 
GeneralRe: socket and timeout Pin
Ammar13-Mar-01 23:06
Ammar13-Mar-01 23:06 
QuestionDCOM or CORBA or what? Pin
ov12-Mar-01 21:42
ov12-Mar-01 21:42 
AnswerRe: DCOM or CORBA or what? Pin
Adrian Edmonds12-Mar-01 22:39
Adrian Edmonds12-Mar-01 22:39 
AnswerRe: DCOM or CORBA or what? Pin
Kannan Kalyanaraman13-Mar-01 1:12
Kannan Kalyanaraman13-Mar-01 1:12 
GeneralGet ID of objects ... Pin
Hadi Rezaee12-Mar-01 19:31
Hadi Rezaee12-Mar-01 19:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.