Personalizing highlight colors through custom draw

The listview control enables a coder to set its normal background color and normal foreground color, but uses the system colors when highlighting. These system colors are only settable through the display properties dialog (yuk!) Ownerdrawing items will do the trick, but requires that the entire row be drawn by the user. Using the following method through custom draw, no additional drawing code need be implemented.

In a desperate attempt at avoiding ownerdraw, I initially tried row bitmap manipulation through device contexts at certain stages of the custom draw process, but this was WAY TOO SLOW. The following steps are simple and FAST! The process is as follows:

  • 1. Intercept the listview draw routine just before it is about to draw a highlighted row (item).
  • 2. Turn off the row highlight.
  • 3. Set the row colors to whatever you want.
  • 4. Let the listview draw the row.
  • 5. Intercept the listview draw routine after it has drawn the row (post-draw item).
  • 6. Turn this row's highlighting back on.
  • This still enables the row related processing, such as single-click highlighting, to be handled by the listview control.

    Code snippets: The working version of this was originally based on CListView, I removed references to it to generalize the code outside of MFC, so you'll have to forgive typos:

    
    COLORREF g_MyClrFgHi; // My foreground hilite color
    COLORREF g_MyClrBgHi; // My background hilite color
    
    HWND     g_hListView; // Window handle of listview control
    
    
    void  EnableHighlighting(HWND hWnd, int row, bool bHighlight)
    {
      ListView_SetItemState(hWnd, row, bHighlight? 0xff: 0, LVIS_SELECTED);
    }
    
    bool  IsRowSelected(HWND hWnd, int row)
    {
      return ListView_GetItemState(hWnd, row, LVIS_SELECTED) != 0;
    }
    
    
    bool  IsRowHighlighted(HWND hWnd, int row)
    {
      // We check if row is selected.
      // We also check if window has focus. This was because the original listview
      //  control I created did not have style LVS_SHOWSELALWAYS. So if the listview
      //  does not have focus, then there is no highlighting.
    
      return IsRowSelected(hWnd, row) && (::GetFocus(hWnd) == hWnd);
    }
    
    
    BOOL OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
    {
      static bool bIsHighlighted = false;
    
      *pResult = 0;
    
      NMHDR *p = (NMHDR *)lParam;
    
      switch (p->code)
      { 
      ... 
      case NM_CUSTOMDRAW:
    
        NMLVCUSTOMDRAW *lvcd = (NMLVCUSTOMDRAW *)p;
        NMCUSTOMDRAW   &nmcd = lvcd->nmcd;
    
        switch (nmcd.dwDrawStage)
        {
        case CDDS_PREPAINT:
    
          // We want item prepaint notifications, so...
          *pResult = CDRF_NOTIFYITEMDRAW;
          break;
    
        case CDDS_ITEMPREPAINT:
        {
          int iRow = (int)nmcd.dwItemSpec;
    
          bHighlighted = IsRowHighlighted(g_hListView, iRow);
          if (bHighlighted)
          {
            lvcd->clrText   = g_MyClrFgHi; // Use my foreground hilite color
            lvcd->clrTextBk = g_MyClrBgHi; // Use my background hilite color
    
            // Turn off listview highlight otherwise it uses the system colors!
            EnableHighlighting(g_hListView, iRow, false);
          }
    
          // We want item post-paint notifications, so...
          *pResult = CDRF_DODEFAULT | CDRF_NOTIFYPOSTPAINT;
          break;
        }
    
        case CDDS_ITEMPOSTPAINT:
        {
          if (bHighlighted)
          {
            int  iRow = (int)nmcd.dwItemSpec;
    
            // Turn listview control's highlighting back on now that we have
            // drawn the row in the colors we want.
            EnableHighlighting(g_hListView, iRow, true);
          }
    
          *pResult = CDRF_DODEFAULT;
          break;
        }
    
        default:
          *pResult = CDRF_DODEFAULT;
          break;
        }
        break;
      ...
      }
    
    }
    


    Comments

    • There are no comments yet. Be the first to comment!

    Leave a Comment
    • Your email address will not be published. All fields are required.

    Top White Papers and Webcasts

    • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

    • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

    Most Popular Programming Stories

    More for Developers

    Latest Developer Headlines

    RSS Feeds