视窗:工具栏:: NM_CUSTOMDRAW。更改工具栏背景颜色

时间:2013-03-08 23:49:43

标签: c++ winapi toolbar

这是代码的摘录。 创建前工具栏的样式:

DWORD styles = TBSTYLE_FLAT | WS_CHILD  | TBSTYLE_TOOLTIPS; //| TBSTYLE_CUSTOMERASE;//| WS_BORDER ;
DWORD cc_styles =  CCS_VERT | CCS_NODIVIDER | CCS_NORESIZE;

处理NM_CUSTOMDRAW:

    case WM_NOTIFY:
        switch ( ((LPNMHDR)lparam)->code )
{
...
        case NM_CUSTOMDRAW:
        {
            //LPNMHDR header_ptr = safe_ptr_cast< LPNMHDR >( &lparam );
            //if ( header_ptr->hwndFrom == m_ptr->get_operation_tab_toolbar_handle( ) )
            LPNMTBCUSTOMDRAW data_ptr = (LPNMTBCUSTOMDRAW) lparam;
            switch(data_ptr->nmcd.dwDrawStage) 
            {
                case CDDS_ITEMPREPAINT:
                    SetWindowTheme(m_ptr->get_operation_tab_toolbar_handle(), _T(""), _T(""));
                    //data_ptr->hbrMonoDither = GetStockBrush(BLACK_BRUSH);
                    SetBkColor( data_ptr->nmcd.hdc, RGB(0,0,0));
                    FillRect( data_ptr->nmcd.hdc, &data_ptr->nmcd.rc, RGB(0,0,0));
                    //FillRect( data_ptr->nmcd.hdc, &data_ptr->nmcd.rc, GetStockBrush(BLACK_BRUSH));
                    return CDRF_NEWFONT;
                case CDDS_PREPAINT:
                {
                    return CDRF_NOTIFYITEMDRAW;
                }
            }
        }
}

我只收到CDDS_PREPAINT。在文档MSDN Custom Drawing Doc.中 据说在返回值CDRF_NOTIFYITEMDRAW CDDS_ITEMPREPAINT应该是下一个。

1 个答案:

答案 0 :(得分:1)

使用样式TBSTYLE_FLATE或TBSTYLE_TRANSPARENT创建工具栏时,TBSTYLE_TRANSPARENT使用父背景颜色。因此,要为工具栏实现不同的背景颜色,应使用SetClassLongPtr(对于32x和64x兼容性)作为父窗口。这样就解决了问题。