1

I created a listview and after that I would like to get the header text, something like this:

HWND hwndHD = ListView_GetHeader(hListView);
HDITEM hdi;
Header_GetItem(hwndHD, 2, (LPHDITEMA) &hdi);
unsigned char HDtext[lMAX];
hdi.pszText = (LPSTR)HDtext;
SendMessage(hListView, HDM_GETITEM, (WPARAM) 0, (LPARAM) &hdi);
std::string str(HDtext, HDtext + sizeof(HDtext));
MessageBox(hwnd, str.c_str() , "CreateFile", MB_OK);

But it didn't work, what am I do wrong?

4

1 回答 1

5

You have to initialise the HDITEM parameter before you call the Header_GetItem. You must specify in the mask which information you are requesting.

In your case you want to do it like this:

char HDtext[lMAX];
HWND hwndHD = ListView_GetHeader(hListView);
HDITEM hdi = { 0 };
hdi.mask = HDI_TEXT;
hdi.pszText = HDtext;
hdi.cchTextMax = lMAX;
Header_GetItem(hwndHD, 2, &hdi);

You have also completely neglected to include error checking in your code. You should add it.

You need to check the return value of every API call. Consult the documentation on MSDN to know how to interpret it.

Using the code above as an example, you would write:

HWND hwndHD = ListView_GetHeader(hListView);
if (hwndHD == NULL) {
    // handle error
}
....
if (!Header_GetItem(hwndHD, 2, &hdi)) {
    // handle error
}
于 2013-03-30T20:08:45.570 回答