Click here to Skip to main content
15,892,005 members
Articles / Desktop Programming / WTL

Custom Tab Controls, Tabbed Frame and Tabbed MDI

Rate me:
Please Sign up or sign in to vote.
4.90/5 (144 votes)
13 Jul 200522 min read 1.6M   35.6K   395  
An extensible framework for creating customized tabs in ATL/WTL, with a VS.NET-like tab control implementation, tabbed frames, tabbed MDI, and more.
// HtmlView.h : interface of the CHtmlView class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_HtmlView_H__053AD676_0AE2_11D6_8BF1_00500477589F__INCLUDED_)
#define AFX_HtmlView_H__053AD676_0AE2_11D6_8BF1_00500477589F__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

class CHtmlView:
	public CWindowImpl<CHtmlView, CAxWindow>
{
public:
	HRESULT get_Control(IUnknown** ppControl)
	{
		if(m_hWnd != NULL)
		{
			return CAxWindow::QueryControl(ppControl);
		}
		return NULL;
	}

	HRESULT get_Browser(IWebBrowser2** ppBrowser)
	{
		if(m_hWnd != NULL)
		{
			return CAxWindow::QueryControl(ppBrowser);
		}
		return NULL;
	}

public:
	DECLARE_WND_SUPERCLASS(_T("HtmlView"), CAxWindow::GetWndClassName())

	BOOL PreTranslateMessage(MSG* pMsg)
	{
		if((pMsg->message < WM_KEYFIRST || pMsg->message > WM_KEYLAST) &&
		   (pMsg->message < WM_MOUSEFIRST || pMsg->message > WM_MOUSELAST))
			return FALSE;

		// give HTML page a chance to translate this message
		return (BOOL)SendMessage(WM_FORWARDMSG, 0, (LPARAM)pMsg);
	}

	BEGIN_MSG_MAP(CHtmlView)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)
		MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
		MESSAGE_HANDLER(WM_SETFOCUS, OnSetFocus)
		DEFAULT_REFLECTION_HANDLER()
	END_MSG_MAP()

// Handler prototypes (uncomment arguments if needed):
//	LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
//	LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//	LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

	LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		// "base::OnCreate"
		LRESULT nResult = DefWindowProc(uMsg, wParam, lParam);

		//CAxWindow::QueryControl(&m_pBrowser);
		//if(m_pBrowser)
		//{
		//	DispEventAdvise(m_pBrowser, &DIID_DWebBrowserEvents2);
		//}

		bHandled = TRUE;
		return nResult;
	}

	LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
	{
		//if(m_pBrowser)
		//{
		//	DispEventUnadvise(m_pBrowser, &DIID_DWebBrowserEvents2);
		//}

		bHandled = FALSE;
		return 0;
	}

	LRESULT OnSetFocus(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
	{
		// The base class ActiveX hosting code for forwarding focus doesn't work well
		// with Internet Explorer, so we'll do it ourself.
		CComPtr<IOleObject> oleObject;
		this->QueryControl(&oleObject);
		if(oleObject)
		{
			CComPtr<IOleClientSite> oleClientSite; 
			this->QueryHost(&oleClientSite);
			if(oleClientSite)
			{
				CRect rcControl;
				this->GetClientRect(&rcControl);
				oleObject->DoVerb(OLEIVERB_UIACTIVATE, NULL, oleClientSite, 0, m_hWnd, &rcControl);
			}
		}

		// Other variations:
		/*
		CComPtr<IWebBrowser2> webBrowser;
		this->QueryControl(&webBrowser);
		if(webBrowser)
		{
			CComPtr<IDispatch> documentDisp;
			webBrowser->get_Document(&documentDisp);
			CComQIPtr<IHTMLDocument2> htmlDocument2(documentDisp);
			if(htmlDocument2)
			{
				bool handled = false;
				CComPtr<IHTMLElement> activeElement;
				htmlDocument2->get_activeElement(&activeElement);
				CComQIPtr<IHTMLElement2> activeElement2(activeElement);
				if(activeElement2)
				{
					handled = true;
					activeElement2->focus();
				}

				if(!handled)
				{
					CComPtr<IHTMLWindow2> parentWindow;
					htmlDocument2->get_parentWindow(&parentWindow);
					if(parentWindow)
					{
						parentWindow->focus();
					}
				}
			}
		}
		*/

		/*
		HWND hWndFocus = ::GetFocus();

		HWND hWndShellEmbedding = ::FindWindowEx(m_hWnd, NULL, _T("Shell Embedding"), NULL);
		if(hWndShellEmbedding)
		{
			HWND hWndShellDocObjectView = ::FindWindowEx(hWndShellEmbedding, NULL, _T("Shell DocObject View"), NULL);
			if(hWndShellDocObjectView )
			{
				HWND hWndInternetExplorerServer = ::FindWindowEx(hWndShellDocObjectView, NULL, _T("Internet Explorer_Server"), NULL);
				if(!::IsChild(hWndInternetExplorerServer, hWndFocus))
				{
					::SetFocus(hWndInternetExplorerServer);
				}
			}
		}
		*/

		/*
		HWND hWndFocus = ::GetFocus();

		HWND hWndShellEmbedding = ::GetWindow(m_hWnd, GW_CHILD);
		if(hWndShellEmbedding)
		{
			HWND hWndShellDocObjectView = ::GetWindow(hWndShellEmbedding, GW_CHILD);
			if(hWndShellDocObjectView )
			{
				HWND hWndInternetExplorerServer = ::GetWindow(hWndShellDocObjectView, GW_CHILD);
				if(!::IsChild(hWndInternetExplorerServer, hWndFocus))
				{
					::SetFocus(hWndInternetExplorerServer);
				}
			}
		}
		*/

		bHandled = FALSE;
		return 0;
	}

};


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

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_HtmlView_H__053AD676_0AE2_11D6_8BF1_00500477589F__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
United States United States
Daniel Bowen used to work as a Software Engineer for Evans & Sutherland in Salt Lake City, Utah working on the modeling tools for high end flight simulators. He then worked for the startup company WiLife Inc. in Draper, Utah working on the software portion of an easy to use and affordable digital video surveillance system. WiLife Inc. is now part of Logitech Inc.

Comments and Discussions