Setting color and font attribute for individual items (单独设置CTreectrl 的item 的颜色 与字体)
来源:互联网 发布:java 设置md5长度 编辑:程序博客网 时间:2024/06/03 11:18
Step 1: Add member variable to track font and color
Since the control has no support for item font or color, we have to track this information within our program. We use a CMap object to associate these properties with the tree items. The map will contain information for only those items that we explicitly change. We define a nested structure that is used with the CMap object, to hold the color and the font information.
protected:
struct Color_Font
{
COLORREF color;
LOGFONT logfont;
};
CMap< void*, void*, Color_Font, Color_Font& > m_mapColorFont ;Step 2: Add helper functions to get/set font and color
Define the helper functions to get or set the item font or color. To set the font, we actually pass the logfont rather than a font handle. Also note that we have defined a pair of functions to get and set the bold attribute. There are two reasons for providing a separate function for the bold attribute although we can use the font function. The first reason is that the tree view control directly supports setting an item to bold. Secondly, using the built in support also maintains the proper setting for the horizontal scrollbar.
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;
}Step 3: Add WM_PAINT handler
In this function we first let the control update a memory device context. We then redraw the visible labels using the user defined attributes. We let the control handle the highlighting of items, so before we update a label we make sure that it is not selected or drophilited. Also, if the items font or color attributes were not changed, we don't need to redraw it. Once all the updates are ready in the memory device context, we blit it to the actual device context.
In the first implementation, I let the control directly draw on the screen and then redrew the items that had a different font or color. This caused a visible flicker as the items got updated a second time. To overcome this we use a memory device context for all the updates and finally we copy this to the main device context. After creating a compatible DC, we add a compatible bitmap to the memory DC and set the clip region to be the same as the paint DC.
After the default window procedure for the control has updated the device context, we scan through all the visible items and update the items that have a user defined color or font.
void CTreeCtrlX::OnPaint()
{
CPaintDC dc(this);
// Create a memory DC compatible with the paint DC
CDC memDC;
memDC.CreateCompatibleDC( &dc );
CRect rcClip, rcClient;
dc.GetClipBox( &rcClip );
GetClientRect(&rcClient);
// Select a compatible bitmap into the memory DC
CBitmap bitmap;
bitmap.CreateCompatibleBitmap( &dc, rcClient.Width(), rcClient.Height() );
memDC.SelectObject( &bitmap );
// Set clip region to be same as that in paint DC
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
{
// No font specified, so use window font
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 );
}
Step 4: Go ahead and change the item font or color
Here are some examples.
// Change the item color to red
SetItemColor( hItem, RGB(255,0,0));
// Change the item to italicized font and underlined
LOGFONT logfont;
CFont *pFont = GetFont();
pFont->GetLogFont( &logfont );
logfont.lfItalic = TRUE;
logfont.lfUnderline = TRUE;
SetItemFont(hti, logfont );
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ah_wind/archive/2007/11/27/1903827.aspx
- Setting color and font attribute for individual items (单独设置CTreectrl 的item 的颜色 与字体)
- ListView的Items字体颜色设置
- Font---Android中字体颜色的设置 .
- CTreeCtrl单独节点颜色设置
- IOS 字体的 NSString Font,以及 UIButton 设置字体的颜色 font 等。
- VC++中改变CTreeCtrl子项Item的字体和颜色
- SWT 的字体(Font)、颜色(Color)、图像(Image)和绘图
- Color & Font - 使用颜色和字体
- UITabbar 中的 item的字体颜色 item点击后的tint颜色 和图片颜色的修改 UINavigationBar字体设置
- MFC CTreeCtrl 设置每一项Item颜色
- User select font-face/color/size/backgroundColor设置 字体,颜色,大小,背景色兼容主流浏览器
- User select font-face/color/size/backgroundColor设置 字体,颜色,大小,背景色兼容主流浏览器
- android设置toolbar的menu itemTextView颜色 set toolbar menu item text color
- <P>段落标签</p>设置字体和大小,颜色以及与<font>文本标签</font>
- [Java] Font/Color/Event 字体/颜色/事件综合
- ToolBar修改menu item的字体颜色
- ToolBar修改menu item的字体颜色
- 单独 定义一个Activity的 标题 的字体颜色
- gstreamer video 相关属性
- 查看.o, .obj文件符号列表,强大的nm命令
- 一个Asp.net下word在线编辑、全文关键字搜索的完整示例
- 深入理解Linux启动过程
- InputStream 和 byte[] 互转
- Setting color and font attribute for individual items (单独设置CTreectrl 的item 的颜色 与字体)
- Delphi 使用 RTTI 动态通过名称调用函数和设置控件属性值
- 打开页面只自动刷新一次
- ASP.Net控制不同的人编辑word文档中不同的可编辑区域的完整示例
- SQL 连接
- 分享两道华为的C语言上机题——包含答案。
- web中的下拉菜单与soaoffice控件
- 控制用户打开excel文件后只能查看和编辑指定的Sheet页
- 没有积分。