Automatically arrange visible controls below the tab control

Here is some code for realigning the controls present on a property sheet. The code realigns the controls that are beneath the tab control, which have the WS_VISIBLE style. The controls are repositioned from right to left, in reverse tab order, and being separated by a standard distance. This is handy for realigning the controls after you have changed the visibile state of the buttons at the bottom of the property sheet. You can also use this function during OnInitDialog(..) as the visible state of the controls is obtained by checking the window style of the child control, and not by using IsWindowVisible(..).

void CPropertySheetDerivedClass::AutoArrangeButtons() 
{
	CWnd* pWnd = GetDlgItem(ID_APPLY_NOW);
	
	if (pWnd && ::IsWindow(pWnd->m_hWnd))
	{
		// Get the last control in the tab order - (tab order is the same as creation order)
		pWnd = pWnd->GetWindow(GW_HWNDLAST);

		// Get the tab control
		CTabCtrl* pTabCtrl = GetTabControl();

		// Get the child identifier of the tab control
		int nTabControlID = ::GetWindowLong(pTabCtrl->m_hWnd, GWL_ID);

		// Child control identifier
		int nControlID = 0;

		// The window rect of the tab control.
		CRect rcTAB(0, 0, 0, 0);

		pTabCtrl->GetWindowRect(rcTAB);

		ScreenToClient(rcTAB);

		CRect rcTemp(0, 0, 0, 0);

		GetClientRect(rcTemp);

		int nLeft = rcTemp.right;
		int nSpacing = rcTemp.right - rcTAB.right;

		// do this bit while we have a control to do something with
		while(pWnd)
		{
			// Get the ID of the current child control
			nControlID = ::GetWindowLong(pWnd->m_hWnd, GWL_ID);
			
			// Do not make changes to the tab control!!
			if (nControlID != nTabControlID)
			{
				// do some stuff
				pWnd->GetWindowRect(rcTemp);

				ScreenToClient(rcTemp);

				// Is the child control visible? - Don't use IsWindowVisible as the parent
				// also has to be visible for that to return TRUE;
				BOOL bChildIsVisible = ((pWnd->GetStyle() & WS_VISIBLE) == WS_VISIBLE);

				// Make sure we are below the TAB control
				if ((rcTemp.top > rcTAB.bottom) && bChildIsVisible)
				{
					// Move each control
					nLeft -= nSpacing + rcTemp.Width();
					
					pWnd->MoveWindow(nLeft, rcTemp.top, rcTemp.Width(), rcTemp.Height());
				}
			}
				
			// Go back in the TAB order again
			pWnd = pWnd->GetWindow(GW_HWNDPREV);
		}
	}
}

Last updated: 29 April 1998



Comments

  • CTabCtrl Style

    Posted by Legacy on 01/21/2002 12:00am

    Originally posted by: LDS

    Hello 
    
    In CPropertySheet,I Want to Modify the backcolor of TabCtrl
    I use
    CTabCtrl* Tab_ChildCtrl=this->GetTabControl();
    but How Can i Modify it's backcolor????
    Thank You!!!

    • ok

      Posted by huangyongtao on 12/04/2008 02:41am

      ok

      Reply
    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: March 27, 2014 Teams need to deliver quality software faster and need integrated agile planning, task tracking, source control, auto deploy with continuous builds and a configurable process to adapt to the way you work. Rational Team Concert and DevOps Services (JazzHub) have everything you need to build great software, integrated seamlessly together right out of the box or available immediately in the cloud. And with the Rational Team Concert Client, you can connect your …

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds