Solved

MFC: Invisible Scroll

Posted on 2011-02-18
7
424 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 32

Expert Comment

by:sarabande
Comment Utility
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
Comment Utility
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
Comment Utility

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:tantormedia
Comment Utility
DeepuAbrahamK,

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

Accepted Solution

by:
Zoppo earned 500 total points
Comment Utility
@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
Comment Utility
Thank you very much.
0
 
LVL 30

Expert Comment

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

Have a nice day,

best regards,

ZOPPO
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now