Ensure (partial) visibility of a column

The following function ensures that a column is at least partially visible. Rex Myer provided a comparable function in his article "Multiline editable subitems", but his code relies on the list having an item with index 0, it steps through all columns to find the left border and finally, if a column overlaps the right border by just one pixel but still has a 200 pixel distance from the left, the column will be moved to the left border. Additionally, the following code provides the bPartialOK-Flag known from CListCtrl::EnsureVisible(). Usage should be pretty much obvious :)

This code was developed with VC++ 5.0 (SP3), Windows NT 4.0 (SP3) and the new COMCTL32.DLL (v4.72)

bool CMyListCtrl::EnsureColumnVisible(int nColumn, bool bPartialOK)
{
	CHeaderCtrl* pHeader = reinterpret_cast(GetDlgItem(0));
	ASSERT(pHeader->IsKindOf(RUNTIME_CLASS(CHeaderCtrl)));

	CRect rcHeader;
	Header_GetItemRect(pHeader->m_hWnd, nColumn, &rcHeader);

	CRect rcClient;
	GetClientRect(&rcClient);

	int nOffset = GetScrollPos(SB_HORZ);

	if(bPartialOK)
	{
		if((rcHeader.left - nOffset < rcClient.right) && (rcHeader.right - nOffset > 0))
		{
			return true;
		}
	}

	int nScrollX = 0;

	if((rcHeader.Width() > rcClient.Width()) || (rcHeader.left - nOffset < 0))
	{
		nScrollX = rcHeader.left - nOffset;
	}
	else if(rcHeader.right - nOffset > rcClient.right)
	{
		nScrollX = rcHeader.right - nOffset - rcClient.right;
	}

	if(nScrollX != 0)
	{
		CSize size(nScrollX, 0);
		Scroll(size);
	}

	return true;
}



Comments

  • Error in compiling.

    Posted by Legacy on 04/23/1999 12:00am

    Originally posted by: Carl Smith

    It looks like on the first line of code:
    
    

    CHeaderCtrl* pHeader = reinterpret_cast(GetDlgItem(0));

    the type-id was left out, reinterpret_cast requires the form:
    reinterpret_cast < type-id > ( expression ).

    The line should read:
    CHeaderCtrl* pHeader = reinterpret_cast<CHeaderCtrl*>(GetDlgItem(0));

    Hope this helps:
    Carl Smith

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

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds