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

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • As mobile devices have pushed their way into the enterprise, they have brought cloud apps along with them. This app explosion means account passwords are multiplying, which exposes corporate data and leads to help desk calls from frustrated users. This paper will discover how IT can improve user productivity, gain visibility and control over SaaS and mobile apps, and stop password sprawl. Download this white paper to learn: How you can leverage your existing AD to manage app access. Key capabilities to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds