void CDiaColorD::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	CBrush brush;
	if(lpDrawItemStruct->CtlID==IDC_COLORON){
		brush.CreateSolidBrush(m_ColorOn);
	}else{
		brush.CreateSolidBrush(m_ColorOff);
	}

	CPen pen;
	pen.CreateStockObject(NULL_PEN);
	HPEN hOldPen=(HPEN)SelectObject(lpDrawItemStruct->hDC,pen.GetSafeHandle());
	HBRUSH hOldBrush=(HBRUSH)SelectObject(lpDrawItemStruct->hDC,brush.GetSafeHandle());
	Rectangle(lpDrawItemStruct->hDC,0,0,lpDrawItemStruct->rcItem.right,lpDrawItemStruct->rcItem.bottom);

	SelectObject(lpDrawItemStruct->hDC,hOldBrush);
	SelectObject(lpDrawItemStruct->hDC,hOldPen);

	CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}
void COptionTreeColorPopUp::DrawCell(CDC* pDC, int nIndex)
{
    // For the Custom Text area
    if (m_strCustomText.GetLength() && nIndex == OT_COLOR_CUSTOMBOXVALUE)
    {
        // -- The extent of the actual text button
        CRect rcTextButton = m_rcCustomTextRect;
        rcTextButton.top += 2 * m_nMargin;

        // -- Fill background
        pDC->FillSolidRect(rcTextButton, ::GetSysColor(COLOR_3DFACE));

        // -- Draw horizontal line
        pDC->FillSolidRect(m_rcCustomTextRect.left + 2 * m_nMargin, m_rcCustomTextRect.top, m_rcCustomTextRect.Width()-4*m_nMargin, 1, ::GetSysColor(COLOR_3DSHADOW));
        pDC->FillSolidRect(m_rcCustomTextRect.left + 2 * m_nMargin, m_rcCustomTextRect.top + 1, m_rcCustomTextRect.Width()-4*m_nMargin, 1, ::GetSysColor(COLOR_3DHILIGHT));
        rcTextButton.DeflateRect(1,1);

        // -- Fill background
        if (m_nChosenColorSel == nIndex && m_nCurrentSel != nIndex)
		{
            pDC->FillSolidRect(rcTextButton, ::GetSysColor(COLOR_3DLIGHT));
		}
        else
		{
            pDC->FillSolidRect(rcTextButton, ::GetSysColor(COLOR_3DFACE));
		}

        // -- Draw button
        if (m_nCurrentSel == nIndex) 
		{
            pDC->DrawEdge(rcTextButton, BDR_RAISEDINNER, BF_RECT);
		}

        // -- Draw custom text
        CFont *pOldFont = (CFont*) pDC->SelectObject(&m_fFont);
        int nOldBack = pDC->SetBkMode(TRANSPARENT);
        pDC->DrawText(m_strCustomText, rcTextButton, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

		// -- Restore
        pDC->SelectObject(pOldFont);
		pDC->SetBkMode(nOldBack);

        return;
    }        

    // For the Default Text area
    if (m_strDefaultText.GetLength() && nIndex == OT_COLOR_DEFAULTBOXVALUE)
    {
        // -- Fill background
        pDC->FillSolidRect(m_rcDefaultTextRect, ::GetSysColor(COLOR_3DFACE));

        // -- The extent of the actual text button
        CRect rcTextButton = m_rcDefaultTextRect;
        rcTextButton.DeflateRect(1,1);

        // -- Fill background
        if (m_nChosenColorSel == nIndex && m_nCurrentSel != nIndex)
		{
			for (long i = rcTextButton.top; i < rcTextButton.bottom; i++)
			{
				_DrawSelectRect(pDC->GetSafeHdc(), rcTextButton.left, i, rcTextButton.Width());
			}
		}
        else
		{
            pDC->FillSolidRect(rcTextButton, ::GetSysColor(COLOR_3DFACE));
		}

        // -- Draw thin line around text
        CRect rcLineRect = rcTextButton;
        CPen pen(PS_SOLID, 1, ::GetSysColor(COLOR_3DSHADOW));
        CPen* pOldPen = pDC->SelectObject(&pen);
		
		// -- Calculate the rectangle
		rcLineRect.left += 3;
		rcLineRect.right -= 3;
		rcLineRect.top += 2;
		rcLineRect.bottom -= 3;

		// -- Restore
        pDC->SelectStockObject(NULL_BRUSH);
        pDC->Rectangle(rcLineRect);
        pDC->SelectObject(pOldPen);
		if (pen.GetSafeHandle() != NULL)
		{
			pen.DeleteObject();
		}

        // -- Draw button
        if (m_nCurrentSel == nIndex) 
		{
            pDC->DrawEdge(rcTextButton, BDR_RAISEDINNER, BF_RECT);
		}
        else if (m_nChosenColorSel == nIndex)
		{
            pDC->DrawEdge(rcTextButton, BDR_SUNKENOUTER, BF_RECT);
		}

        // -- Draw custom text
        CFont *pOldFont = (CFont*) pDC->SelectObject(&m_fFont);
        int nOldBack = pDC->SetBkMode(TRANSPARENT);
        pDC->DrawText(m_strDefaultText, rcTextButton, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

		// -- Restore
        pDC->SelectObject(pOldFont);
		pDC->SetBkMode(nOldBack);

		// -- Select and realize the palette
		CPalette* pOldPalette = NULL;
		if (pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
		{
			pOldPalette = pDC->SelectPalette(&m_plPalette, FALSE);
			pDC->RealizePalette();
		}

		// -- Draw sample cell of default color
		CRect rcSample;
		rcSample.left = rcLineRect.left;
		rcSample.right = rcSample.left + m_nBoxSize;
		rcSample.top = rcLineRect.top;
		rcSample.bottom = rcLineRect.top + m_nBoxSize;
		rcSample.DeflateRect(m_nMargin + 1, m_nMargin + 1);

		// -- Create objects
		CBrush brush(PALETTERGB(GetRValue(m_crDefault), GetGValue(m_crDefault), GetBValue(m_crDefault)));
		CPen penSample;
		penSample.CreatePen(PS_SOLID, 1, ::GetSysColor(COLOR_3DSHADOW));
		CBrush* pOldBrush = (CBrush*) pDC->SelectObject(&brush);
		CPen* pOldSamplePen = (CPen*) pDC->SelectObject(&penSample);

		// Draw the cell color
		pDC->Rectangle(rcSample);

		// Restore
		pDC->SelectObject(pOldBrush);
		pDC->SelectObject(pOldSamplePen);
		if (brush.GetSafeHandle() != NULL)
		{
			brush.DeleteObject();
		}
		if (penSample.GetSafeHandle() != NULL)
		{
			penSample.DeleteObject();
		}
		if (pOldPalette && pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
		{
			pDC->SelectPalette(pOldPalette, FALSE);
		}
        return;
    }        

	// Declare variables
    CRect rcCell;
    if (!GetCellRect(nIndex, rcCell)) 
	{
		return;
	}

    // -- Select and realize the palette
    CPalette* pOldPalette = NULL;
    if (pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
    {
        pOldPalette = pDC->SelectPalette(&m_plPalette, FALSE);
        pDC->RealizePalette();
    }

    // -- Fill background
    if (m_nChosenColorSel == nIndex && m_nCurrentSel != nIndex)
	{
		for (long i = rcCell.top; i < rcCell.bottom; i++)
		{
			_DrawSelectRect(pDC->GetSafeHdc(), rcCell.left, i, rcCell.Width());
		}
	}
    else
	{
        pDC->FillSolidRect(rcCell, ::GetSysColor(COLOR_3DFACE));
	}

    // Draw button
    if (m_nCurrentSel == nIndex) 
	{
        pDC->DrawEdge(rcCell, BDR_RAISEDINNER, BF_RECT);
	}
    else if (m_nChosenColorSel == nIndex)
	{
        pDC->DrawEdge(rcCell, BDR_SUNKENOUTER, BF_RECT);
	}

	// Create objects
    CBrush brush(PALETTERGB(GetRValue(GetColor(nIndex)), GetGValue(GetColor(nIndex)), GetBValue(GetColor(nIndex))));
	CPen pen;
    pen.CreatePen(PS_SOLID, 1, ::GetSysColor(COLOR_3DSHADOW));
    CBrush* pOldBrush = (CBrush*) pDC->SelectObject(&brush);
    CPen* pOldPen = (CPen*) pDC->SelectObject(&pen);

    // Draw the cell color
    rcCell.DeflateRect(m_nMargin + 1, m_nMargin + 1);
    pDC->Rectangle(rcCell);

    // Restore
    pDC->SelectObject(pOldBrush);
    pDC->SelectObject(pOldPen);
	if (brush.GetSafeHandle() != NULL)
	{
		brush.DeleteObject();
	}
	if (pen.GetSafeHandle() != NULL)
	{
		pen.DeleteObject();
	}
    if (pOldPalette && pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
	{
        pDC->SelectPalette(pOldPalette, FALSE);
	}
}