Jeg har en list boks i MFC, hvor jeg vil kunne sorter indholdet efter hvilken header man klikker på. det skal være muligt at sorter på bogstaver og tal. Jeg har været igennem mange forskellige eksempler, men har ikke kunne få noget til at virke 100 %
Jeg har prøver eksemplet fra MSDN, men det virker ikke! den laver godt nok en sortering, men den er vilkårlig!
eks. fra MSDN // Sort the item in reverse alphabetical order. static int CALLBACK MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) { // lParamSort contains a pointer to the list view control. // The lParam of an item is just its index. CListCtrl* pListCtrl = (CListCtrl*) lParamSort; CString strItem1 = pListCtrl->GetItemText(lParam1, 0); CString strItem2 = pListCtrl->GetItemText(lParam2, 0);
return strcmp(strItem2, strItem1); }
void snip_CListCtrl_SortItems() { // The pointer to my list view control. extern CListCtrl* pmyListCtrl;
// Sort the list view items using my callback procedure. pmyListCtrl->SortItems(MyCompareProc, (LPARAM) pmyListCtrl); }
Hvis der er nogen der kan hjælpe mig, med en løsning på mit problem, giver jeg 60 point oveni!
Understående artikel findes åbenbart ikke længere online, men jeg har kopieret den fra MSDN Oktober 2000. Den giver et eksempel på hvordan det gøres...
HOWTO: Sort Items in a CListCtrl in Report View
-------------------------------------------------------------------------------- The information in this article applies to:
SUMMARY Not much information has been provided for the process of sorting the items in a ListView control, especially one wrapped by the CListCtrl MFC class. The details are easy to implement and this article attempts to fill the gap in documentation for this useful feature. The example code represents an MFC dialog with a list control set to the LVS_REPORT style.
MORE INFORMATION In order to sort the items in a ListView control, there must be an LVITEM structure associated with the item. MFC provides this for the developer, allowing an item to be inserted with a simple InsertItem(int nItem, LPCTSTR lpszItem) function call that creates the structure with reasonable defaults. Such a buffer from the underlying complexity can sometimes be misleading. However, the LVITEM structure is an important key to manipulating ListView items, including the sorting mechanism.
The lParam element of LVITEM provides necessary information. When the SortItems function of the CListCtrl class is called, it must provide a function pointer to a sort callback function, and an application-defined DWORD value. During the sort, the callback function is repeatedly invoked as two items from the list control are selected for comparison. The parameters it receives are the lParam element from each item's LVITEM structure, and the DWORD value passed by the SortItems call.
The code below represents a simple example of sorting a list of ten U.S. Presidents in a ListView control. The presidents are initially stored in a static multi-dimensional CString array.
The callback sort function may be defined statically as a member of a class or, as here, simply as a global function:
int CALLBACK SortFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);
The lParam element can be anything from simple to highly complex. Frequently, a structure is useful in this context, allowing multiple pieces of data to be referenced. For this example, a structure called ITEMDATA was defined to hold the three elements comprising a given item:
Three columns were inserted for the last name, first name, and term of office. Then, for each of the ten items, a new ITEMDATA structure is allocated and initialized from the CString array. The item is inserted very simply, using only the index and the last name string, then the text is set for the other two columns of the item. Finally, the function SetItemData is called, passing the new ITEMDATA as a parameter. This reinitializes the lParam of the item's LVITEM structure, and prepares the way for the sort.
MFC in Visual C++ 6.0 has a problem with header notifications for the ListView control. Although a handler can be added, in the current version it isn't called. For instance, use Class Wizard or the WizardBar to add a Windows Message Handler. If the ID for the ListView control is highlighted, a number of notification messages are available for selection. To sort the items when the header is clicked for a given column, select the notification HDN_ITEMCLICK. An ON_NOTIFY message map entry is generated, as well as a handler function. For the current example, the entry appears as follows:
The problem here is that the notification doesn't actually originate from the ListView control; instead, the Header control created by the ListView sends the notification. The message map entry listed above does not work. The fix is simple, however, since the Header control always has an ID of 0, the macro can be edited to work correctly:
ON_NOTIFY(HDN_ITEMCLICK, 0, OnItemclickList1)
Then, in the OnItemclickList1 handler, the SortItems call is made:
The notification message header (NMHDR) is actually a ListView notification, NMLISTVIEW, that contains the index to the column that was clicked. In this example, this is represented by iItem. More complex lists might need to reference the iSubItem element of this structure as well. The address of the callback function is passed to SortItems, along with the column number which was clicked.
The SortFunc routine is called repeatedly as pairs of the ListView's items are passed to the function for comparison. The first two parameters are the lParam element of the respective items' LVITEM structure, and the third parameter (application-defined) is the column number provided in the SortItems call.
int CALLBACK SortFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) { int nRetVal;
switch(lParamSort) { case 0: // Last Name nRetVal = strcmp(pData1->pszLastName, pData2->pszLastName); break;
case 1: // First Name nRetVal = strcmp(pData1->pszFirstName, pData2->pszFirstName); break;
case 2: // Term nRetVal = strcmp(pData1->pszTerm, pData2->pszTerm); break;
default: break; }
return nRetVal; }
The column index passed in lParamSort determines which element of the ITEMDATA objects passed in lParam1 and lParam2 should be used for comparison. The result is returned and the process continues until all items have been sorted.
As a reminder, the ITEMDATA structures which were allocated for the list items need to eventually be destroyed. For this example, the WM_DESTROY handler for the dialog iterates through the member elements and deletes them.
for (int i=0; i<10; i++) delete m_pData[i];
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.