Solved

MFC: Invisible Scroll

Posted on 2011-02-18
7
428 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 34

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:DeepuAbrahamK
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
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!

 

Author Comment

by:tantormedia
ID: 34955426
DeepuAbrahamK,

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

Accepted Solution

by:
Zoppo earned 500 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

Industry Leaders: 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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

734 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