一、自繪接口
WTL提供的CCustomDraw,通過讓控件繼承CCustomDraw,可以實現控件的自繪。
二、自繪步驟
1. 派生新的控件類,同時繼承CCustomDraw,比如:
class COwerTree :
public CWindowImpl<COwerTree, CTreeViewCtrl>,
public CCustomDraw<COwerTree>
2. 添加Message Map
BEGIN_MSG_MAP(COwerTree)
REFLECTED_NOTIFY_CODE_HANDLER_EX(TVN_ITEMEXPANDING, OnItemExpanding)
CHAIN_MSG_MAP_ALT(CCustomDraw<COwerTree>, 1)
DEFAULT_REFLECTION_HANDLER()
END_MSG_MAP()
CHAIN_MSG_MAP_ALT(CCustomDraw<COwerTree>, 1)
DEFAULT_REFLECTION_HANDLER()
END_MSG_MAP()
3. 添加相應函數
// 自繪
LRESULT COwerTree::OnItemExpanding ( NMHDR* phdr )
{
return FALSE;
}
DWORD COwerTree::OnPrePaint(int idCtrl, LPNMCUSTOMDRAW lpNMCD)
{
return CDRF_NOTIFYITEMDRAW;
}
DWORD COwerTree::OnItemPrePaint(int idCtrl, LPNMCUSTOMDRAW lpNMCD)
{
NMTVCUSTOMDRAW* pnmtv = (NMTVCUSTOMDRAW*) lpNMCD;
RECT rcItem = lpNMCD->rc ; // Item的區域
HDC hdcItem = lpNMCD->hdc ; // Item的dc
POINT ptItem ;
ptItem.x = rcItem.left + 1 ;
ptItem.y = rcItem.top + 1 ;
UINT uFlags;
HTREEITEM hItem = HitTest(ptItem, &uFlags); // Item 句柄
if(!hItem) return CDRF_DODEFAULT;
LRESULT COwerTree::OnItemExpanding ( NMHDR* phdr )
{
return FALSE;
}
DWORD COwerTree::OnPrePaint(int idCtrl, LPNMCUSTOMDRAW lpNMCD)
{
return CDRF_NOTIFYITEMDRAW;
}
DWORD COwerTree::OnItemPrePaint(int idCtrl, LPNMCUSTOMDRAW lpNMCD)
{
NMTVCUSTOMDRAW* pnmtv = (NMTVCUSTOMDRAW*) lpNMCD;
RECT rcItem = lpNMCD->rc ; // Item的區域
HDC hdcItem = lpNMCD->hdc ; // Item的dc
POINT ptItem ;
ptItem.x = rcItem.left + 1 ;
ptItem.y = rcItem.top + 1 ;
UINT uFlags;
HTREEITEM hItem = HitTest(ptItem, &uFlags); // Item 句柄
if(!hItem) return CDRF_DODEFAULT;
// 在這裏實現Item的自繪
return CDRF_SKIPDEFAULT; // 忽略系統的默認自繪
}
return CDRF_SKIPDEFAULT; // 忽略系統的默認自繪
}
三、附件實現了簡單的樹控件的自繪