Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Custom Tab Controls, Tabbed Frame and Tabbed MDI

, 13 Jul 2005
An extensible framework for creating customized tabs in ATL/WTL, with a VS.NET-like tab control implementation, tabbed frames, tabbed MDI, and more.
tabbingframework_demo.zip
TabbedSDISplitter
Release
TabbedSDISplitter.exe
res
Doc.ico
folder_c.ico
folder_o.ico
TabbedSDISplitter.exe.manifest
TabbedSDISplitter.ico
toolbar.bmp
TabbedSDISplitter.dsp
TabbedSDISplitter.dsw
TabDemo
Release
TabDemo.exe
res
error.ico
folder_closed.ico
folder_open.ico
info.ico
log.ico
TabDemo.exe.manifest
TabDemo.ico
TabDemodoc.ico
toolbar.bmp
warning.ico
TabDemo.dsp
TabDemo.dsw
DockingDemo
DockingDemo.dsp
DockingDemo.dsw
Release
DockingDemo.exe
res
DockingDemo.exe.manifest
DockingDemo.ico
DockingDemodoc.ico
error.ico
folder_closed.ico
folder_open.ico
info.ico
log.ico
msdev_tab_icons.bmp
toolbar.bmp
warning.ico
include
Sergey Klimov
SimpleTabbedMDIDemo
Release
SimpleTabbedMDIDemo.exe
res
SimpleTabbedMDIDemo.exe.manifest
SimpleTabbedMDIDemo.ico
SimpleTabbedMDIDemodoc.ico
toolbar.bmp
SimpleTabbedMDIDemo.dsp
SimpleTabbedMDIDemo.dsw
tabbingframework_history.zip
tabbingframework_priorhistory.zip
tabbingframework_src.zip
// Copyright (c) 2002
// Peter Krnjevic(peter@dynalinktech.com)
// WTL Docking windows
//
// This code is provided "as is", with absolutely no warranty expressed
// or implied. Any use is at your own risk.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name is included. If
// the source code in  this file is used in any commercial application
// then a simple email woulod be nice.

#pragma once
#ifndef __WTL_DW__DOCKINGFOCUS_H__
#define __WTL_DW__DOCKINGFOCUS_H__

#define DF_FOCUS_FEATURES

#include <map>
#include <queue>

namespace dockwins {

typedef std::map<HWND,HWND> HWNDMAP;

class CDockingFocusHandler : public CMessageMap
{
public:
	CDockingFocusHandler() { This(this); m_hWnd=0; m_hHook=0; }
	HRESULT	InstallHook(HWND hWnd)
	{
		ATLASSERT(0 != hWnd);
		ATLASSERT(0 == m_hWnd);
		m_hWnd = hWnd;
		// make	sure the hook is installed
		if (m_hHook == NULL)
		{
			m_hHook = ::SetWindowsHookEx(WH_CBT, CBTProcStub, _Module.m_hInst, GetCurrentThreadId());
			// is the hook set?
			if (m_hHook == NULL)
			{
				ATLASSERT(false);
				return E_UNEXPECTED;
			}
		}
		return S_OK;
	}

	HRESULT	RemoveHook(HWND /*hWnd*/)
	{
		HRESULT	hr = S_OK;
		if (!::UnhookWindowsHookEx(m_hHook))
			hr = HRESULT_FROM_WIN32(::GetLastError());
		m_hHook = NULL;
		return hr;
	}

	void AddWindow(HWND hwnd)
	{
		ATLASSERT(hwnd);
		if (m_mapHwnd.end() == m_mapHwnd.find(hwnd))
			m_mapHwnd.insert(HWNDMAP::value_type(hwnd,::IsChild(hwnd,GetFocus())?GetFocus():NULL));
	}

    BEGIN_MSG_MAP(thisClass)
		m_hwndMSg = hWnd;
		MESSAGE_HANDLER(WM_MOUSEACTIVATE, OnMouseActivate)
		MESSAGE_HANDLER(WM_ACTIVATE, OnActivate)
    END_MSG_MAP()

	LRESULT OnMouseActivate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
//		bHandled = FALSE;
		SetFocusIfWindowFound(m_hwndMSg);
		return 0;
	}

