Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I modify the font colors of individual characters in a ListView subitem?

I was writing an app using the Windows API, and I wanted to know how to make a listview with subitems that contain multi-colored text.

To clarify, here is a picture of how it is implemented in API Monitor:

Notice that in the "API" column, the text has multiple colors, like it is rich text or something. I was wondering how I would do this.

Someone told me to do something with custom-drawing, but he wasn't sure. I looked into it, and I handled NM_CUSTOMDRAW. Here is the result of my test:

And here is the code:

inline LRESULT HandleWM_NOTIFY(LPARAM lParam)
{
   switch (((LPNMHDR)lParam)->code)
   {
      case NM_CUSTOMDRAW:
      {
         switch (((LPNMHDR)lParam)->idFrom)
         {
            case ID_LISTVIEW1:
            {
               LPNMLVCUSTOMDRAW lpNMLVCD = (LPNMLVCUSTOMDRAW)lParam;
               if (lpNMLVCD->nmcd.dwDrawStage == CDDS_PREPAINT)
               {
                  return CDRF_NOTIFYITEMDRAW;
               }
               else if (lpNMLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)
               {
                  COLORREF crText;
                  switch (lpNMLVCD->nmcd.dwItemSpec % 3)
                  {
                     case 0:
                        crText = RGB(255, 0, 0);
                        break;
                     case 1:
                        crText = RGB(0, 255, 0);
                        break;
                     case 2:
                        crText = RGB(0, 0, 255);
                        break;
                  }

                  lpNMLVCD->clrText = crText;
                  lpNMLVCD->
               }

               return CDRF_DODEFAULT;
            }
            default: break;
         }

         break;
      }

      default: break;
   }

   return 0;
}

Using the NM_CUSTOMDRAW method, I can't modify the font colors of individual characters; I can only modify the font color of everything in the subitem, which is not what I want.

How can I achieve what API monitor does? I have a feeling this is going to be very difficult, but any suggestions are welcome.

like image 814
Neal P Avatar asked Feb 20 '11 07:02

Neal P


1 Answers

NM_CUSTOMDRAW is the solution. Sorry to say there's no easy solution here. You simply need to ownerdraw the text you want in different colours sequentially rather than collectively, use the GetTextExtentPoint32 API to assist in the text drawing. You return CDRF_SKIPDEFAULT to tell the listview not to render the text, you took care of it.

if (lpNMHdr->code == NM_CUSTOMDRAW)
{
    LPNMLVCUSTOMDRAW lpCD = (LPNMLVCUSTOMDRAW)lpNMHdr;
    if (lpCD->nmcd.dwDrawStage == CDDS_PREPAINT)
    {
        return CDRF_NOTIFYITEMDRAW;
    }

    if (lpCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)
    {
        return CDRF_NOTIFYSUBITEMDRAW;
    }

    if (lpCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT|CDDS_SUBITEM))
    {
        if (lpCD->iSubItem == 0) //detect which subitem is being drawn
        {
            LPCTSTR lpcszBuf1 = _T("example");
            LPCTSTR lpcszBuf2 = _T("text");

            RECT iR = { 0 };
            ListView_GetSubItemRect(lpCD->nmcd.hdr.hwndFrom, lpCD->nmcd.dwItemSpec, lpCD->iSubItem, LVIR_BOUNDS, &iR);

            SetBkMode(lpCD->nmcd.hdc, TRANSPARENT);

            SIZE sz = { 0 };
            GetTextExtentPoint32(lpCD->nmcd.hdc, lpcszBuf1, 7, &sz);

            SetTextColor(lpCD->nmcd.hdc, RGB(255, 0, 0));                   
            DrawText(lpCD->nmcd.hdc, lpcszBuf1, -1, &iR, DT_LEFT);

            iR.left += sz.cx;

            SetTextColor(lpCD->nmcd.hdc, RGB(0, 255, 0));                   
            DrawText(lpCD->nmcd.hdc, lpcszBuf2, -1, &iR, DT_LEFT);                  

            return CDRF_SKIPDEFAULT;
        }
    }
like image 150
Maurizio Avatar answered Sep 23 '22 00:09

Maurizio