VC++でCTreeCtrlサブアイテムItemのフォントと色を変更
CTreeCtrlを継承するクラスを定義することで、サブアイテムのフォントと色を変更します.
// 、 :
protected:
struct Color_Font
{
COLORREF color;
LOGFONT logfont;
};
// CMap Item 、
CMap< void*, void*, Color_Font, Color_Font& > m_mapColorFont ;
// 、 、
void CTreeCtrlX::SetItemFont(HTREEITEM hItem, LOGFONT& logfont)
{
Color_Font cf;
if( !m_mapColorFont.Lookup( hItem, cf ) )
cf.color = (COLORREF)-1;
cf.logfont = logfont;
m_mapColorFont[hItem] = cf;
}
void CTreeCtrlX::SetItemBold(HTREEITEM hItem, BOOL bBold)
{
SetItemState( hItem, bBold ? TVIS_BOLD: 0, TVIS_BOLD );
}
void CTreeCtrlX::SetItemColor(HTREEITEM hItem, COLORREF color)
{
Color_Font cf;
if( !m_mapColorFont.Lookup( hItem, cf ) )
cf.logfont.lfFaceName[0] = '\0';
cf.color = color;
m_mapColorFont[hItem] = cf;
}
BOOL CTreeCtrlX::GetItemFont(HTREEITEM hItem, LOGFONT * plogfont)
{
Color_Font cf;
if( !m_mapColorFont.Lookup( hItem, cf ) )
return FALSE;
if( cf.logfont.lfFaceName[0] == '\0' )
return FALSE;
*plogfont = cf.logfont;
return TRUE;
}
BOOL CTreeCtrlX::GetItemBold(HTREEITEM hItem)
{
return GetItemState( hItem, TVIS_BOLD ) & TVIS_BOLD;
}
COLORREF CTreeCtrlX::GetItemColor(HTREEITEM hItem)
{
// Returns (COLORREF)-1 if color was not set
Color_Font cf;
if( !m_mapColorFont.Lookup( hItem, cf ) )
return (COLORREF)-1;
return cf.color;
}
// WM_PAINT , OnPaint()
afx_msg void CMyTreeCtrl::OnPaint()
{
// DC
CPaintDC dc(this);
// DC memory DC
//memory device context : ( 、 、
// ) 。 , ,
// 。
CDC memDC;
// DC
memDC.CreateCompatibleDC( &dc );
// CRect ,
CRect rcClip, rcClient;
//
dc.GetClipBox( &rcClip );
//
GetClientRect(&rcClient);
// Select a compatible bitmap into the memory DC
// bmp , memDC
//
CBitmap bitmap;
bitmap.CreateCompatibleBitmap( &dc, rcClient.Width(), rcClient.Height() );
memDC.SelectObject( &bitmap );
// Set clip region to be same as that in paint DC
// CRgn
CRgn rgn;
rgn.CreateRectRgnIndirect( &rcClip );
memDC.SelectClipRgn(&rgn);
rgn.DeleteObject();
// First let the control do its default drawing.
// ,
CWnd::DefWindowProc( WM_PAINT, (WPARAM)memDC.m_hDC, 0 );
//
HTREEITEM hItem = GetFirstVisibleItem();
//
int n = GetVisibleCount()+1;
while( hItem && n--)
{
CRect rect;
// Do not meddle with selected items or drop highlighted items
//
UINT selflag = TVIS_DROPHILITED | TVIS_SELECTED;
// 、
Color_Font cf;
//
if ( !(GetItemState( hItem, selflag ) & selflag )
&& m_mapColorFont.Lookup( hItem, cf ))
{
CFont *pFontDC;
CFont fontDC;
LOGFONT logfont;
if( cf.logfont.lfFaceName[0] != '\0' )
{
//
logfont = cf.logfont;
}
else
{
// ,
CFont *pFont = GetFont();
pFont->GetLogFont( &logfont );
}
//
if( GetItemBold( hItem ) )
logfont.lfWeight = 700;
//
fontDC.CreateFontIndirect( &logfont );
pFontDC = memDC.SelectObject( &fontDC );
//
if( cf.color != (COLORREF)-1 )
memDC.SetTextColor( cf.color );
//
CString sItem = GetItemText( hItem );
//
GetItemRect( hItem, &rect, TRUE );
//
memDC.SetBkColor( GetSysColor( COLOR_WINDOW ) );
// ,
memDC.TextOut( rect.left+2, rect.top+1, sItem );
memDC.SelectObject( pFontDC );
}
hItem = GetNextVisibleItem( hItem );
}
dc.BitBlt( rcClip.left, rcClip.top, rcClip.Width(), rcClip.Height(), &memDC,
rcClip.left, rcClip.top, SRCCOPY );
}