	LRESULT OnActivate(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
	{
		bHandled = false;
		static HWND hWnd;
		static HWND hWndActive;
		if (::GetActiveWindow() == m_hWnd)
		{
			if (WA_INACTIVE == wParam)
			{
				hWnd = GetFocus();
			}
			else
			{
				if (::IsChild(m_hWnd,hWnd))
				{
					bHandled = true;
					SetFocusEx(hWnd); // ::PostMessage(hWnd,WM_SETFOCUS,0,0);
				}
			}
			InvalidateCaption(hWnd);
		}
		return 0;
	}

	// ugly, but effective.
	static CDockingFocusHandler* This(CDockingFocusHandler* t=0)
	{
		static CDockingFocusHandler* pThis;
		if (t)
		{
			ATLASSERT(0 == pThis);
			pThis=t;
		}
		ATLASSERT(0 != pThis);
		return pThis;
	}

	static HWND SetFocusEx(HWND hwnd)
	{
		CWindow dad(GetParent(hwnd));
		if (dad.IsWindow())
		{
			TCHAR sz[100];
			int n = GetClassName(dad,sz,sizeof(sz)/sizeof(sz[0]));
			if (n && 0 == _tcscmp(_T("#32770"),sz))
			{
				HWND hwndFocus = GetFocus();
				dad.GotoDlgCtrl(hwnd);
				return hwndFocus;
			}
		}
		return ::SetFocus(hwnd);
	}

private:
	static LRESULT CALLBACK CBTProcStub(int nCode, WPARAM wParam, LPARAM lParam)
	{
		ATLASSERT(This());
		return This()->CBTProc(nCode,wParam,lParam);
	}

	LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
	{
		LPMSG lpMsg	= (LPMSG) lParam;
		lpMsg;  // avoid warning on level 4
		if (nCode == HCBT_SETFOCUS)
		{
			InvalidateCaption((HWND)wParam);
			InvalidateCaption((HWND)lParam);
			UpdateFocus(GetDockingParent((HWND)wParam),(HWND)wParam);
		}
		else if (nCode == HCBT_DESTROYWND)
		{
			HWNDMAP::iterator i =  m_mapHwnd.find((HWND)wParam);
			if (m_mapHwnd.end() != i)
				m_mapHwnd.erase(i);
		}
		// Pass to the next hook in the chain.
		return ::CallNextHookEx(m_hHook, nCode, wParam, lParam);
	}

	void InvalidateCaption(HWND hWnd)
	{
		CWindow wnd(GetDockingParent(hWnd));
		if (!wnd.IsWindow())
			return;
		m_qHwndCaption.push(wnd);
		// since the focus has yet to be set, we need to wait a bit before redrawing the caption.
		SetTimer(NULL,0,1,TimerProc);
	}

	static VOID CALLBACK TimerProc(HWND hWnd, UINT /*uMsg*/, UINT_PTR idEvent, DWORD /*dwTime*/)
	{
		KillTimer(hWnd,idEvent);
		ATLASSERT(This());
		ATLASSERT(!This()->m_qHwndCaption.empty());
		CWindow w(This()->m_qHwndCaption.front());
		if (w.IsWindow())
			w.RedrawWindow(NULL,NULL,RDW_FRAME|RDW_INVALIDATE);
		This()->m_qHwndCaption.pop();
	}

	static HWND GetDockingParent(HWND hWnd)
	{
		static ATOM aClass;

		if (!aClass)
		{
			WNDCLASSEX wc;
			wc.cbSize = sizeof(wc);
			aClass = (ATOM)::GetClassInfoEx(_Module.GetModuleInstance(),_T("CPackageWindowFrame::CPackageWindow"),&wc);
		}
		// looks for parent window 2 levels down from m_hWnd, which
		// was initialized in InitializeDockingFrame.
		//	ATLASSERT(aClass);
		if (0 == aClass)
			return NULL;
		ATLASSERT(This());
		while (NULL != hWnd)
		{
			HWND h = ::GetParent(hWnd);
			if (!h)
				return NULL;
			if (::GetClassLongPtr(h,GCW_ATOM) == aClass)
				return hWnd;
			hWnd = h;
		}
		return hWnd;
	}

	bool SetFocusIfWindowFound(HWND hwnd)
	{
		HWNDMAP::iterator i =  m_mapHwnd.find(hwnd);
		if (m_mapHwnd.end() == i)
			return false;
		if (i->second)
		{
			// ensure setting focus won't change activation.
			if (::IsChild(hwnd,i->second))
				SetFocusEx(i->second);
		}
		else
			SetFocusEx(i->first);
		return true;
	}

	void UpdateFocus(HWND hwndDocking, HWND hwndFocus)
	{
		if (!hwndDocking)
			return;
		HWNDMAP::iterator i = m_mapHwnd.find(hwndDocking);
		if (m_mapHwnd.end() != i && ::IsChild(hwndDocking,hwndFocus))
			i->second = hwndFocus;
	}
private:
	HHOOK m_hHook;
	HWND m_hWnd;
	HWND m_hwndMSg;
	std::queue<HWND> m_qHwndCaption;	// Invalid caption queue
	HWNDMAP m_mapHwnd;			// Windows that have received focus
};


class CCaptionFocus
{
public:
	CCaptionFocus(HDC hdc) : m_hWnd(::WindowFromDC(hdc)), m_DockingFocusHandler(*CDockingFocusHandler::This()) { }
	HBRUSH GetCaptionBgBrush()
	{
		m_DockingFocusHandler.AddWindow(m_hWnd);
		return GetSysColorBrush(HasFocus() ? COLOR_ACTIVECAPTION : COLOR_3DFACE);
	}
	DWORD GetCaptionTextColor() const { return GetSysColor(HasFocus() ? COLOR_CAPTIONTEXT : COLOR_BTNTEXT); }
//private:
	bool HasFocus() const { return 0!= CWindow(m_hWnd).IsChild(GetFocus()); }
private:
	HWND m_hWnd;
	CDockingFocusHandler& m_DockingFocusHandler;
};



} // namespace dockwins
#endif // __WTL_DW__DOCKINGFOCUS_H__

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

Share

About the Author

Daniel Bowen
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.

| Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 14 Jul 2005
Article Copyright 2002 by Daniel Bowen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid