我把滑动控件(TrackBar)放入了工具栏里面.
只是背景色跟其它的不匹配.如图:上面二个都有渐变背景色.  从左往右颜色逐渐变深 .
最下面的是个带菜单的对话框. 它的菜单整个的颜色都相同.怎么让滑动控件像其它控件一样,支持工具栏里的渐变色?

解决方案 »

  1.   

    重写CTooLBar类,添加处理WM_ERASEBKGND消息,里面自绘背景色
      

  2.   

    http://www.codeproject.com/Articles/8985/Customizing-the-Appearance-of-CSliderCtrl-Using-Cu#Introduction
    这个文章里涉及了一些用NM_CUSTOMDRAW自绘的方法.
    里面提到了可以自绘的一些部件.好像这个背景不属于自绘范围.
    我想比起自绘,应该有一些简单通过的方法的.
    那菜单对渐变色的支持从哪里设置或取消呢.
      

  3.   

    应该不用自绘那么麻烦的,正如Tab控件背景,我后来才知道可以用EnableThemeDialogTexture搞定
      

  4.   

    那个渐变色是rebar的吧
    我试了CDialogBar用EnableThemeDialogTexture也是不行的,唉,自绘吧
      

  5.   

    楼主,我搞掂了HBRUSH CMyTrackBar::CtlColor(CDC* pDC, UINT nCtlColor) 
    {
      CRect rect;
      CWnd* pParent = GetParent();
      pParent->GetWindowRect(&rect);  CDC dcMem;
      dcMem.CreateCompatibleDC(pDC);  CBitmap Bitmap;
      Bitmap.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
      CBitmap* BitmapOld = dcMem.SelectObject(&Bitmap);  CPoint pt(0, 0);
      MapWindowPoints(pParent, &pt, 1);
      pt = dcMem.OffsetWindowOrg(pt.x, pt.y);
      pParent->SendMessage(WM_ERASEBKGND, (WPARAM)dcMem.m_hDC, 0L);
      dcMem.SetWindowOrg(pt.x, pt.y);
      dcMem.SelectObject(BitmapOld);
      
      static HBRUSH hBrush;
      if (hBrush)
        ::DeleteObject((HGDIOBJ)hBrush);
      hBrush = ::CreatePatternBrush((HBITMAP)Bitmap);
      Bitmap.DeleteObject();
      return hBrush;
    }
      

  6.   


    ,的确是个完美的解决方案.
    谢谢你,gfm688.
      

  7.   

    把我COPY你的代码贴出来.
    有一个GDI对象没有释放,我帮你改过来了.
    MESSAGE_HANDLER(WM_CTLCOLORSTATIC,OnCtrlColorStatic)LRESULT OnCtrlColorStatic(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
    {
    LRESULT lResult=FALSE;
    HDC dc=(HDC)wParam;
    HWND wnd=(HWND)lParam; if (wnd==pVolume->m_hWnd 
    ||wnd==pTrack->m_hWnd 
    )
    {
    //!m_hWnd ,父控件,Rebar
    //!wnd    ,子控件,trackBar RECT rc;
    ::GetWindowRect(m_hWnd,&rc); HDC dcMem;
    dcMem= ::CreateCompatibleDC(dc);
    HBITMAP bmp,oldBmp;
    bmp=::CreateCompatibleBitmap(dc,rc.right-rc.left,rc.bottom-rc.top);
    oldBmp=(HBITMAP)::SelectObject(dcMem,bmp); POINT pt={0,0};
    ::MapWindowPoints(wnd,m_hWnd,&pt,1);
    ::OffsetWindowOrgEx(dcMem,pt.x,pt.y,&pt);
    ::SendMessage(m_hWnd,WM_ERASEBKGND, (WPARAM)dcMem, 0L);
    ::SetWindowOrgEx(dcMem,pt.x,pt.y,NULL);
    ::SelectObject(dcMem,oldBmp);
    ::DeleteDC(dcMem); static HBRUSH hBrush=0;
    if (hBrush)
    ::DeleteObject((HGDIOBJ)hBrush); hBrush = ::CreatePatternBrush(bmp);
    ::DeleteObject(bmp); lResult=(LRESULT)hBrush;
    } return lResult;
    }
      

  8.   

     ::SelectObject(dcMem,oldBmp);
    这行好像是多余的.
      

  9.   

    用MFC的话,可以按我那样写,因为用CDC的话,析构函数自己会DeleteDC的,但析构函数执行的顺序肯定在Bitmap.DeleteObject();之后,所以必须dcMem.SelectObject(BitmapOld);你是用WTL? 用API的话,我也会像你那样写