Zeile in Listview Markieren



  • Hallo
    Ich habe ein Listview erstellt. Nun möchte ich aber gerne, dass wen ich auf einen Eintrag klicke, das dann die ganze Zeile markiert wird. Kann mir jemand sagen wie ich das verwirklichen kann?

    Mfg Tim

    bool InsertListViewColumn (HWND hListView, 
                               LPTSTR lpColumnName, 
                               int iWidth, 
                               int iSubItem, 
                               DWORD dwStyle = NULL) 
    { 
        int iStatus; 
        LV_COLUMN lvc; 
    
        lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM; 
        lvc.fmt = LVCFMT_LEFT | dwStyle; 
        lvc.cx = iWidth; 
        lvc.pszText = lpColumnName; 
        lvc.cchTextMax = 100; 
        lvc.iSubItem = iSubItem; 
    
        iStatus = ListView_InsertColumn (hListView, iSubItem, &lvc); 
    
        return (iStatus != -1); 
    }
    
    bool InsertListViewEntry (HWND hListView, 
                              LPTSTR lpEntryValue, 
                              int iRowID, 
                              int iColID) 
    { 
        int iStatus; 
        LV_ITEM lvi; 
    
        lvi.mask = LVIF_TEXT; 
        lvi.iItem = iRowID; 
        lvi.iSubItem = iColID; 
        lvi.pszText = lpEntryValue; 
        lvi.cchTextMax = MAX_PATH; 
    
        if (iColID == 0) 
            iStatus = ListView_InsertItem (hListView, &lvi); 
        else 
            iStatus = ListView_SetItem (hListView, &lvi); 
    
        return (iStatus != -1 || iStatus != false); 
    }
    
    ...
    case WM_CREATE:
    		{
    			hListView = CreateWindow (WC_LISTVIEW, L"", 
                                  WS_CHILD | WS_VISIBLE | LVS_REPORT, 
                                  10, 10, 775, 500,
                                      hWnd,
                                      NULL,
                                      ((LPCREATESTRUCT) lParam) -> hInstance,
                                      NULL);
    	InsertListViewColumn (hListView, L"Genere", 150, 0); 
    	InsertListViewColumn (hListView, L"Bezeichnung", 150, 1); 
    	InsertListViewColumn (hListView, L"Farbe", 150, 2); 
    
    	InsertListViewEntry (hListView, L"Auto", 0, 0);
        InsertListViewEntry (hListView, L"A400453", 0, 1); 
    	InsertListViewEntry (hListView, L"ROT", 0, 2); 
    	ListView_SetItemState(hListView,1,LVIS_FOCUSED, LVIS_STATEIMAGEMASK);
    
    			return 0;
    		}
    ...
    


  • Probiers mal so

    SendMessage(hListView, LVM_SETEXTENDEDLISTVIEWSTYLE, (WPARAM)0, (LPARAM)LVS_EX_FULLROWSELECT);
    


  • Perfekt. Danke



  • Hab jetzt aber noch eine Frage:
    Ich hätte gerne, dass man immer nur eine Zeile markieren kann. Ich habs mit LVS_SINGLESEL probiert aber das bewirkt nur, dass ich vor jede Zeile eine Checkbox bekomme aber immer noch mehrere markieren kann.

    Mfg Tim



  • LVS_SINGLESEL ist ein einfacher Fensterstil, Du kannst diesen direkt beim Aufruf der CreateWindow-Funktion festlegen:

    WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SINGLESEL
    

    Bei der Verwendung von LVM_SETEXTENDEDLISTVIEWSTYLE schlägt es fehl, weil LVS_EX_CHECKBOXES den gleichen Wert hat.



  • Ah ok. Da drauf bin ich nicht gekommen. Wieder was dazugelernt..


Anmelden zum Antworten