ListView Sortier Problem



  • Hab ein bisschen auf Google.de und msdn.com geguckt und alles moegliche gefunden... Das Problem is ich steig durch keinen Code da.

    Hat wer vielleicht einen leicht zu verstehenden Link zu dem Thema ?



  • Was isn dein Problem?



  • Wie sortiere ich ListView's (Report Modus)



  • PhoeNix_FasT schrieb:

    Wie sortiere ich ListView's (Report Modus)

    Mit der Nachricht LVM_SORTITEMS:

    MSDN schrieb:

    LVM_SORTITEMS

    LVM_SORTITEMS
    wParam = (WPARAM) (LPARAM) lParamSort;
    lParam = (LPARAM) (PFNLVCOMPARE) pfnCompare;

    Uses an application-defined comparison function to sort the items of a list view control. The index of each item changes to reflect the new sequence. You can send this message explicitly or by using the ListView_SortItems macro.

    Returns TRUE if successful, or FALSE otherwise.
    lParamSort
    Application-defined value that is passed to the comparison function.
    pfnCompare
    Address of the application-defined comparison function. The comparison function is called during the sort operation each time the relative order of two list items needs to be compared.
    The comparison function has the following form:

    int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2,
    LPARAM lParamSort);

    The lParam1 parameter is the 32-bit value associated with the first item being compared, and the lParam2 parameter is the value associated with the second item. These are the values that were specified in the lParam member of the items' LVITEM structure when they were inserted into the list. The lParamSort parameter is the same value passed to the LVM_SORTITEMS message.

    The comparison function must return a negative value if the first item should precede the second, a positive value if the first item should follow the second, or zero if the two items are equivalent.



  • Na gut das lass ich dann wohl lieber..



  • PhoeNix_FasT schrieb:

    Na gut das lass ich dann wohl lieber..

    Wieso ? So schwer ist das doch nicht. 👍

    1. Du Definierst eine Vergleichsfunktion:

    Beispiel:

    // Return-Werte:
    #define COMP_EQUAL      0 // Beide Items sind gleich (Item1 = Item2)
    #define COMP_SMALLER   -1 // Item1 (lParam1) soll vor Item2 (lParam2)
    #define COMP_GREATER    1 // Item2 (lParam2) soll vor Item1 (lParam1)
    
    int CALLBACK CompareFunction(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
    {
       if(lParam1 < lParam2)
          return (COMP_SMALLER);
       else if(lParam1 > lParam2)
          return (COMP_GREATER);
       else
          return (COMP_EQUAL);
    }
    

    2. Dann 'startest' du den Vergleich so:
    Beispiel:

    SendMessage(hWndListView, LVM_SORTITEMS, 0, reinterpret_cast<LPARAM>(CompareFunction));
    

    Jetzt sollte ich vllt noch erwähnen, dass lParam1 bzw. lParam2 die 'Zusatz-Daten' eines Items sind.
    In meiner obrigen Beispiel-Vergleichsfunktion gehe ich also davon aus, dass das einfache L-Werte sind,
    keine Speicheradressen. Wenn du deinem Item als Zusatz-Datenstruktur ein Zeiger auf eine Struktur
    übermittelst, um dann beispielsweise ein Element aus der Struktur zu vergleichen, musst du in der
    Vergleichsfunktion (CompareFunction) natürlich entsprechend die Parameter lParam1 und lParam2 casten.
    Dann kannst du auf die Strukturen der Items zugreifen und deren Elemente vergleichen. So lassen sich dann
    beispielsweise Strings vergleichen o.ä.. 😉

    PS_1: Die #defines sind nur wg. der Übersichtlichkeit. 💡
    PS_2: Hab leider keinen fertigen Code, sonst würde ich den posten. 🤡



  • hm.. zu 70 % verstanden.

    Also ich hab das so gemacht : Wenn ich das Programm starte , zeig der mir alle Dateien und Ordner an und die Dateigroesse.

    Das sind dann 2 Items ( LVITEM lvi0 und lvi1 ).

    Wie .. gehts jetzt weiter ?



  • Nach welchen Kriterien soll den sortiert werden ?

    Und:

    Wie hast du die Items hinzugefügt ?
    Sprich: Wie (und überhaupt?!) hast du die lParam's der Items bestückt ?



  • Ich hab das jedesmal so gemacht :

    lvi0.iItem = 0;
    lvi0.mask = LVIF_TEXT;
    lvi0.iSubItem = 0;
    
    ListView_InsertItem(GetDlgItem(phDlg[3],IDC_LIST),&lvi0);
    

    Und mit ListView_SetItemText() mehr eingefuegt .. funktioniert auch.. was meinst du mit lParam ? 😕



  • PhoeNix_FasT schrieb:

    [...]was meinst du mit lParam ? 😕

    U look'in here;

    typedef struct _LVITEM { 
        UINT mask; 
        int iItem; 
        int iSubItem; 
        UINT state; 
        UINT stateMask; 
        LPTSTR pszText; 
        int cchTextMax; 
        int iImage; 
        LPARAM lParam; // /!\ This!
    #if (_WIN32_IE >= 0x0300)
        int iIndent;
    #endif
    #if (_WIN32_WINNT >= 0x560)
        int iGroupId;
        UINT cColumns; // tile view columns
        PUINT puColumns;
    #endif
    #if (_WIN32_WINNT >= 0x0600)
        int* piColFmt
        int iGroup
    #endif
    } LVITEM, *LPLVITEM;
    

    MSDN schrieb:

    [...]

    lParam
    Value specific to the item. If you use the LVM_SORTITEMS message,
    the list-view control passes this value to the application-defined
    comparison function. You can also use the LVM_FINDITEM message to search
    a list-view control for an item with a specified lParam value.

    [...]

    Siehe <hier>


Anmelden zum Antworten