15,979
社区成员
发帖
与我相关
我的任务
分享
void CRollPanelWnd::DrawImage(CDC *pDC, Image *pImg, const RECT &rect, BOOL bDefaultSize)
{
if (pImg == NULL || pImg->GetLastStatus() != Ok)
return;
int cx = rect.right - rect.left;
int cy = rect.bottom - rect.top;
if (bDefaultSize)
{
cx = (int)pImg->GetWidth();
cy = (int)pImg->GetHeight();
}
Graphics graphics(pDC->m_hDC);
graphics.DrawImage(pImg, rect.left, rect.top, cx, cy);
}
// 绘画按钮
void CRollPanelWnd::DrawButton(CDC *pDC, int nIndex, LPRECT lprc, BOOL *pbPress)
{
if (nIndex < 0 || nIndex >= m_lpPanelInfo->button.nButtonCount)
return;
LPPANELBUTTON lpButton = &m_lpPanelInfo->button.lpButtons[nIndex];
if (lpButton == NULL || (pbPress != NULL && *pbPress == lpButton->bPress))
return;
if (lprc == NULL) lprc = &lpButton->rect;
int cx = lprc->right - lprc->left;
int cy = lprc->bottom - lprc->top;
if (cx == 0 || cy == 0) return;
CRect rcItem(lprc);
rcItem.OffsetRect(-lprc->left, -lprc->top);
CBitmap bmp; bmp.CreateCompatibleBitmap(pDC, cx, cy);
CDC memDC; memDC.CreateCompatibleDC(pDC);
memDC.SelectObject(&bmp);
CFont *pOldFont = memDC.SelectObject(&m_lpPanelInfo->button.fontText);
if (m_lpPanelInfo->bkgnd.hDC != NULL)
memDC.BitBlt(0, 0, cx, cy, CDC::FromHandle(m_lpPanelInfo->bkgnd.hDC), lprc->left, lprc->top, SRCCOPY);
CSize sizeText = memDC.GetTextExtent(_T("焯"), 1);
CRect rcIcon(0, 0, 0, 0);
if (lpButton->pImg != NULL && lpButton->pImg->GetLastStatus() == Ok)
{
rcIcon.bottom = rcIcon.right = rcItem.Height();
if (rcIcon.bottom > rcIcon.right)
rcIcon.right = rcIcon.bottom = rcItem.Width();
if (!m_lpPanelInfo->button.bTile)
{
rcIcon.right = rcIcon.bottom = (rcIcon.Width() - (sizeText.cy + BUTTONTEXT_MARGIN));
rcIcon.OffsetRect((rcItem.Width() - rcIcon.Width()) / 2, 0);
}
}
// 画高亮背景
if (pbPress == NULL) pbPress = &lpButton->bPress;
if (*pbPress)
{
CBitmap bmp; bmp.CreateCompatibleBitmap(pDC, cx, cy);
CDC dcMem; dcMem.CreateCompatibleDC(pDC);
dcMem.SelectObject(&bmp);
dcMem.BitBlt(0, 0, cx, cy, &memDC, 0, 0, SRCCOPY);
CBrush br(RGB(0x7f, 0x9d, 0xb9));
dcMem.SelectObject(&br);
dcMem.RoundRect(0, 0, cx, cy, 10, 10);
BLENDFUNCTION bf = { AC_SRC_OVER };
bf.SourceConstantAlpha = 0x4c;
memDC.AlphaBlend(0, 0, cx, cy, &dcMem, 0, 0, cx, cy, bf);
if (!rcIcon.IsRectEmpty())
rcIcon.InflateRect(-5, -5);
}
lpButton->bPress = *pbPress;
////////////////////////////////////////////
// 把这行注释掉之后, 就不会发生我说的情况了
DrawImage(&memDC, lpButton->pImg, rcIcon);
if (lpButton->lpText != NULL)
{
memDC.SetBkMode(TRANSPARENT);
CRect rcText(rcItem);
if (!m_lpPanelInfo->button.bTile)
{
rcText.InflateRect(-BUTTONTEXT_MARGIN, 0);
rcText.top = rcIcon.bottom + BUTTONTEXT_MARGIN;
if (!rcIcon.IsRectEmpty() && (*pbPress))
rcText.top += 5;
rcText.OffsetRect(1, 1);
memDC.SetTextColor(RGB(0xcc, 0xcc, 0xcc));
memDC.DrawText(lpButton->lpText[0], -1, rcText, DT_VCENTER | DT_CENTER | DT_SINGLELINE | DT_END_ELLIPSIS);
rcText.OffsetRect(-1, -1);
memDC.SetTextColor(0);
memDC.DrawText(lpButton->lpText[0], -1, rcText, DT_VCENTER | DT_CENTER | DT_SINGLELINE | DT_END_ELLIPSIS);
}
else
{
rcText.left = rcIcon.right + 5;
if (!rcIcon.IsRectEmpty() && (*pbPress))
rcText.left += 5;
int nTextCount = lpButton->nTextCount;
// 计算按钮中可显示数量的文字行数
int nRowCount = ((rcText.Height() + BUTTONTEXT_MARGIN) / (sizeText.cy + BUTTONTEXT_MARGIN)); // 可显示数量
if (nRowCount > nTextCount) nRowCount = nTextCount;
int nTextHeight = (sizeText.cy + BUTTONTEXT_MARGIN) * nRowCount - BUTTONTEXT_MARGIN; // 实际显示高度
rcText.top = (rcItem.Height() - nTextHeight) / 2;
rcText.bottom = rcText.top + nTextHeight;
// 画文字阴影
rcText.OffsetRect(1, 1);
memDC.SetTextColor(RGB(0xcc, 0xcc, 0xcc));
memDC.DrawText(lpButton->lpText[0], -1, rcText, DT_LEFT | DT_TOP | DT_SINGLELINE | DT_END_ELLIPSIS);
rcText.OffsetRect(-1, -1);
memDC.SetTextColor(0);
memDC.DrawText(lpButton->lpText[0], -1, rcText, DT_LEFT | DT_TOP | DT_SINGLELINE | DT_END_ELLIPSIS);
memDC.SetTextColor(RGB(0x80, 0x80, 0x80));
int nOffsetY = sizeText.cy + BUTTONTEXT_MARGIN;
for (int i = 1; i < nRowCount; i++)
{
rcText.top += nOffsetY;
memDC.DrawText(lpButton->lpText[i], -1, rcText, DT_LEFT | DT_TOP | DT_SINGLELINE | DT_END_ELLIPSIS);
}
}
}
memDC.SelectObject(pOldFont);
pDC->BitBlt(lprc->left, lprc->top, cx, cy, &memDC, 0, 0, SRCCOPY);
}