Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List Control Column Widths

Status
Not open for further replies.

OneSource

Programmer
Jun 18, 2002
22
US
Hi and thanks for reading.

As with most things in VC++ (as it seems to me), something simple takes forever to figure out. Here's the deal. I have a list control with 3 columns. I want to size the columns based on the total width of the list control and not on hard-coded values. Here's my code:
Code:
	CRect		rListViewWidth;
	LV_COLUMN	listColumn;
	int          nColumn = 0;
	char*		arColumns[3] = {"One", "Two", "Three"};	

	// Set up the list control.
	listColumn.mask = LVCF_FMT|LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM;
	listColumn.fmt = LVCFMT_LEFT;
	::SendMessage(m_listCtrl.m_hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
				  LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);

	m_listCtrl.GetWindowRect(&rListViewWidth);
	int nWidth = rListViewWidth.Width();

	// Add the column headers to the list view.
	for( nColumn = 0; nColumn < 3; nColumn++ )
	{
		listColumn.iSubItem = nColumn;
		listColumn.pszText = arColumns[nColumn];

		// Size the columns.
		if(nColumn == 0) // One
			listColumn.cx = (nWidth * 20 / 100);
		else if(nColumn == 1) // Two
			listColumn.cx = (nWidth * 37 / 100);
		else if(nColumn == 2) // Three
			listColumn.cx = (nWidth * 42 / 100);

		m_listCtrl.InsertColumn(nColumn, &listColumn);
	}[code]

I put in the (nWidth * x / 100) to avoid compiler warnings about converting from double to int. Anyway, I know this is simple, but I've already spent too much time trying to figure it out. Can someone tell me how to do this? 

Thanks, 

OneSource
 
After you insert all the columns iterate though them again and for each one use CListCtrl::SetColumnWidth()

-pete
 
Palbano,

Thanks for your response .... it got me closer to a solution because now the columns can be sized independently. But there is still a piece of the puzzle missing. I want to size the columns based on the width of the list control. I don't know how to get the width. Here's how my code looks now:
Code:
	m_listCtrl.GetWindowRect(&rListViewWidth);
	ScreenToClient(&rListViewWidth);
	int nWidth = rListViewWidth.Width();

	// Add the column headers to the list view.
	for( nColumn = 0; nColumn < 3; nColumn++ )
	{
		listColumn.iSubItem = nColumn;
		listColumn.pszText = arColumns[nColumn];
		listColumn.cx = 10;
		m_listCtrl.InsertColumn(nColumn, &listColumn);
	}

	for( nColumn = 0; nColumn < 3; nColumn++ )
	{
		// Size the columns.
		if(nColumn == 0) // One
			m_listCtrl.SetColumnWidth(nColumn, (nWidth * 20 / 100));
		else if(nColumn == 1) // Two
			m_listCtrl.SetColumnWidth(nColumn, (nWidth * 37 / 100));
		else if(nColumn == 2) // Three
			m_listCtrl.SetColumnWidth(nColumn, (nWidth * 42 / 100));
	}
How do I get the width of the list control into nWidth? Example code would be great! [thumbsup]
 
what you have should work fine.

>> m_listCtrl.GetWindowRect(&rListViewWidth);

you don't need screentoclient since you only care about the width that won't change. If you use GetClientRect() there might be a few pixel difference.

Have you looked at the value of nWidth? Does it look correct? If so I believe your problem is in your algebra formula

(nWidth * 42 / 100)

you have not applied the distributive property to the formula. I think you want (nWidth * (42 / 100))

-pete
 
OneSource,
Also keep in mind that your list control may have a scrool bar at the right, which will decrease the width of your control. So a more accurate code to find the width of the list control would be
[tt]
const int maxitems = /* number of items that fit w/o scrolling */

int cx; // width
RECT clientRect;
m_ctlList.GetClientRect(&clientRect);
cx = clientRect.right - clientRect.left;

if (m_ctlList.GetCount() > maxitems) {
cx -= ::GetSystemMetrics(SM_CXVSCROLL);
}
[/tt]
 
palbano and apatterno, thanks for your replies. I was able to get it to work. The problem I was having was due to some font manipulation that I was doing on the property sheet that contained the list control. I tried my code in a project from scratch and everything worked just fine. :)

FYI, the problem wasn't the distributive property formula because the following statements always produce the same results: (nWidth * 42 / 100) and (nWidth * (42 / 100)).

Also, I didn't need to use m_listCtrl.SetColumnWidth because listColumn.cx = 10 did the trick.

I do have another question about all this. I'll ask it in a new post ....

Thanks again, [thumbsup]

OneSource
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top