Reputation: 4025
I am trying to customize CToolbar
class(MFC library) for my needs (enabling support of 32 bit icons with anti-aliasing).
For that purpose I am using custom draw method provided by CToolbar, where I paint my own Icons.
Icons are rendered fine and everything is OK, until user opens customization dialog - where he can pick needed icons and arrange them as he likes (this is standard customization dialog for MFC toolbar).
Problem: in customization dialog the same image is drawn for all buttons.
Here are code snippets from my project:
void CCustomToolBar::OnCustomDraw (NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMTBCUSTOMDRAW lpNMCustomDraw = (LPNMTBCUSTOMDRAW)pNMHDR;
switch (lpNMCustomDraw->nmcd.dwDrawStage)
{
// the Painting loop starts
case CDDS_PREPAINT:
{
*pResult = CDRF_NOTIFYITEMDRAW; // register for items drawing events
//if (m_bAdjusting)SetWindowLong (lpNMCustomDraw->nmcd.hdr.hwndFrom, DWL_MSGRESULT, CDRF_NOTIFYITEMDRAW);
}
break;
// Item (button) is going to be painted, do own drawing
case CDDS_ITEMPREPAINT:
{
CDC DrawDC;
DrawDC.Attach(lpNMCustomDraw->nmcd.hdc);
CToolBarCtrl& ControlBar = GetToolBarCtrl();
CRect btnRect = lpNMCustomDraw->nmcd.rc;
int State = lpNMCustomDraw->nmcd.uItemState; // CDIS_GRAYED | CDIS_CHECKED | CDIS_DEFAULT | CDIS_DISABLED | CDIS_FOCUS | CDIS_GRAYED | CDIS_HOT | CDIS_SELECTED
hugeSizedImages_.SetTransparentColor(afxGlobalData.clrBtnFace);
DrawDC.DrawFrameControl(&btnRect, DFC_BUTTON , DFCS_BUTTONPUSH );
DWORD dwItem = (DWORD)lpNMCustomDraw->nmcd.dwItemSpec;
int btnIndex = 0;
GUARD_IF(m_BitmapIndex.find(dwItem) != m_BitmapIndex.end())
{
btnIndex = m_BitmapIndex[dwItem];
}
CAfxDrawState ds;
hugeSizedImages_.PrepareDrawImage(ds, CSize(48,48));
hugeSizedImages_.Draw(&DrawDC, btnRect.left, btnRect.top, btnIndex);
hugeSizedImages_.EndDrawImage(ds);
DrawDC.Detach();
*pResult = CDRF_SKIPDEFAULT; // No further drawing
//if (m_bAdjusting) SetWindowLong (lpNMCustomDraw->nmcd.hdr.hwndFrom, DWL_MSGRESULT, CDRF_SKIPDEFAULT);
}
break;
default:
*pResult = 0;
}
Message map:
BEGIN_MESSAGE_MAP ( CCustomToolBar, CToolBarWithHideableButtons )
//{{AFX_MSG_MAP ( CCustomToolBar )
ON_WM_CONTEXTMENU ()
ON_COMMAND ( CM_TOOLBAR_CUSTOMIZE, OnPopupCustomize )
ON_NOTIFY_REFLECT ( TBN_QUERYDELETE, OnQueryDelete )
ON_NOTIFY_REFLECT ( TBN_QUERYINSERT, OnQueryInsert )
ON_NOTIFY_REFLECT ( TBN_GETBUTTONINFO, OnGetButtonInfo )
ON_NOTIFY_REFLECT ( TBN_BEGINADJUST, OnBeginAdjust )
ON_NOTIFY_REFLECT ( TBN_ENDADJUST, OnEndAdjust )
ON_NOTIFY_REFLECT ( TBN_RESET, OnReset )
ON_NOTIFY_REFLECT ( TBN_TOOLBARCHANGE, OnToolBarChange )
ON_NOTIFY_REFLECT ( TBN_INITCUSTOMIZE, OnInitCustomize ) //only with IE 5.0 and
ON_NOTIFY_REFLECT ( NM_CUSTOMDRAW, OnCustomDraw)
ON_WM_CREATE ()
//}}AFX_MSG_MAP
END_MESSAGE_MAP ()
I have done debugging a lot, and managed to get that when customizing window appear - information that is sent to CustomDraw related to the item(button) to be drawn is missed, always zero index is sent.
If I comment out *pResult = CDRF_SKIPDEFAULT; when system is able to draw icons in customization dialog according to their indexes(but with 4 bit depth only) - hence I think that I have ommited smth important in the custom draw.
I have read all possible articles in MSDN and other sources related to the custom draw, but no problem of icons in customization window is mentioned there.
So if anybody has faced with similar problem, could you please give me a hint? thanks in advance!
Upvotes: 1
Views: 1110
Reputation: 15355
Use the new CMFCToolBar... it is much simpler and it provides an easy overridable DrawItem method.
Upvotes: 2