OWL NExt - Knowledge Base

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

Working source of TListWindow::EvDrawItem

Date: 11/30/99
Time: 1:01:20 AM


he following subroutine displays 6 columns of information in a listview: a
checkbox (bitmap) + label, a colored rectangle, a line style, a line
thickness, a symbol (bitmap), and a symbol spacing. It also paints the
background blue, turns the text white, and inverts the bitmaps if the line
is selected.

Also, in Resource Workshop, remember to set the owner drawn flag when you
place the list view in the dialog box, otherwise this function will not be
called.

void TEditLinesView::EvDrawItem(uint /*ctrlId*/, DRAWITEMSTRUCT far& drawInfo)
{
  const int MESSSIZE = 256;
  char mess[MESSSIZE];
  int ttop;

  TDC dc( drawInfo.hDC );

  dc.SetBkColor( drawInfo.itemState & ODS_SELECTED ? TColor::SysHighlight : TColor::SysWindow );
  dc.SetTextColor( drawInfo.itemState & ODS_SELECTED ? TColor::SysHighlightText : TColor::Black );
  TBrush bkBrush(drawInfo.itemState & ODS_SELECTED ? TColor::SysHighlight : TColor::SysWindow );
  dc.SelectObject( bkBrush );
  TPen wPen( drawInfo.itemState & ODS_SELECTED ?
  TColor::SysHighlight : TColor::SysWindow );
  dc.SelectObject( wPen );
  dc.Rectangle( drawInfo.rcItem );
  dc.RestorePen();

  TListWindItem item;

  // *** draw subimage and label
  if ( GetItem( item, (int)drawInfo.itemID, 0 ) ){
    int w, h;
    TSize ts;
    // grab and draw the Icon
    HIMAGELIST SIL = GetImageList( Small );
    ImageList_GetIconSize( SIL, &w, &h );
    int buff = ( (drawInfo.rcItem.bottom - drawInfo.rcItem.top) - h ) / 2;
    ImageList_Draw( SIL, item.GetImageIndex(), dc.GetHDC(),
    drawInfo.rcItem.left + buff, drawInfo.rcItem.top + buff, ILD_NORMAL );
    // get the display rectangle for the first column and remove the Icon area
    TRect r( drawInfo.rcItem );
    r.right = r.left + GetColumnWidth( 0 );
    r.left += w + 2*buff;
    // get the item text and text dimensions
    item.GetText( mess, MESSSIZE );
    dc.GetTextExtent( mess, strlen( mess ), ts );
    ttop = r.top + (r.Height() - ts.cy) / 2;
    // if the text dimensions are > than the window then reduce the text
    if ( ts.cx > r.Width() ){
      strcpy( &mess[strlen(mess)-3], "...\0" );
      dc.GetTextExtent( mess, strlen( mess ), ts );
      while ( ts.cx > r.Width() ){
        strcpy( &mess[strlen(mess)-4], "...\0" );
        dc.GetTextExtent( mess, strlen( mess ), ts );
      }
    }
    // print the remaining text
    dc.TextOut( r.left, ttop, mess );
  }

  // *** draw a ractangle containing the color
  if ( GetItem( item, (int)drawInfo.itemID, 1 ) ){
    // select the appropriate background brush
    dc.RestoreBrush();
    item.GetText( mess, MESSSIZE );
    char *s = mess;
    char *e = strchr( s, '-' );
    *e = 0;
    int r = atoi( s );
    s = e + 1;
    e = strchr( s, '-' );
    *e = 0;
    int g = atoi( s );
    s = e + 1;
    int b = atoi( s );
    TBrush c(TColor(r,g,b));
    dc.SelectObject( c );
    TPen pn( drawInfo.itemState & ODS_SELECTED ? TColor::White :
    TColor(r,g,b));
    dc.SelectObject( pn );
    // create rectangle for displaying color
    TRect p( drawInfo.rcItem );
    p.left += GetColumnWidth( 0 );
    p.right = p.left + GetColumnWidth( 1 ) - 2;
    p.left += 2;
    p.top += 2;
    p.bottom -= 2;
    // do it
    dc.Rectangle( p );
    // replace brush
    dc.RestoreBrush();
    dc.RestorePen();
    dc.SelectObject( bkBrush );
  }

  file://*** display line style
  if ( GetItem( item, (int)drawInfo.itemID, 2 ) ){
    item.GetText( mess, MESSSIZE );
    int l = drawInfo.rcItem.left + GetColumnWidth(0) + GetColumnWidth(1);
    dc.TextOut( l, ttop, mess );
  }

  file://*** display thickness
  if ( GetItem( item, (int)drawInfo.itemID, 3 ) ){
    item.GetText( mess, MESSSIZE );
    int l = drawInfo.rcItem.left + GetColumnWidth(0) + GetColumnWidth(1) + GetColumnWidth(2);
    dc.TextOut( l, ttop, mess );
  }

  file://*** display symbol
  if ( GetItem( item, (int)drawInfo.itemID, 4 ) ){
    item.GetText( mess, MESSSIZE );
    TBitmap *bm = symbols[atoi( mess )];
    TRect fromRect( 0, 0, bm->Width(), bm->Height() );
    TMemoryDC memDC( dc );
    TRect r( drawInfo.rcItem );
    r.left += GetColumnWidth(0) + GetColumnWidth(1) + GetColumnWidth(2) + GetColumnWidth(3) + 1;
    r.right = r.left + bm->Width();
    int delta=(drawInfo.rcItem.bottom - drawInfo.rcItem.top -
    bm->Height() )/2;
    r.top += delta;
    r.bottom = r.top + bm->Height();
    memDC.SelectObject( *bm );
    dc.BitBlt( r, memDC, TPoint(0,0),
    drawInfo.itemState & ODS_SELECTED ? MERGEPAINT : MERGECOPY );
  }

  file://*** display symbol spacing
  if ( GetItem( item, (int)drawInfo.itemID, 5 ) ){
    item.GetText( mess, MESSSIZE );
    int l = drawInfo.rcItem.left + GetColumnWidth(0) + GetColumnWidth(1)
              + GetColumnWidth(2) + GetColumnWidth(3)
              + GetColumnWidth(4);
    dc.TextOut( l, ttop, mess );
  }

  dc.RestoreBrush();
}

--
Regards,
Scott
http://www.gnt.net/~heiman

Last changed: July 14, 2001