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

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds