想在一个界面里添加几个Bitmaps的按钮,选择了一个扩展类,CButtonST,这个类应该用的人挺多的,用起来还不错,不过有一点小问题,如何取出按钮的3D效果?有3D效果看起来我的按钮很别扭!图片上下边缘在按下去的时候有阴影,我向去掉这个3D效果应该怎么做???还有一个问题,目前用CButtonST这个类只能贴两个状态的图片,一个是常态的,一个是鼠标经过的,能不能添加最后点击过的(或者叫激活的)状态?就像TAB控件那样最后点击的按钮状态和其他的不一样?谢谢大家了
调试欢乐多
{
CDC* pDC = CDC::FromHandle(lpDIS->hDC);
CPen* pOldPen; // Checkbox?
if (m_bIsCheckBox)
{
m_bIsPressed = (lpDIS->itemState & ODS_SELECTED) || (m_nCheck != 0);
} // if
else // Normal button OR other button style ...
{
m_bIsPressed = (lpDIS->itemState & ODS_SELECTED); // If there is a menu and it's displayed, draw the button as pressed
if (
#ifdef BTNST_USE_BCMENU
m_menuPopup.m_hMenu
#else
m_hMenu
#endif
&& m_bMenuDisplayed) m_bIsPressed = TRUE;
} // else m_bIsFocused = (lpDIS->itemState & ODS_FOCUS);
m_bIsDisabled = (lpDIS->itemState & ODS_DISABLED); CRect itemRect = lpDIS->rcItem; pDC->SetBkMode(TRANSPARENT); if (m_bIsFlat == FALSE)
{
if (m_bIsFocused || m_bIsDefault)
{
CBrush br(RGB(0,0,0));
pDC->FrameRect(&itemRect, &br);
itemRect.DeflateRect(1, 1);
} // if
} // if // Prepare draw... paint button background // Draw transparent?
if (m_bDrawTransparent)
PaintBk(pDC);
else
OnDrawBackground(pDC, &itemRect); // Draw pressed button
if (m_bIsPressed)
{
if (m_bIsFlat)
{
if (m_bDrawBorder)
OnDrawBorder(pDC, &itemRect);
}
else
{
CBrush brBtnShadow(GetSysColor(COLOR_BTNSHADOW));
pDC->FrameRect(&itemRect, &brBtnShadow);
}
}
else // ...else draw non pressed button
{
CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // White
CPen pen3DLight(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT)); // Light gray
CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW)); // Dark gray
CPen pen3DDKShadow(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW)); // Black if (m_bIsFlat)
{
if (m_bMouseOnButton && m_bDrawBorder)
OnDrawBorder(pDC, &itemRect);
}
else
{
// Draw top-left borders
// White line
pOldPen = pDC->SelectObject(&penBtnHiLight);
pDC->MoveTo(itemRect.left, itemRect.bottom-1);
pDC->LineTo(itemRect.left, itemRect.top);
pDC->LineTo(itemRect.right, itemRect.top);
// Light gray line
pDC->SelectObject(pen3DLight);
pDC->MoveTo(itemRect.left+1, itemRect.bottom-1);
pDC->LineTo(itemRect.left+1, itemRect.top+1);
pDC->LineTo(itemRect.right, itemRect.top+1);
// Draw bottom-right borders
// Black line
pDC->SelectObject(pen3DDKShadow);
pDC->MoveTo(itemRect.left, itemRect.bottom-1);
pDC->LineTo(itemRect.right-1, itemRect.bottom-1);
pDC->LineTo(itemRect.right-1, itemRect.top-1);
// Dark gray line
pDC->SelectObject(penBtnShadow);
pDC->MoveTo(itemRect.left+1, itemRect.bottom-2);
pDC->LineTo(itemRect.right-2, itemRect.bottom-2);
pDC->LineTo(itemRect.right-2, itemRect.top);
//
pDC->SelectObject(pOldPen);
} // else
} // else // Read the button's title
CString sTitle;
GetWindowText(sTitle);
// If we don't want the title displayed
if (m_bShowText == FALSE) sTitle.Empty();
CRect captionRect = lpDIS->rcItem; // Draw the icon
if (m_csIcons[0].hIcon)
{
DrawTheIcon(pDC, !sTitle.IsEmpty(), &lpDIS->rcItem, &captionRect, m_bIsPressed, m_bIsDisabled);
} // if if (m_csBitmaps[0].hBitmap)
{
pDC->SetBkColor(RGB(255,255,255));
DrawTheBitmap(pDC, !sTitle.IsEmpty(), &lpDIS->rcItem, &captionRect, m_bIsPressed, m_bIsDisabled);
} // if // Write the button title (if any)
if (sTitle.IsEmpty() == FALSE)
{
// Draw the button's title
// If button is pressed then "press" title also
if (m_bIsPressed && m_bIsCheckBox == FALSE)
captionRect.OffsetRect(1, 1);
// ONLY FOR DEBUG
//CBrush brBtnShadow(RGB(255, 0, 0));
//pDC->FrameRect(&captionRect, &brBtnShadow); // Center text
CRect centerRect = captionRect;
pDC->DrawText(sTitle, -1, captionRect, DT_WORDBREAK | DT_CENTER | DT_CALCRECT);
captionRect.OffsetRect((centerRect.Width() - captionRect.Width())/2, (centerRect.Height() - captionRect.Height())/2);
/* RFU
captionRect.OffsetRect(0, (centerRect.Height() - captionRect.Height())/2);
captionRect.OffsetRect((centerRect.Width() - captionRect.Width())-4, (centerRect.Height() - captionRect.Height())/2);
*/ pDC->SetBkMode(TRANSPARENT);
/*
pDC->DrawState(captionRect.TopLeft(), captionRect.Size(), (LPCTSTR)sTitle, (bIsDisabled ? DSS_DISABLED : DSS_NORMAL),
TRUE, 0, (CBrush*)NULL);
*/
if (m_bIsDisabled)
{
captionRect.OffsetRect(1, 1);
pDC->SetTextColor(::GetSysColor(COLOR_3DHILIGHT));
pDC->DrawText(sTitle, -1, captionRect, DT_WORDBREAK | DT_CENTER);
captionRect.OffsetRect(-1, -1);
pDC->SetTextColor(::GetSysColor(COLOR_3DSHADOW));
pDC->DrawText(sTitle, -1, captionRect, DT_WORDBREAK | DT_CENTER);
} // if
else
{
if (m_bMouseOnButton || m_bIsPressed)
{
pDC->SetTextColor(m_crColors[BTNST_COLOR_FG_IN]);
pDC->SetBkColor(m_crColors[BTNST_COLOR_BK_IN]);
} // if
else
{
pDC->SetTextColor(m_crColors[BTNST_COLOR_FG_OUT]);
pDC->SetBkColor(m_crColors[BTNST_COLOR_BK_OUT]);
} // else
pDC->DrawText(sTitle, -1, captionRect, DT_WORDBREAK | DT_CENTER);
} // if
} // if if (m_bIsFlat == FALSE || (m_bIsFlat && m_bDrawFlatFocus))
{
// Draw the focus rect
if (m_bIsFocused)
{
CRect focusRect = itemRect;
focusRect.DeflateRect(3, 3);
pDC->DrawFocusRect(&focusRect);
} // if
} // if
} // End of DrawItem