Solved

MFC: Invisible Scroll

Posted on 2011-02-18
7
425 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 33

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 119
Handle Exceptions during instantiation 28 411
computer science syllabus 3 70
What is sub-make ? 2 40
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

867 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

16 Experts available now in Live!

Get 1:1 Help Now