void CTreeListCtrl::OnPaint() { CPaintDC dc(this); // device context for painting CRect rcClip, rcClient; dc.GetClipBox( &rcClip ); GetClientRect(&rcClient); // Set clip region to be same as that in paint DC CRgn rgn; rgn.CreateRectRgnIndirect( &rcClip ); dc.SelectClipRgn(&rgn); rgn.DeleteObject(); COLORREF m_wndColor = GetSysColor( COLOR_WINDOW ); dc.SetViewportOrg(m_nOffset, 0); dc.SetTextColor(m_wndColor); // First let the control do its default drawing. CWnd::DefWindowProc( WM_PAINT, (WPARAM)dc.m_hDC, 0 ); HTREEITEM hItem = GetFirstVisibleItem(); int n = GetVisibleCount(), m_nWidth; dc.FillSolidRect(GetColumnWidth(0),1,rcClient.Width(),rcClient.Height(),m_wndColor); CTreeListItem *pItem; // the most urgent thing is to erase the labels that were drawn by the tree while(hItem!=NULL && n>=0) { CRect rect; UINT selflag = TVIS_DROPHILITED | TVIS_SELECTED; CRect m_labelRect; GetItemRect( hItem, &m_labelRect, TRUE ); GetItemRect( hItem, &rect, FALSE ); if (GetColumnsNum()>1) rect.left = min(m_labelRect.left, GetColumnWidth(0)); else rect.left = m_labelRect.left; rect.right = m_nColumnsWidth; dc.FillSolidRect(rect.left,rect.top,rect.Width(),rect.Height(),m_wndColor); hItem = GetNextVisibleItem( hItem ); n--; } // create the font CFont *pFontDC; CFont fontDC, boldFontDC; LOGFONT logfont; CFont *pFont = GetFont(); pFont->GetLogFont( &logfont ); fontDC.CreateFontIndirect( &logfont ); pFontDC = dc.SelectObject( &fontDC ); logfont.lfWeight = 700; boldFontDC.CreateFontIndirect( &logfont ); // and now let's get to the painting itself hItem = GetFirstVisibleItem(); n = GetVisibleCount(); while (hItem != NULL && n >= 0) { CRect rect; UINT selflag = TVIS_DROPHILITED | TVIS_SELECTED; if ( !(GetItemState( hItem, selflag ) & selflag )) { dc.SetBkMode(TRANSPARENT); pItem = (CTreeListItem *)CTreeCtrl::GetItemData(hItem); CString sItem = ((pItem != NULL) ? pItem->GetItemText() : ""); CRect m_labelRect; GetItemRect( hItem, &m_labelRect, TRUE ); GetItemRect( hItem, &rect, FALSE ); if (GetColumnsNum()>1) rect.left = min(m_labelRect.left, GetColumnWidth(0)); else rect.left = m_labelRect.left; rect.right = m_nColumnsWidth; dc.SetBkColor( m_wndColor ); if (pItem != NULL) { dc.SetTextColor( pItem->m_Color ); if (pItem->m_Bold) { dc.SelectObject( &boldFontDC ); } } int imageIndex; int selectedImageIndex; GetItemImage(hItem,imageIndex,selectedImageIndex); int imageOffset = 0; if (imageIndex >= 0) { m_imageList->Draw(&dc,imageIndex,rect.TopLeft(),ILD_TRANSPARENT); imageOffset = 16; } DrawItemText(&dc, sItem, CRect(rect.left+2+imageOffset, rect.top, GetColumnWidth(0)-imageOffset, rect.bottom), GetColumnWidth(0)-rect.left-2-imageOffset, GetColumnAlign(0)); m_nWidth = 0; for (int i = 1;i < m_nColumns;i++) { CString subString = ((pItem != NULL) ? pItem->GetSubstring(*this,hItem,i) : ""); m_nWidth += GetColumnWidth(i-1); DrawItemText(&dc,subString,CRect(m_nWidth, rect.top, m_nWidth+GetColumnWidth(i), rect.bottom), GetColumnWidth(i), GetColumnAlign(i)); } dc.SetTextColor(::GetSysColor (COLOR_WINDOWTEXT )); if (pItem != NULL && pItem->m_Bold) { dc.SelectObject( &fontDC ); } } else { CRect m_labelRect; GetItemRect( hItem, &m_labelRect, TRUE ); GetItemRect( hItem, &rect, FALSE ); if (GetColumnsNum() > 1) rect.left = min(m_labelRect.left, GetColumnWidth(0)); else rect.left = m_labelRect.left; rect.right = m_nColumnsWidth; int imageIndex; int selectedImageIndex; GetItemImage(hItem,imageIndex,selectedImageIndex); int imageOffset = 0; if (selectedImageIndex >= 0) { m_imageList->Draw(&dc,selectedImageIndex,rect.TopLeft(),ILD_TRANSPARENT); imageOffset = 16; } // If the item is selected, paint the rectangle with the system color // COLOR_HIGHLIGHT COLORREF m_highlightColor = ::GetSysColor (COLOR_HIGHLIGHT); CBrush brush(m_highlightColor); CRect fillRect(rect); fillRect.left += imageOffset; dc.FillRect (fillRect, &brush); // draw a dotted focus rectangle dc.DrawFocusRect (rect); pItem = (CTreeListItem *)CTreeCtrl::GetItemData(hItem); CString sItem = ((pItem != NULL) ? pItem->GetItemText() : ""); dc.SetBkColor(m_highlightColor); dc.SetTextColor(::GetSysColor (COLOR_HIGHLIGHTTEXT)); if (pItem != NULL && pItem->m_Bold) { dc.SelectObject( &boldFontDC ); } //DrawItemText(&dc, sItem, CRect(rect.left+2, rect.top, GetColumnWidth(0), rect.bottom), GetColumnWidth(0)-rect.left-2, GetColumnAlign(0)); DrawItemText(&dc, sItem, CRect(rect.left+2+imageOffset, rect.top, GetColumnWidth(0)-imageOffset, rect.bottom), GetColumnWidth(0)-rect.left-2-imageOffset, GetColumnAlign(0)); m_nWidth = 0; for (int i = 1;i < m_nColumns;i++) { CString subString = ((pItem != NULL) ? pItem->GetSubstring(*this,hItem,i) : ""); m_nWidth += GetColumnWidth(i-1); DrawItemText(&dc,subString,CRect(m_nWidth, rect.top, m_nWidth+GetColumnWidth(i), rect.bottom), GetColumnWidth(i), GetColumnAlign(i)); } if (pItem != NULL && pItem->m_Bold) { dc.SelectObject( &fontDC ); } } hItem = GetNextVisibleItem( hItem ); n--; } dc.SelectObject( pFontDC ); }
void ListBox::DrawContent(const Rect& ctrl_rect) { SizeColumns(); Rect item_rect = ctrl_rect; item_rect.h = line_height; int h = rect.h; // draw headings at top, if needed: if (show_headings) { Color save_color = back_color; back_color = ShadeColor(back_color, 1.3); font->SetColor(fore_color); int max_column = columns.size()-1; item_rect.h += HEADING_EXTRA; page_size = (h-item_rect.h) / (line_height + leading); for (int column = 0; column <= max_column; column++) { item_rect.w = GetColumnWidth(column); // draw heading button FillRect(item_rect, back_color); DrawStyleRect(item_rect, WIN_RAISED_FRAME); Rect title_rect = item_rect; title_rect.Deflate(3,3); DrawText(GetColumnTitle(column), 0, title_rect, DT_CENTER|DT_SINGLELINE); item_rect.x += item_rect.w; } item_rect.y += item_rect.h; back_color = save_color; item_rect.h = line_height; } int index = 0; ListIter<ListBoxItem> iter = items; while (++iter && item_rect.y < h) { ListBoxItem* item = iter.value(); if (index++ >= top_index) { // draw main item: int column = 0; item_rect.x = ctrl_rect.x; item_rect.w = GetColumnWidth(column) - 2; if (item_rect.y + item_rect.h > h) { item_rect.h = h - item_rect.y - 1; } Color item_color = GetItemColor(index-1, 0); if (item->selected) { font->SetColor(selected_color); if (seln_style == LIST_ITEM_STYLE_FILLED_BOX) FillRect(item_rect, selected_color * 0.25); if (seln_style >= LIST_ITEM_STYLE_BOX) DrawRect(item_rect, selected_color); } else { font->SetColor(item_color); if (item_style == LIST_ITEM_STYLE_FILLED_BOX) FillRect(item_rect, item_color * 0.25); if (item_style >= LIST_ITEM_STYLE_BOX) DrawRect(item_rect, item_color); } Rect text_rect = item_rect; if (item->image && item->image->Width() > 0 && item->image->Height() > 0) { DrawBitmap(text_rect.x, text_rect.y, text_rect.x + text_rect.w, text_rect.y + line_height, item->image); } else { text_rect.Deflate(2,0); DrawText(item->text.data(), item->text.length(), text_rect, GetColumnAlign(column)|DT_SINGLELINE); } // draw subitems: ListIter<ListBoxCell> sub_iter = item->subitems; while (++sub_iter) { ListBoxCell* sub = sub_iter.value(); column++; item_rect.x += item_rect.w + 2; item_rect.w = GetColumnWidth(column) - 2; if (item->selected) { if (seln_style == LIST_ITEM_STYLE_FILLED_BOX) FillRect(item_rect, selected_color * 0.25); if (seln_style >= LIST_ITEM_STYLE_BOX) DrawRect(item_rect, selected_color); } else { if (item_style == LIST_ITEM_STYLE_FILLED_BOX) FillRect(item_rect, item_color * 0.25); if (item_style >= LIST_ITEM_STYLE_BOX) DrawRect(item_rect, item_color); } if (item->selected) font->SetColor(selected_color); else font->SetColor(GetItemColor(index-1, column)); Rect text_rect = item_rect; if (sub->image && sub->image->Width() > 0 && sub->image->Height() > 0) { DrawBitmap(text_rect.x, text_rect.y, text_rect.x + text_rect.w, text_rect.y + line_height, sub->image); } else { text_rect.Deflate(2,0); DrawText(sub->text.data(), sub->text.length(), text_rect, GetColumnAlign(column)|DT_SINGLELINE); } } item_rect.y += line_height + leading; } } }