Solved

MFC: Invisible Scroll

Posted on 2011-02-18
7
426 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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.

809 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