Grid Control with Splitters

.

Sample image

Environment: VC6 SP2, NT4 SP5, CE 2.11

The Grid control is the best thing I found at Codeguru. Hats off to all those brains behind it!
I have used the control for a few weeks now and I realised that it would be very handy to be
able to use the grid in several split windows and accomodate synchronized scrolling of all the
views. I have extended the source example that Chris Maunder provides with the article
‘MFC Grid Control (Derived from CWnd)’.


The demo project is an SDI with the view derived from CFormView. The grid control appears on a view as described in Chris Maunder’s article titled ‘Using the grid control in a view’. The class CMyGrid, derived from CGridCtrl
overides the WM_VSCROLL message handler function. The message handler for the WM_VSCROLL message tracks the position of the scroll bar on the grid and notifies the parent frame of the parent view. The parent frame then updates the scroll bar of the remaining views.
The grid is derived from CMyGrid instead of CGridCtrl.

Scrolling the views synchronously is complex when the thumb is dragged to a different position. In that case, the scrolling
occurs only when the SB_ENDSCROLL occurs (when the thumb is released). In all other cases, a call to the OnVScroll is sufficient.


void CMyGrid::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar *pScrollBar)
{
	CGridSplitApp *aP = (CGridSplitApp *)AfxGetApp();
	CGridCtrl::OnVScroll(nSBCode,nPos,pScrollBar);
	CGridSplitView *pView = (CGridSplitView *)GetParent();
	if(pView)
	{
		if(nSBCode == SB_THUMBPOSITION || nSBCode == SB_THUMBTRACK)
			aP->nThumb = 1;
		else if(nSBCode == SB_ENDSCROLL)
		{
			CMainFrame *pFrame = (CMainFrame *)pView->GetParentFrame();
			pFrame->OnSynchScroll(SB_ENDSCROLL, CGridCtrl::GetScrollPos32(SB_VERT,TRUE));
			aP->nThumb = 0;
		}
		else
		{
			CMainFrame *pFrame = (CMainFrame *)pView->GetParentFrame();
			pFrame->OnSynchScroll(nSBCode,nPos);
		}
	}
}

The application variable nThumb keeps track of whether the thumb is being dragged or has been released. It is initialised in the application’s constructor.
It is reset whenever the scrolling ends. The CMainFrame member function OnSynchScroll synchronises the
other views.


void CMainFrame::OnSynchScroll(UINT nSBCode, UINT nPos)
{
    for(int i=0;i<3;i++)
	{
		CGridSplitView *vPane = (CGridSplitView *)m_wndSplitter.GetPane(0,i);
		if(vPane != (CGridSplitView *)GetActiveView())
		vPane->SynchScroll(nSBCode,nPos);
	}
}

The function calls, for each pane in the frame, the CGridSplitView member function SynchScroll.


void CGridSplitView::SynchScroll(UINT nSBCode, UINT nPos)
{
	m_dGrid.SetScrollPos32(nSBCode,nPos);
}

The object m_dGrid is an instance of the class CMyGrid.


BOOL CMyGrid::SetScrollPos32(int nSBCode, int nPos, BOOL bRedraw)
{
	CGridSplitApp *aP = (CGridSplitApp *)AfxGetApp();
	CCellID idTopLeft = GetTopleftNonFixedCell();
	CRect rect;
	GetClientRect(rect);
	if(nSBCode == SB_ENDSCROLL)
	{
		if(aP->nThumb == 1)
		{
			CGridCtrl::SetScrollPos32(SB_VERT, nPos);
			CCellID idNewTopLeft = GetTopleftNonFixedCell();
			if (idNewTopLeft != idTopLeft)
			{
				rect.top = GetFixedRowHeight();
				InvalidateRect(rect);
			}
		}
	}
	else
		CGridCtrl::OnVScroll(nSBCode, nPos,CGridCtrl::GetScrollBarCtrl(SB_VERT));
	return TRUE;
}

The function checks if the scrolling has ended and the last action was a thumbwheel drag, and sets the scroll position. Otherwise
it makes a call to the base class’s OnVScroll. Note that the function is invoked when a WM_VSCROLL message is received by the grid in any one of the views.

Downloads

Download demo project – 259Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read