wsk3q

导航

如何更改CPropertySheet的背景色?

//从我的新浪博客搬过来

如何更改CPropertySheet的背景色? 当然这里指的是包括改CPropertyPage和标签的背景颜色.

 
(1)改CPropertyPage的背景色:
1)创建一个CPropertyPage的子类: class COwnerDrawPropPage : public CPropertyPage;
2)处理它的WM_ERASEBKGND 和 WM_CTLCOLOR消息:
BOOL COwnerDrawPropPage::OnEraseBkgnd(CDC* pDC)
{
    CRect rectClient(0,0,0,0);
    GetClientRect(&rectClient);
    CBrush brush;
    brush.CreateSolidBrush(BACKCOLOR_BLUE);
    pDC->FillRect( &rectClient, &brush);
    return TRUE;
    //return CPropertyPage::OnEraseBkgnd(pDC);
}
 
HBRUSH COwnerDrawPropPage::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    //    HBRUSH hbr = CPropertyPage::OnCtlColor(pDC, pWnd, nCtlColor);
    //    return hbr;
 
    pDC->SetBkColor(BACKCOLOR_BLUE);
    pDC->SetBkMode(TRANSPARENT);
    HBRUSH hbrushBack = ::CreateSolidBrush(BACKCOLOR_BLUE);
    return hbrushBack;      
}
    3)把所有属性页的父类都改为COwnerDrawPropPage.这样就改了属性页的颜色.
 
(2)改CPropertySheet的背景色, 这里包括两方面: 客户区的颜色, 标签的颜色.
1)改客户区的颜色跟改属性页的一样:
 
HBRUSH CMyPropertySheet::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
//    HBRUSH hbr = CPropertySheet::OnCtlColor(pDC, pWnd, nCtlColor);
//    return hbr;
    pDC->SetBkColor(BACKCOLOR_BLUE);
    pDC->SetBkMode(TRANSPARENT);
    HBRUSH hbrushBack = ::CreateSolidBrush(BACKCOLOR_BLUE);
    return hbrushBack;    
}
BOOL CMyPropertySheet::OnEraseBkgnd(CDC* pDC)
{
    CRect rectClient(0,0,0,0);
    GetClientRect(&rectClient);
    CBrush brush;
    brush.CreateSolidBrush(BACKCOLOR_BLUE);
    pDC->FillRect( &rectClient, &brush);
    return TRUE;
    //return CPropertySheet::OnEraseBkgnd(pDC);
}
 
2)改标签的就有一定难度了. 先从CTabCtrl派生一个COwnerDrawTabCtrl, 处理其WM_ERASEBKGND消息. 然后再实现其DrawItem方法,注意不是WM_DRAWITEM!! 而是 void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) .
 
BOOL COwnerDrawTabCtrl::OnEraseBkgnd(CDC* pDC)
{
    CRect rectClient(0,0,0,0);
    GetClientRect(&rectClient);
    pDC->FillRect( &rectClient, &m_brushBK);
    return TRUE;
    //return CTabCtrl::OnEraseBkgnd(pDC);
}
void COwnerDrawTabCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
 
    TCHAR szTabText[64]={0};
 
    TC_ITEM     tci;
    tci.mask        = TCIF_TEXT;
    tci.pszText     = szTabText;
    tci.cchTextMax  = sizeof(szTabText)-1;
 
    GetItem( lpDrawItemStruct->itemID, &tci);
    CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);    
    pDC->FillRect( &lpDrawItemStruct->rcItem, &m_brushBK);
    pDC->SetBkColor( BACKCOLOR_BLUE);
 
    if ((lpDrawItemStruct->itemState & ODS_SELECTED) && 
        (lpDrawItemStruct->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
    {
        //Make the color of text of the selected tab to be BLUE.
        pDC->SetTextColor(RGB( 17, 139, 1));
    }
    //! 文字的位置可能的偏移
    pDC->TextOut(lpDrawItemStruct->rcItem.left+4,
        lpDrawItemStruct->rcItem.top+4,
        tci.pszText,
        lstrlen(tci.pszText));     
}
现在, 万里长征就已经完成了一大截了,最后一步是为CMyPropertySheet 加入一个  COwnerDrawTabCtrl 成员变量, 然后在OnInitDialog() 加入子类化代码:
 
BOOL CMyPropertySheet::OnInitDialog() 
{
    BOOL bResult = CPropertySheet::OnInitDialog();
    //! 省略原有代码
 
    //! Jasmine 2011-11-08
    m_tabOwnerDraw.SubclassWindow( GetTabControl()->m_hWnd);
    m_tabOwnerDraw.ModifyStyle(0,TCS_OWNERDRAWFIXED);
    return bResult;
}
这样就把用CPropertySheet和CPropertyPage实现的属性框的背景颜色改好. 
Have a good time!
 
 

 

posted on 2022-12-07 21:42  wsk3q  阅读(56)  评论(0编辑  收藏  举报