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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Information is data with context. The era of Big Data has begun demonstrating to information security that there is more that can, and must, be done to identify threats, reduce risk, address fraud, and improve compliance monitoring activities by bringing better context to data and thereby creating information for actionable intelligence. This analyst report sets the stage and provides insights into IT and information security practitioners' perceptions of the impediments to, and the solutions necessary for, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds