|
//重绘按钮
void CButtonST::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
CDC* pDC = CDC::FromHandle(lpDIS->hDC);
TRACE(L"DRAWITEM\r\n");
CPen *pOldPen;
BOOL bIsPressed = (lpDIS->itemState & ODS_SELECTED);
BOOL bIsFocused = (lpDIS->itemState & ODS_FOCUS);
BOOL bIsDisabled = (lpDIS->itemState & ODS_DISABLED);
CRect itemRect = lpDIS->rcItem;
if ((m_bIsFlat == FALSE) && (m_bDrawBorder == TRUE))
{
if (bIsFocused)
{
CBrush br(RGB(0,0,0));
pDC->FrameRect(&itemRect, &br);
itemRect.DeflateRect(1, 1);
}
//准备绘画,用背景颜色填充背景区域
COLORREF bgColor;
if ((m_MouseOnButton == TRUE) || (bIsPressed))
{
bgColor = GetActiveBgColor();
}
else
{
bgColor = GetInactiveBgColor();
}
CBrush br(bgColor);
pDC->FillRect(&itemRect, &br);
//画按下的按钮
if(bIsPressed)
{
TRACE(L"PRESSED\r\n");
if (m_bIsFlat == TRUE)
{
if (m_bDrawBorder == TRUE)
{
CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_HIGHLIGHT)); // Bianco
CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW)); // Grigio scuro
//话边框
//深灰线
pOldPen = pDC->SelectObject(&penBtnShadow);
pDC->MoveTo(itemRect.left, itemRect.bottom-1);
pDC->LineTo(itemRect.left, itemRect.top);
pDC->LineTo(itemRect.right, itemRect.top);
//白线
pDC->SelectObject(penBtnHiLight);
pDC->MoveTo(itemRect.left, itemRect.bottom-1);
pDC->LineTo(itemRect.right-1, itemRect.bottom-1);
pDC->LineTo(itemRect.right-1, itemRect.top-1);
//恢复画笔
pDC->SelectObject(pOldPen);
}
}
else
{
CBrush brBtnShadow(GetSysColor(COLOR_BTNSHADOW));
pDC->FrameRect(&itemRect, &brBtnShadow);
}
}
else //画未按下的按钮
{
CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_HIGHLIGHT)); //白
CPen pen3DLight(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT)); //浅灰
CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW)); //深灰
CPen pen3DDKShadow(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW)); //黑
if (m_bIsFlat == TRUE)
{
if (m_MouseOnButton == TRUE && m_bDrawBorder == TRUE)
{
//画边框
//白线
pOldPen = pDC->SelectObject(&penBtnHiLight);
pDC->MoveTo(itemRect.left, itemRect.bottom-1);
pDC->LineTo(itemRect.left, itemRect.top);
pDC->LineTo(itemRect.right, itemRect.top);
//深灰线
pDC->SelectObject(penBtnShadow);
pDC->MoveTo(itemRect.left, itemRect.bottom-1);
pDC->LineTo(itemRect.right-1, itemRect.bottom-1);
pDC->LineTo(itemRect.right-1, itemRect.top-1);
//恢复画笔
pDC->SelectObject(pOldPen);
}
}
else
{
//白线
pOldPen = pDC->SelectObject(&penBtnHiLight);
pDC->MoveTo(itemRect.left, itemRect.bottom-1);
pDC->LineTo(itemRect.left, itemRect.top);
pDC->LineTo(itemRect.right, itemRect.top);
//浅灰线
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);
//黑线
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);
//深灰线
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);
}
}
}
//读取按钮字符
CString sTitle;
GetWindowText(sTitle);
//如果不想显示字符
if (m_bShowText == FALSE)
{
sTitle.Empty();
}
CRect captionRect = lpDIS->rcItem;
captionRect.top += captionRect.Height()/2-8;
//画图标
if (m_hIconIn != NULL || m_hBmpIn.m_hObject )
{
DrawTheIcon(pDC, &sTitle, &lpDIS->rcItem, &captionRect, bIsPressed, bIsDisabled);
}
//如果有的话,写上字符
if (sTitle.IsEmpty() == FALSE)
{
if (bIsPressed)
{
captionRect.OffsetRect(1,1);
}
if ((m_MouseOnButton == TRUE) || (bIsPressed))
{
pDC->SetTextColor(GetActiveFgColor());
pDC->SetBkColor(GetActiveBgColor());
}
else
{
pDC->SetTextColor(GetInactiveFgColor());
pDC->SetBkColor(GetInactiveBgColor());
}
//文本居中
//CRect centerRect = captionRect;
//pDC->DrawText(sTitle, sTitle.GetLength(), captionRect, NULL);//DT_SINGLELINE|DT_CALCRECT);
//captionRect.OffsetRect((centerRect.Width() - captionRect.Width())/2, (centerRect.Height() - captionRect.Height())/2);
int startx,starty;
startx=1;
starty=1;
if (bIsPressed)
{
startx++;
starty++;
}
CRect drawRect;
int nTextAlign=0;
switch(m_nAlign)
{
default:
case 0:
drawRect.left =startx+m_IconSize.x+1;
drawRect.top =starty+(m_ButtonSize.y-m_nFontHeight)/2;
drawRect.right =drawRect.left+m_ButtonSize.x-2;
drawRect.bottom=drawRect.top+(m_ButtonSize.y-2);
break;
case 1:
drawRect.left =startx+1;
drawRect.top =starty+m_IconSize.y+1;
drawRect.right =m_ButtonSize.x-2;
drawRect.bottom=drawRect.top+(m_ButtonSize.y-m_IconSize.y-2);
nTextAlign=1;
break;
}
int nOld=pDC->SetBkMode(TRANSPARENT);
pDC->DrawText(sTitle, sTitle.GetLength(), drawRect, nTextAlign);
pDC->SetBkMode(nOld);
}
if (m_bIsFlat == FALSE || (m_bIsFlat == TRUE && m_bDrawFlatFocus == TRUE))
{
//画焦点边框
if (bIsFocused && m_bDrawBorder)
{
CRect focusRect = itemRect;
focusRect.DeflateRect(3, 3);
pDC->DrawFocusRect(&focusRect);
}
}
}
void CButtonST::DrawTheIcon(CDC* pDC, CString* title, RECT* rcItem, CRect* captionRect, BOOL IsPressed, BOOL IsDisabled)
{
CRect iconRect = rcItem;
switch (m_nAlign)
{
default:
case 0:
if (title->IsEmpty())
{
//图标水平居中
iconRect.left += ((iconRect.Width() - m_IconSize.x)/2);
}
else
{
iconRect.left += 3;
captionRect->left += m_IconSize.x+ 3;
}
//图标竖直居中
iconRect.top += ((iconRect.Height() - m_IconSize.y)/2);
break;
case 1:
//图标水平居中
iconRect.left += ((iconRect.Width() - m_IconSize.x)/2);
if (title->IsEmpty())
{
//图标竖直居中
iconRect.top += ((iconRect.Height() - m_IconSize.y)/2);
}
else
{
captionRect->top += m_IconSize.y;
}
break;
}
//如果按钮按下,图标也要按下
if (IsPressed)
{
iconRect.OffsetRect(1,1);
}
//Ole
if(!m_bBmpButton)
{
pDC->DrawIcon(iconRect.TopLeft().x,
iconRect.TopLeft().y,
(m_MouseOnButton == TRUE || IsPressed) ? m_hIconIn : m_hIconOut);
AfxMessageBox(L"test");
}
else
{
CDC* pMemDC = new CDC;
pMemDC -> CreateCompatibleDC(pDC);
int startx,starty;
startx=2;
starty=2;
if (IsPressed)
{
startx++;
starty++;
}
//保存旧对象
CBitmap * pOldBitmap;
if(m_MouseOnButton == TRUE || IsPressed||IsDisabled)
pOldBitmap = pMemDC -> SelectObject(&m_hBmpIn);
else
pOldBitmap = pMemDC -> SelectObject(&m_hBmpOut);
CString strText;
GetWindowText(strText);
if(m_bShowText&&!strText.IsEmpty())
{
switch(m_nAlign)
{
default:
case 0:
pDC->BitBlt(startx,starty,m_ButtonSize.x,m_ButtonSize.y,pMemDC,0,0,SRCCOPY);
break;
case 1:
pDC->BitBlt(startx+(m_ButtonSize.x-m_IconSize.x)/2,starty+1,m_ButtonSize.x,m_ButtonSize.y,pMemDC,0,0,SRCCOPY);
break;
}
}
else
pDC->BitBlt(starty,starty,m_ButtonSize.x,m_ButtonSize.y,pMemDC,0,0,SRCCOPY);
pMemDC -> SelectObject(pOldBitmap);
delete pMemDC;
}
} |
|