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


Comments

  • Grid Control in DLL

    Posted by Legacy on 01/12/2000 12:00am

    Originally posted by: Kotesh

    VC++6.0 is giving some problem( not showing the dialog box in which Grid control is a child control ) in showing the grid control which is in a dll ( in a Dialog box say ), when invoked by an exe( which is another MFC Exe application ).

    Reply
  • About you Grid.

    Posted by Legacy on 10/13/1999 12:00am

    Originally posted by: Ed

    Nice work!!! Thanks!

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

Top White Papers and Webcasts

  • Wednesday, September 24, 2014 8:00 AM - 9:00 AM PDT According to a recent Forrester Research report, many companies are choosing low-code platforms over traditional programming platforms, due to the speed with which low-code apps can be assembled and tested. With customer-facing applications on the rise, traditional programming platforms simply can't keep up with the "short schedules and rapid change cycles" required to develop these applications. Check out this upcoming webinar and join Clay Richardson from …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds