ListView einzelne Items andersfarbig darstellen ...



  • moin meisters ...

    ich habe einen Dialog mit einer ListView im ResourcenEditor erstellt.
    Ich habe 3 Einträge in der LV stehen.

    Jetzt möchte ich jeden 2. Eintrag rot schreiben, sonst schwarz.

    In der DialogProg ist folgendes zu finden:

    case WM_NOTIFY:
    if( wParam == IDC_LIST1 )
    {
        nRet = DoNotify(hDlg, message, wParam, lParam);
        return nRet ;
    }
    

    Aus der MSDN habe ich folgenden Code verwendet

    LRESULT DoNotify(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
       LPNMLISTVIEW  pnm    = (LPNMLISTVIEW)lParam;
    
       switch (pnm->hdr.code)
       {
    	case NM_CUSTOMDRAW:
    		{
             LPNMLVCUSTOMDRAW  lplvcd = (LPNMLVCUSTOMDRAW)lParam;
    
    		 /*
             CDDS_PREPAINT is at the beginning of the paint cycle. You 
             implement custom draw by returning the proper value. In this 
             case, we're requesting item-specific notifications.
             */
    
    		 if(lplvcd->nmcd.dwDrawStage == CDDS_PREPAINT)
                // Request prepaint notifications for each item.
                return CDRF_NOTIFYITEMDRAW;
    
             /*
             Because we returned CDRF_NOTIFYITEMDRAW in response to
             CDDS_PREPAINT, CDDS_ITEMPREPAINT is sent when the control is
             about to paint an item.
             */
             if(lplvcd->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)
    		 {
                /*
                To change the font, select the desired font into the 
                provided HDC. We're changing the font for every third item
                in the control, starting with item zero.
                */
                if(lplvcd->nmcd.dwItemSpec % 2)
                   return(CDRF_DODEFAULT);
    
                /*
                To change the text and background colors in a list view 
                control, set the clrText and clrTextBk members of the 
                NMLVCUSTOMDRAW structure to the desired color.
    
                This differs from most other controls that support 
                CustomDraw. To change the text and background colors for 
                the others, call SetTextColor and SetBkColor on the provided HDC.
                */
                lplvcd->clrText = RGB(255, 0, 0);
                lplvcd->clrTextBk = RGB(255,255,255);
    
                /*
                We changed the font, so we're returning CDRF_NEWFONT. This
                tells the control to recalculate the extent of the text.
                */
                return CDRF_NEWFONT;
    		 }
    		}
          default:
             break;
       }
    
       return 0;
    }
    

    Das Problem ist, daß dwDrawStage zwar irgendwann mal CDDS_PREPAINT ist, aber
    niemals CDDS_ITEMPREPAINT !!!

    Habe ich im Debugger mir angesehen, ich verwende Win98 und habe die CommCtrl Version 4.7 es sollte eigentlich funktionieren.

    Es passiert aber garnix, alle Einträge sind schwarz.

    Bitte um Hilfe.
    Besten Dank im Voraus.

    mfg
    RB



  • bin mir nicht mehr sicher aber ich glaube ich hatte ein ähnliches problem und man musste das result mit SetWindowLong an das fenster geben.



  • moin meister ...

    Du meinst sicherlich SetWindowLong mit Index = DWL_MSGRESULT ... ?!

    Also nur welches hwnd ? das vom Dialog oder das der ListView ?

    Ich probiere mal aus ...

    Danke schon mal
    mfg
    RB

    PS: woher soll ich das denn wissen, in der Doku habe ich im Umgang mit WM_NOTIFY sowas nicht gefunden 😞

    Ich vermute mal das ohne Dialog sondern mit normal WinFenster das Problem
    vielleicht nicht auftritt ???



  • moin meister ... 👍

    ich schulde Dir nen Kasten Bier 🤡

    [cpp]
    case WM_NOTIFY:
    if( wParam == IDC_LIST1 )
    {
    nRet = DoNotify(hDlg, message, wParam, lParam);
    **
    SetWindowLong(hDlg, DWL_MSGRESULT, nRet);
    **
    return TRUE ;
    }
    [/cpp]

    Das war es, ich frage mich nur wie ich mir das alles merken soll.
    Api gut und schön aber auf meiner Platte sind schon mehr Testprojekte als denn
    nützliche Creationen ...

    Vielen Dank für die schnelle und nützliche Hilfe

    mfg
    RB
    🙂



  • Ist doch auch irgendwo nur logisch, oder?


Anmelden zum Antworten