[win32api] List View control help

I'm trying to create this List View control, but somehow I can't load any column to it, here's the code and below is the .rc line for listview, any suggestions? also those string columns are gonna be changed dynamically depending on the file (it will load them from file), this is just for testing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
	case WM_CREATE:
		{
			LVCOLUMN lvc;
			string *sColumn = new string[COLUMN_MAX];
			sColumn[0] = "col1";
			sColumn[1] = "col2";
			sColumn[2] = "col3";

			HWND hTemp = GetDlgItem(hWnd, IDC_LIST);

			lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;

			for(int i = 0; i < COLUMN_MAX; i++)
			{
				lvc.iSubItem = i;
				lvc.pszText = (char*)sColumn[i].c_str();
				lvc.cx = 60; 
				lvc.fmt = LVCFMT_CENTER;

				ListView_InsertColumn(hTemp, i, &lvc);
			}
		}
		break;


CONTROL "", IDC_LIST, WC_LISTVIEW, WS_TABSTOP | WS_BORDER | LVS_REPORT | LVS_EDITLABELS, 5, 10, 180, 150
Last edited on
Your code looks ok to me.

You have called InitCommonControlsEx with ICC_LISTVIEW_CLASSES ??

And the WM_CREATE handler is being triggered? From the fragment of dialog template, I take it you're using a dialog; so I'd normally expect to see WM_INITDIALOG.

Andy

PS I assume new string is temp code...
it's the main dialog window, created using WNDCLASSEX and CreateDialog(), also I used InitCommonControlsEx with ICC_LISTVIEW_CLASSES like this

1
2
3
4
INITCOMMONCONTROLSEX icex;
			icex.dwICC = ICC_LISTVIEW_CLASSES;
			icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
			InitCommonControlsEx(&icex);


I added this to the code after browsing a internet a little bit, but it doesnt help at all, and yes, new string is temp code, I will be getting those columns from CSV file

whole code

http://pastebin.com/PyfgPCYZ

also using the "debug point" (or however is that called) tells me that it IS calling the function ListView_InsertColumn...

edit:

also with
1
2
3
				int abc = ListView_InsertColumn(hTemp, i, &lvc);
				if(abc != -1)
					MessageBox(hWnd, NULL, NULL, NULL);


Im getting MessageBoxes, therefore function works fine
Last edited on
What is the value you get for the handle to the List View control? I get NULL if I try to get it with GetDlgItem in WM_CREATE. But if I use PostMessage to send myself a custom message in WM_CREATE and then run the same code there, it's fine.

It think the problem is that the dialog template processing hasn't been done when WM_CREATE is called, so the dialog's child controls don't yet exist; they get created between WM_CREATE and WM_INITDIALOG.

But because you're using a a custom WndProc for your dialog, it doesn't appear to get sent WM_INITDIALOG like a normal dialog; so you have to send yourself a custom message instead.

Andy
oh yeah, I have this basic "template" for all my win32 apps and there IS PostMessage that posts MY_WM_INITDIALOG, that YOU did approx 1 year ago when I was starting with GUI apps, (when you described how to end dialogbox/createdialog and stuff), but few weeks ago I totally forgot about this (that I need my own init message for main window as dialog) and I just deleted it, since it was kinda weird, having PostMessage that does everything I want to do on create, thanks! it works now :)

edit: actually, I still need some hlep with listview,

I have this code right here

lvc.cx = 60;

and my listview has width 180, therefore it should be enough for 3 columns, also my columns are supposed to have text centered

lvc.fmt = LVCFMT_CENTER;

HOWEVER with 3 columns I still have a LOT of space left, AND the first column is not centered, any suggestion?

screen: http://filebeam.com/dd3d0dff3be6de9c1816e58fb75c07c6.jpg
Last edited on
There is a gottcha with the first column.

For some reason, the alignment value is ignored for the first column. The remarks from the MSDN entry for LVCOLUMN suggest you add a dummy first column, add the columns you want (with the required alignments), and then delete the dummy first column.

If you still have too much space, maybe you need to make the list narrower and use the extra dialog space for something else? Poss. even an image??

Andy

LVCOLUMN structure
http://msdn.microsoft.com/en-us/library/windows/desktop/bb774743%28v=vs.85%29.aspx

If a column is added to a list-view control with index 0 (the leftmost column), it is always LVCFMT_LEFT. Setting other flags on column 0 does not override that alignment. Therefore if you keep inserting columns with index 0, the text in all columns are left-aligned. If you want the first column to be right-aligned or centered you can make a dummy column, then insert one or more columns with index 1 or higher and specify the alignment you require. Finally delete the dummy column.
Last edited on
1. is there a NEED to do a dummy column? since it's working just fine when I start with 1, except 0, ANYWAY, I just tried to actually do 1 for everything (iSubItem = 1, insert column second parameter is 1 too) and the listview looks just like every other (with changing isubitem), what is the difference? why do I need to set different iSubItem values ?

1
2
3
4
5
6
7
			for(int i = 0; i < COLUMN_MAX; i++)
			{
				lvc.iSubItem = 1;
				lvc.pszText = (char*)sColumn[i].c_str();

				ListView_InsertColumn(hTemp, 1, &lvc);
			}


2. also it's not that there's too much space, it's that it's width is 180, while width of one column is supposed to be 60, I have 3 columns, therefore 3x60 = 180, so it should fill whole listview, but it's not, why is that?

3. also will listview take care of sorting automatically when I click on one of those columns (just like in explorer) or do I have to program it too?
Last edited on
1. is there a NEED to do a dummy column?, ... what is the difference? why do I need to set different iSubItem values ?

No idea; I've never hit the problem myself, as I always use left aligment for the first columm. I was just aware of the gottcha.

2. ... it should fill whole listview, but it's not, why is that?

The 180 and 60 are in different units. 3 x 60 pixels is not the same as 180 dialog units. You can either convert the dialog units into pixels...

How to calculate dialog box units based on the current font in Visual C++
http://support.microsoft.com/kb/145994

... or use GetClientRect to find out how big the control is (though you might have to adjust for the inner edges of the control.)

3. also will listview take care of sorting automatically when I click on one of those columns (just like in explorer) or do I have to program it too?

You'll need to handle it. See:

LVM_SORTITEMS message
http://msdn.microsoft.com/en-us/library/windows/desktop/bb761227%28v=vs.85%29.aspx

and

HDM_SETITEM message
http://msdn.microsoft.com/en-us/library/windows/desktop/bb775367%28v=vs.85%29.aspx

HDITEM structure
http://msdn.microsoft.com/en-us/library/windows/desktop/bb775247%28v=vs.85%29.aspx

(with HDF_SORTDOWN or HDF_SORTUP) to set arrow

Andy

Last edited on
Topic archived. No new replies allowed.