Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MFC: Invisible Scroll

Posted on 2011-02-18
7
Medium Priority
?
437 Views
Last Modified: 2012-05-11
Dear experts,

I have a ListBox. I would like to show line numbers of all items in it. I am thinking of adding another Listbox containing only numbers and synchronize it with the main ListBox. But is there a way not to show any scroll in the additional ListBox, and still make its contents move synchronously with the main ListBox's scroll? Thanks.
0
Comment
Question by:tantormedia
7 Comments
 
LVL 35

Expert Comment

by:sarabande
ID: 34929068
you can add only the visible lines to second list box. then no vertical scrollbar would appear. the line numbers you can achieve by making the listbox owner drawn, then you can draw two columns like in list control.

Sara
0
 

Author Comment

by:tantormedia
ID: 34951796
Sara, could you please give me an example or a link where I can read how to draw line numbers?
Also, why do I need two columns? I thought I would have a separate listbox for line numbers.
Also, somebody suggested to just use CWnd::ShowScrollBar(), but
m_lstOldData.ShowScrollBar(SB_VERT, FALSE);
didn't change anything, as I still see both scrollbars.
Thanks.
0
 
LVL 11

Expert Comment

by:Deepu Abraham
ID: 34955317

CListCtrl list;
list.InsertColumn(0,"Line number");
list.InsertColumn(1,"Your Data");

While populating the data you could add the line number as well.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tantormedia
ID: 34955426
DeepuAbrahamK,

But I am using CListBox, not CListCtrl...
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 2000 total points
ID: 34958793
@tantormedia: Attached you can find a sample code which implements showing line numbers within the list box with an owner drawn list box.

To use it adapt the code so it fits into yours (best by adding 'DrawItem' and 'OnScroll' via ClassWizard and replacing the created code with those from the two appropriate functions).

Set the list box's properties 'Owner drawn' to 'fixed' and 'Has Strings' to 'True' in the resource editor.

After filling the list box call CListBoxEx::AdjustColumnWidth' to ensure the horizontal extent of the list box is set correctly.


I found it mostly works fine with one except: If windows uses smooth scrolling (i.e. with Windows 7 and activated Aero theme) on horizontal scrolling the line numbers move slightly sideways befor redrawn in the correct place - unfortunateley I didn't find a workaround to solve this yet.

Anyway, maybe you can live with this, then using that method may save you a lot of work regarding the synchronization. So it may be worth a try.

ZOPPO
#define LINE_NUM_BORDER 25


void CListBoxEx::DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct )
{
	// TODO:  Add your code to draw the specified item
	CDC dc;

	dc.Attach( lpDrawItemStruct->hDC );

	int nIndex = lpDrawItemStruct->itemID;
	int nOff = GetScrollPos( SB_HORZ );

	CString strLine;
	GetText( nIndex, strLine );

	CRect rect = lpDrawItemStruct->rcItem;
	CRect rectText = rect;
	rectText.left += LINE_NUM_BORDER;

	// Save these value to restore them when done drawing.
	COLORREF crOldTextColor = dc.GetTextColor();
	COLORREF crOldBkColor = dc.GetBkColor();
	int nOldMode = dc.GetBkMode();

	// If this item is selected, set the background color 
	// and the text color to appropriate values. Also, erase
	// rect by filling it with the background color.
	if ( ( lpDrawItemStruct->itemAction | ODA_SELECT ) && ( lpDrawItemStruct->itemState & ODS_SELECTED ) )
	{
		dc.SetTextColor(::GetSysColor( COLOR_HIGHLIGHTTEXT ) );
		dc.SetBkColor( ::GetSysColor( COLOR_HIGHLIGHT ) );
		dc.FillSolidRect( &rectText, ::GetSysColor( COLOR_HIGHLIGHT ) );
	}
	else
	{
		dc.FillSolidRect( &rectText, crOldBkColor );
	}

	// If this item has the focus, draw a red frame around the
	// item's rect.
	if ( ( lpDrawItemStruct->itemAction | ODA_FOCUS ) && ( lpDrawItemStruct->itemState & ODS_FOCUS ) )
	{
		CBrush br( RGB( 255, 0, 0 ) );
		CRect rectFrame = rect;
		rectFrame.left = nOff + LINE_NUM_BORDER;

		dc.FrameRect( &rectFrame, &br );
	}

	// Draw the text.
	dc.SetBkMode( TRANSPARENT );

	rectText.left += 2;
	dc.DrawText( strLine, &rectText, DT_LEFT|DT_SINGLELINE|DT_VCENTER );

	// Reset the background color and the text color back to their
	// original values.
	dc.SetTextColor(crOldTextColor);

	strLine.Format( "%d", nIndex + 1 );

	rect.left = nOff;
	rect.right = nOff + LINE_NUM_BORDER;

	dc.FillSolidRect( rect, RGB( 224, 224, 224 ) );
	rect.right--;
	dc.DrawEdge( rect, BF_RIGHT, EDGE_RAISED );
	rect.DeflateRect( 1, 0 );
	dc.DrawText( strLine, &rect, DT_LEFT|DT_SINGLELINE|DT_VCENTER );

	// Reset the background color and the text color back to their
	// original values.
	dc.SetBkColor(crOldBkColor);
	dc.SetBkMode( nOldMode );

	dc.Detach();
}

void
CListBoxEx::AdjustColumnWidth()
{
	CClientDC dc( this );
	CFont* pOldFont = dc.SelectObject( GetFont() );

	int nMinWidth = 0;
	CString strLine;

	for ( int n = 0; n < GetCount(); n++ )
	{
		GetText( n, strLine );
		CSize s = dc.GetTextExtent( strLine );

		if ( s.cx > nMinWidth )
		{
			nMinWidth = s.cx;
		}
	}

	dc.SelectObject( pOldFont );

	SetHorizontalExtent( nMinWidth + LINE_NUM_BORDER );
}


void CListBoxEx::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	// TODO: Add your message handler code here and/or call default
	LockWindowUpdate();

	CListBox::OnHScroll(nSBCode, nPos, pScrollBar);

	UnlockWindowUpdate();
}

Open in new window

0
 

Author Closing Comment

by:tantormedia
ID: 34961034
Thank you very much.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 34961941
You're welcome - I hope it's good enough for your needs :o)

Have a nice day,

best regards,

ZOPPO
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question