简体   繁体   中英

What is the difference between LVM_GETITEM and LVM_GETITEMTEXT?

I want to get the text of a ListView row (text of the item and the subitems), but I am not sure if I should use LVM_GETITEM or LVM_GETITEMTEXT .

What is the difference between these two messages, does the first retrieve the entire information about an item or subitem while the other only retrieve the text?

Both LVM_GETITEM and LVM_GETITEMTEXT can be used to retrieve an item's or subitem's text data. Since retrieving an item's text data is a very common operation, the LVM_GETITEMTEXT message is provided as a convenience implementation.

To illustrate the difference, here are two implementations using either message (error handling elided for brevity):

std::wstring GetListViewItemText( HWND a_hWnd, int a_Item, int a_SubItem) {
    std::vector<wchar_t> buffer( 1024 );
    LVITEM lvi = { 0 };
    lvi.mask = LVIF_TEXT;  // Only required when using LVM_GETITEM
    lvi.pszText = buffer.data();
    lvi.cchTextMax = static_cast<int>( buffer.size() );
    lvi.iItem = a_Item;    // Only required when using LVM_GETITEM
    lvi.iSubItem = a_SubItem;

    ::SendMessage( hwnd, LVM_GETITEM, 0, reinterpret_cast<LPARAM>( &lvi ) );

    return std::wstring( lvi.pszText );
}

Slightly shorter, using LVM_GETITEMTEXT :

std::wstring GetListViewItemText( HWND a_hWnd, int a_Item, int a_SubItem) {
    std::vector<wchar_t> buffer( 1024 );
    LVITEM lvi = { 0 };
    lvi.pszText = buffer.data();
    lvi.cchTextMax = static_cast<int>( buffer.size() );
    lvi.iSubItem = a_SubItem;

    ::SendMessage( hwnd, LVM_GETITEMTEXT, a_Item, reinterpret_cast<LPARAM>( &lvi ) );

    return std::wstring( lvi.pszText );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM