void CHideButton::DrawControl(CDC &dc, CRect rcUpdate) { int nWidth = m_rc.Width(); int nHeight = m_rc.Height(); if(!m_bUpdate) { UpdateMemDC(dc, nWidth * 5, nHeight); for(int i = 0; i < 4; i++) { m_memDC.BitBlt(i * nWidth, 0, nWidth, nHeight, &dc, m_rc.left ,m_rc.top, SRCCOPY); } Color clrText[4] = {m_clrTextNormal, m_clrTextHover, m_clrTextDown, m_clrTextDisable}; Graphics graphics(m_memDC); BSTR bsFont = m_strFont.AllocSysString(); FontFamily fontFamily(bsFont); Font font(&fontFamily, (REAL)m_nFontWidth, m_fontStyle, UnitPixel); graphics.SetTextRenderingHint( TextRenderingHintClearTypeGridFit ); ::SysFreeString(bsFont); // 设置水平和垂直对齐方式 DUI_STRING_ALIGN_DEFINE(); strFormat.SetFormatFlags( StringFormatFlagsNoWrap | StringFormatFlagsMeasureTrailingSpaces); Size sizeTip = GetTextBounds(font, strFormat, m_strTip); Size sizeText = GetTextBounds(font, strFormat, m_strTitle); m_rcText.SetRect(m_rc.left + sizeTip.Width + 10, m_rc.top, m_rc.left + sizeTip.Width + 10 + sizeText.Width, m_rc.top + sizeText.Height); SolidBrush solidBrushTip(m_clrTip); for(int i = 0; i < 4; i++) { RectF rect((Gdiplus::REAL)(i * nWidth), (Gdiplus::REAL)0, (Gdiplus::REAL)(sizeTip.Width + 10), (Gdiplus::REAL)sizeTip.Height); BSTR bsTip = m_strTip.AllocSysString(); graphics.DrawString(bsTip, (INT)wcslen(bsTip), &font, rect, &strFormat, &solidBrushTip); ::SysFreeString(bsTip); if(i > 0) { SolidBrush solidBrush(clrText[i - 1]); RectF rect((Gdiplus::REAL)(sizeTip.Width + 10 + i * nWidth), (Gdiplus::REAL)0, (Gdiplus::REAL)(nWidth - (sizeTip.Width + 10)), (Gdiplus::REAL)sizeText.Height); BSTR bsTitle = m_strTitle.AllocSysString(); graphics.DrawString(bsTitle, (INT)wcslen(bsTitle), &font, rect, &strFormat, &solidBrush); ::SysFreeString(bsTitle); } } } dc.BitBlt(m_rc.left,m_rc.top, m_rc.Width(), m_rc.Height(), &m_memDC, m_bShowButton ? (1 + m_enButtonState) * nWidth : 0, 0, SRCCOPY); }
void CDuiEdit::DrawControl(CDC &dc, CRect rcUpdate) { Graphics graphics(dc); DrawImageFrame(graphics, m_pImage, m_rc, m_EditState * m_sizeImage.cx, 0, m_sizeImage.cx, m_sizeImage.cy, 4); if(m_pLeftImage) { CRect rc; rc.left = m_rc.left + 2; rc.top = m_rc.top + (m_rc.Height() - m_sizeLeftImage.cy) / 2; rc.right = rc.left + m_sizeLeftImage.cx; rc.bottom = rc.top + m_sizeLeftImage.cy; if(m_nLeftImageCount > m_buttonState) { graphics.DrawImage(m_pLeftImage, RectF((Gdiplus::REAL)rc.left , (Gdiplus::REAL)rc.top, (Gdiplus::REAL)rc.Width(), (Gdiplus::REAL)rc.Height()), (Gdiplus::REAL)(m_buttonState * m_sizeLeftImage.cx), 0, (Gdiplus::REAL)m_sizeLeftImage.cx, (Gdiplus::REAL)m_sizeLeftImage.cy, UnitPixel); }else { graphics.DrawImage(m_pLeftImage, RectF((Gdiplus::REAL)rc.left , (Gdiplus::REAL)rc.top, (Gdiplus::REAL)rc.Width(), (Gdiplus::REAL)rc.Height()), 0, 0, (Gdiplus::REAL)m_sizeLeftImage.cx, (Gdiplus::REAL)m_sizeLeftImage.cy, UnitPixel); } } if(m_pSmallImage) { CRect rc; rc.left = m_rc.right - m_sizeSmallImage.cx - 2; rc.top = m_rc.top + (m_rc.Height() - m_sizeSmallImage.cy) / 2; rc.right = rc.left + m_sizeSmallImage.cx; rc.bottom = rc.top + m_sizeSmallImage.cy; if(m_nSmallImageCount > m_buttonState) { graphics.DrawImage(m_pSmallImage, RectF((Gdiplus::REAL)rc.left , (Gdiplus::REAL)rc.top, (Gdiplus::REAL)rc.Width(), (Gdiplus::REAL)rc.Height()), (Gdiplus::REAL)(m_buttonState * m_sizeSmallImage.cx), 0, (Gdiplus::REAL)m_sizeSmallImage.cx, (Gdiplus::REAL)m_sizeSmallImage.cy, UnitPixel); }else { graphics.DrawImage(m_pSmallImage, RectF((Gdiplus::REAL)rc.left , (Gdiplus::REAL)rc.top, (Gdiplus::REAL)rc.Width(), (Gdiplus::REAL)rc.Height()), 0, 0, (Gdiplus::REAL)m_sizeSmallImage.cx, (Gdiplus::REAL)m_sizeSmallImage.cy, UnitPixel); } } BSTR bsFont = m_strFont.AllocSysString(); FontFamily fontFamily(bsFont); Font font(&fontFamily, (REAL)m_nFontWidth, m_fontStyle, UnitPixel); SolidBrush solidBrush(m_clrText); SolidBrush solidBrushTip(m_clrTooltip); graphics.SetTextRenderingHint( TextRenderingHintClearTypeGridFit ); ::SysFreeString(bsFont); StringFormat strFormat; strFormat.SetAlignment(StringAlignmentNear); // 水平方向左对齐 if(!m_bMultiLine) { // 单行文字 strFormat.SetLineAlignment(StringAlignmentCenter); // 垂直方向中间对齐 strFormat.SetFormatFlags( StringFormatFlagsNoWrap | StringFormatFlagsMeasureTrailingSpaces); }else { strFormat.SetLineAlignment(StringAlignmentNear); // 垂直方向上对齐 } RectF rect((Gdiplus::REAL)m_rcText.left, (Gdiplus::REAL)(m_rcText.top+2), (Gdiplus::REAL)m_rcText.Width(), (Gdiplus::REAL)(m_rcText.Height()-2)); if(!m_strTitle.IsEmpty()) { // 文字非空 CString strTitle = m_strTitle; if(m_bPassWord) { int nlen = strTitle.GetLength(); strTitle = ""; for(int i = 0; i < nlen; i++) { strTitle += '*'; } } BSTR bsTitle = strTitle.AllocSysString(); graphics.DrawString(bsTitle, (INT)wcslen(bsTitle), &font, rect, &strFormat, &solidBrush); ::SysFreeString(bsTitle); }else if(!m_strTooltip.IsEmpty()) { // 如果没有文字,但设置了tooltip,则显示tooltip BSTR bsTooltip = m_strTooltip.AllocSysString(); graphics.DrawString(bsTooltip, (INT)wcslen(bsTooltip), &font, rect, &strFormat, &solidBrushTip); ::SysFreeString(bsTooltip); } }