Carl Mark
Carl Mark

Reputation: 371

c++ winapi, get listview header text

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?

Upvotes: 1

Views: 2338

Answers (1)

David Heffernan
David Heffernan

Reputation: 612894

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
}

Upvotes: 5

Related Questions