OWL NExt - Knowledge Base

[ Home | Contents | Search | Next | Previous | Up ]

Text Colour in a TreeList?

Date: 12 May 2000
Time: 10:13:37
Remote Name: 199.203.141.97

Comments

Q. Does anyone know how to set the text colour for items in a TTreeList control. I have used EvCtlColor in the parent and can set the background colour using SetBkColor, but this has no effect on the text background colour and using SetTextColor or SetBkMode seem to have no effect either.

A. I overrode EvPaint (I think I stole most part of this code from 
somewhere...).

Example:

void MyTreeWindow::EvPaint()
{
  TPaintDC  dc (*this);
  TMemoryDC               memDC(dc);
  TRect                   rcClip, rcClient;
  uint32          data;
  
  dc.GetClipBox (rcClip);
  GetClientRect (rcClient);

  // Select a compatible bitmap into the memory DC
  TBitmap bitmap(dc, rcClient.Width(), rcClient.Height());
  memDC.SelectObject(bitmap);

  // Set clip region to be same as that in paint DC
  TRegion rgn(rcClip);
  memDC.SelectClipRgn(rgn);

  // First let the control do its default drawing.
  TWindow::DefWindowProc(WM_PAINT, (WPARAM)(HDC)memDC, 0);

  TTreeNode presentNode = GetFirstVisible();
  int n = GetVisibleCount()+1;
  while(presentNode && n--){
    TRect rect;
    // Do not meddle with selected items or drop highlighted items
    UINT selflag = TVIS_DROPHILITED | TVIS_SELECTED, temp;

    presentNode.GetState(temp);
    if (!(temp & selflag )){
      TFont *fontDC;
      LOGFONT logfont;

      TFont pFont(GetWindowFont());
      pFont.GetObject (logfont);
      presentNode.GetItemData(data);

      fontDC = new TFont(&logfont);
      memDC.SelectObject(*fontDC);

      memDC.SetTextColor(TColor (data & ColorRefMask));
      string sItem = presentNode.GetText();

      presentNode.GetItemRect(rect, TRUE );

      memDC.SetBkColor( GetSysColor( COLOR_WINDOW ) );
      memDC.TextOut( rect.left+2, rect.top+1, sItem.c_str() );
      memDC.SelectObject( *fontDC);
      delete fontDC;
    }
    presentNode = presentNode.GetNextVisible();
  }
  dc.BitBlt(rcClip.left, rcClip.top, rcClip.Width(), rcClip.Height(), 
            memDC, rcClip.left, rcClip.top, SRCCOPY );
}

Ulf Nilsson (QRA)

Last changed: July 14, 2001