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));

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

	CRect rcClient;

	int nOffset = GetScrollPos(SB_HORZ);

		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);

	return true;


  • 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

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

Top White Papers and Webcasts

  • Enterprises are increasingly looking to platform as a service (PaaS) to lower their costs and speed their time to market for new applications. Developing, deploying, and managing applications in the cloud eliminates the time and expense of managing a physical infrastructure to support them. PaaS offerings must deliver additional long-term benefits, such as a lower total cost of ownership (TCO), rapid scalability, and ease of integration, all while providing robust security and availability. This report …

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date