Contiuously updating sizing header

If you have ever used a CHeader the first thing you notice that it isn't as nice as the one that appears at the top of list controls. What I mean by this is that when you resize the control
all you get is a resizing line.  When you move this line the header does not update its items
as it moves only at the end when you release the button.  With a CListCtrl the headers continuously update as they move.

This functionality is cool and would be nice to have in a standard CHeader control.
Following is the procedure by which you can add this functionality to your own CHeader derived classes.  All you need to do is override a couple of the mouse procedures and you are in business. An Avi of how it works:

Below are the new procedures and the code for each:
//Header
class CSizeHeader : public CHeaderCtrl
{
	// Construction
	public:
	CSizeHeader();

	// Attributes
	public:

	// Operations
	public:

	// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CSizeHeader)
	//}}AFX_VIRTUAL

	// Implementation
	public:
	CString m_sGrayFont;
	CString m_sSelFont;
	virtual ~CSizeHeader();

	// Generated message map functions
	protected:
	CSize m_minSize;
	CRect m_itemRect;
	CSize m_offset;
	int m_cx;
	int m_twidth;
	int m_hititem;
	CFont m_selFont;
	//{{AFX_MSG(CSizeHeader)
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
	//}}AFX_MSG

	DECLARE_MESSAGE_MAP()
	};

// -------------------------------------------------------------------------------------------------------
//.cpp file
CSizeHeader::CSizeHeader()
{
	m_sSelFont = _T("Helv");
	m_sGrayFont = _T("Helv");
}

CSizeHeader::~CSizeHeader()
{
}
 

BEGIN_MESSAGE_MAP(CSizeHeader, CHeaderCtrl)
//{{AFX_MSG_MAP(CSizeHeader)
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CSizeHeader::OnLButtonDown(UINT nFlags, CPoint point) 
{
	HD_ITEM item;
	char text[255];

	item.pszText = text;
	item.mask = HDI_TEXT | HDI_WIDTH;
	item.cchTextMax = 255;


	m_twidth = 0;
	for(int i = 0; i < GetItemCount(); i++)
	{
		GetItem(i, &item);
		m_twidth += item.cxy;
		if(m_twidth  > point.x - 10 && m_twidth < point.x + 10)
		{
			m_hititem = i;
			m_cx = item.cxy;
			break;
		}
		else
			m_hititem = -1;
//		return;
	}	
	CHeaderCtrl::OnLButtonDown(nFlags, point);
	return;
}

void CSizeHeader::OnMouseMove(UINT nFlags, CPoint point) 
{
	if(m_hititem > -1)
	{
		SetCapture();
		HD_ITEM item;
		char text[255];

		item.pszText = text;
		item.mask = HDI_TEXT | HDI_WIDTH;
		item.cchTextMax = 255;

		GetItem(m_hititem, &item);

		item.cxy = m_cx - (m_twidth - point.x);

		SetItem(m_hititem, &item);
		return;
	}
	CHeaderCtrl::OnMouseMove(nFlags, point);
	return;
}

void CSizeHeader::OnLButtonUp(UINT nFlags, CPoint point) 
{
	if(m_hititem > -1)
	{
		m_hititem = -1;
		ReleaseCapture();
		return;
	}
	CHeaderCtrl::OnLButtonUp(nFlags, point);
	return;
}

BOOL CSizeHeader::OnEraseBkgnd(CDC* pDC) 
{
	HD_ITEM item;
	char text[255];

	item.pszText = text;
	item.mask = HDI_TEXT | HDI_WIDTH;
	item.cchTextMax = 255;

	int twidth = 0;

	for(int i = 0; i < GetItemCount(); i++)
	{
		GetItem(i, &item);
		twidth += item.cxy;
	}

	CRect rect;

	GetClientRect(rect);
	rect.DeflateRect(0, CY_BORDER);
	rect.left = twidth;
	
	CBrush rBrush(GetSysColor(COLOR_3DFACE));
	pDC->FillRect(rect, &rBrush);
	return FALSE;	
}



Comments

  • Some useful undocumented header styles

    Posted by Legacy on 04/14/2000 12:00am

    Originally posted by: Matthew Shovelton

    I've just been playing about with CHeaderCtrls and, through scanning the MFC header files, have discovered 3 undocumented styles (HDS_* codes). One of them produces the behaviour described in the above article. The styles are:

    HDS_FULLDRAG - Dynamically updates the header control as you resize it. It sends HDN_ITEMCHANGING and HDN_ITEMCHANGED notifications as you resize, so trap these instead of HDN_TRACK.

    HDS_DRAGDROP - Allows drag and drop movement of header items

    HDS_HOTTRACK - Highlights header items as you move the cursor over the header.

    Hope you find this useful.


    • Trapping the messages

      Posted by mpirooz on 06/30/2008 03:41pm

      The best way I've discovered to accomplish this is to set the LRESULT* passed into the reflect functions to -1.

      Reply
    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: February 12, 2015 The evolution of systems engineering with the SysML modeling language has resulted in improved requirements specification, better architectural definition, and better hand-off to downstream engineering. Agile methods have proven successful in the software domain, but how can these methods be applied to systems engineering? Check out this webcast and join Bruce Powel Douglass, author of Real-Time Agility, as he discusses how agile methods have had a tremendous …

  • According to a 2014 Javelin Strategy & Research Study, there is a new victim of fraud every two seconds – and that's just in the U.S. With identity theft and consumer fraud rising, and more frequent and sizable data breaches in the news, financial institutions need to work harder than ever to protect their customers and their business. Download this white paper to learn how you can use multi-channel communications to minimize the costs of fraud while also increasing customer loyalty.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date