开发者

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

开发者 https://www.devze.com 2023-02-12 17:41 出处:网络
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.

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:

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

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:

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

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);
             开发者_如何学Go           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.


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;
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号