rt,如何改变一个TAB控件的颜色?

解决方案 »

  1.   

    自画 Tab#if !defined(AFX_TABCTRLEX_H__28407441_38AF_11D1_ABBA_00A0243D1382__INCLUDED_)
    #define AFX_TABCTRLEX_H__28407441_38AF_11D1_ABBA_00A0243D1382__INCLUDED_#if _MSC_VER >= 1000
    #pragma once
    #endif // _MSC_VER >= 1000// TabCtrlEx.h : header file
    ///////////////////////////////////////////////////////////////////////////////
    // CTabCtrlEx windowclass CTabCtrlEx : public CTabCtrl
    {
    // Construction/destruction
    public:
    CTabCtrlEx();
    virtual ~CTabCtrlEx();// Attributes:
    public:// Operations
    public:
    void SetFonts(CFont* pSelFont, CFont* pUnselFont);
    void SetFonts(int nSelWeight=FW_SEMIBOLD, BOOL bSelItalic=FALSE,   BOOL bSelUnderline=FALSE,
                  int nUnselWeight=FW_MEDIUM, BOOL bUnselItalic=FALSE, BOOL bUnselUnderline=FALSE); void SetColours(COLORREF bSelColour, COLORREF bUnselColour);// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CTabCtrlEx)
    public:
    virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
    protected:
    virtual void PreSubclassWindow();
    //}}AFX_VIRTUAL// Implementation
    protected:
    COLORREF m_crSelColour, m_crUnselColour;
    CFont    m_SelFont, m_UnselFont;// Generated message map functions
    protected:
    //{{AFX_MSG(CTabCtrlEx)
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
    // Microsoft Developer Studio will insert additional declarations immediately before the previous line.#endif // !defined(AFX_TABCTRLEX_H__28407441_38AF_11D1_ABBA_00A0243D1382__INCLUDED_)/////////////////////////////////////////////////////////////////////////////// TabCtrlEx.cpp : implementation file
    //#include "stdafx.h"
    #include "TabCtrlEx.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CTabCtrlExCTabCtrlEx::CTabCtrlEx()
    {
    m_crSelColour     = RGB(0,0,255);
    m_crUnselColour   = RGB(50,50,50);
    }CTabCtrlEx::~CTabCtrlEx()
    {
    m_SelFont.DeleteObject();
    m_UnselFont.DeleteObject();
    }BEGIN_MESSAGE_MAP(CTabCtrlEx, CTabCtrl)
    //{{AFX_MSG_MAP(CTabCtrlEx)
    ON_WM_CREATE()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CTabCtrlEx message handlersint CTabCtrlEx::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
    if (CTabCtrl::OnCreate(lpCreateStruct) == -1) return -1;
    ModifyStyle(0, TCS_OWNERDRAWFIXED);

    return 0;
    }void CTabCtrlEx::PreSubclassWindow() 
    {
    CTabCtrl::PreSubclassWindow();
    ModifyStyle(0, TCS_OWNERDRAWFIXED);
    }void CTabCtrlEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
    {
    CRect rect = lpDrawItemStruct->rcItem;
    int nTabIndex = lpDrawItemStruct->itemID;
    if (nTabIndex < 0) return;
    BOOL bSelected = (nTabIndex == GetCurSel()); char label[64];
    TC_ITEM tci;
    tci.mask = TCIF_TEXT|TCIF_IMAGE;
    tci.pszText = label;     
    tci.cchTextMax = 63;    
    if (!GetItem(nTabIndex, &tci )) return; CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
    if (!pDC) return;
    int nSavedDC = pDC->SaveDC(); // For some bizarre reason the rcItem you get extends above the actual
    // drawing area. We have to workaround this "feature".
    rect.top += ::GetSystemMetrics(SM_CYEDGE); pDC->SetBkMode(TRANSPARENT);
    pDC->FillSolidRect(rect, ::GetSysColor(COLOR_BTNFACE)); // Draw image
    CImageList* pImageList = GetImageList();
    if (pImageList && tci.iImage >= 0) { rect.left += pDC->GetTextExtent(_T(" ")).cx; // Margin // Get height of image so we 
    IMAGEINFO info;
    pImageList->GetImageInfo(tci.iImage, &info);
    CRect ImageRect(info.rcImage);
    int nYpos = rect.top; pImageList->Draw(pDC, tci.iImage, CPoint(rect.left, nYpos), ILD_TRANSPARENT);
    rect.left += ImageRect.Width();
    } if (bSelected) {
    pDC->SetTextColor(m_crSelColour);
    pDC->SelectObject(&m_SelFont);
    rect.top -= ::GetSystemMetrics(SM_CYEDGE);
    pDC->DrawText(label, rect, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
    } else {
    pDC->SetTextColor(m_crUnselColour);
    pDC->SelectObject(&m_UnselFont);
    pDC->DrawText(label, rect, DT_SINGLELINE|DT_BOTTOM|DT_CENTER);
    } pDC->RestoreDC(nSavedDC);
    }/////////////////////////////////////////////////////////////////////////////
    // CTabCtrlEx operationsvoid CTabCtrlEx::SetColours(COLORREF bSelColour, COLORREF bUnselColour)
    {
    m_crSelColour = bSelColour;
    m_crUnselColour = bUnselColour;
    Invalidate();
    }void CTabCtrlEx::SetFonts(CFont* pSelFont, CFont* pUnselFont)
    {
    ASSERT(pSelFont && pUnselFont); LOGFONT lFont;
    int nSelHeight, nUnselHeight; m_SelFont.DeleteObject();
    m_UnselFont.DeleteObject(); pSelFont->GetLogFont(&lFont);
    m_SelFont.CreateFontIndirect(&lFont);
    nSelHeight = lFont.lfHeight; pUnselFont->GetLogFont(&lFont);
    m_UnselFont.CreateFontIndirect(&lFont);
    nUnselHeight = lFont.lfHeight; SetFont( (nSelHeight > nUnselHeight)? &m_SelFont : &m_UnselFont);
    }
    void CTabCtrlEx::SetFonts(int nSelWeight,   BOOL bSelItalic,   BOOL bSelUnderline,
                              int nUnselWeight, BOOL bUnselItalic, BOOL bUnselUnderline)
    {
    // Free any memory currently used by the fonts.
    m_SelFont.DeleteObject();
    m_UnselFont.DeleteObject(); // Get the current font
    LOGFONT lFont;
    CFont *pFont = GetFont();
    if (pFont)
    pFont->GetLogFont(&lFont);
    else {
    NONCLIENTMETRICS ncm;
    ncm.cbSize = sizeof(NONCLIENTMETRICS);
    VERIFY(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 
                                sizeof(NONCLIENTMETRICS), &ncm, 0));
    lFont = ncm.lfMessageFont; 
    } // Create the "Selected" font
    lFont.lfWeight = nSelWeight;
    lFont.lfItalic = bSelItalic;
    lFont.lfUnderline = bSelUnderline;
    m_SelFont.CreateFontIndirect(&lFont); // Create the "Unselected" font
    lFont.lfWeight = nUnselWeight;
    lFont.lfItalic = bUnselItalic;
    lFont.lfUnderline = bUnselUnderline;
    m_UnselFont.CreateFontIndirect(&lFont); SetFont( (nSelWeight > nUnselWeight)? &m_SelFont : &m_UnselFont);
    }
      

  2.   

    他只能改变tab顶部选项的颜色,但是不能改变他的背景,比如,我对话框是兰色,但是tab仍然是灰色的背景,我想让tab也变为兰色,怎么办?
      

  3.   

    在CTabCtrlEx中增加一个成员变量CBrush m_brush;
    HBRUSH CTabCtrlEx::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
    //HBRUSH hbr = CTabCtrl::OnCtlColor(pDC, pWnd, nCtlColor);

             m_brush.CreateSolidBrush(RGB(0, 0, 255));
    // TODO: Change any attributes of the DC here

    // TODO: Return a different brush if the default is not desired
    return m_brush;
    }
      

  4.   

    Up!
    顺便问一下:想搞底层开发,但又酷爱java,是往vc还是java发展好?
      

  5.   

    to : cxjlw(老为) 
    不行啊,我试了,但颜色还是没有改变