void CCheckButton::DrawControl(CDC &dc, CRect rcUpdate) { int nWidth = m_rc.Width(); int nHeight = m_rc.Height(); if(!m_bUpdate) { UpdateMemDC(dc, nWidth * 6, nHeight); Graphics graphics(m_memDC); CRect rcTemp(0, 0, nWidth, nHeight); for(int i = 0; i < 6; i++) { m_memDC.BitBlt(i * nWidth, 0, nWidth, nHeight, &dc, m_rc.left ,m_rc.top, SRCCOPY); graphics.DrawImage(m_pImage, Rect(rcTemp.left, rcTemp.top + (nHeight - m_sizeImage.cy) / 2, m_sizeImage.cx, m_sizeImage.cy), i * m_sizeImage.cx, 0, m_sizeImage.cx, m_sizeImage.cy, UnitPixel); rcTemp.OffsetRect(nWidth, 0); } if(!m_strTitle.IsEmpty()) { m_memDC.SetBkMode(TRANSPARENT); rcTemp.SetRect(0, 0, nWidth, nHeight); FontFamily fontFamily(m_strFont.AllocSysString()); Font font(&fontFamily, (REAL)m_nFontWidth, m_fontStyle, UnitPixel); graphics.SetTextRenderingHint( TextRenderingHintClearTypeGridFit ); StringFormat strFormat; strFormat.SetAlignment(StringAlignmentNear); strFormat.SetFormatFlags( StringFormatFlagsNoWrap | StringFormatFlagsMeasureTrailingSpaces); Size size = GetTextBounds(font, strFormat, m_strTitle); CPoint point = GetOriginPoint(nWidth - m_sizeImage.cx - 3, nHeight, size.Width, size.Height, m_uAlignment, m_uVAlignment); for(int i = 0; i < 6; i++) { SolidBrush solidBrush(enBSDisable == i ? Color(128, 128, 128) : m_clrText); RectF rect(m_sizeImage.cx + 3 + point.x + i * nWidth, point.y, nWidth - m_sizeImage.cx - 3 - point.x, size.Height); graphics.DrawString(m_strTitle.AllocSysString(), (INT)wcslen(m_strTitle.AllocSysString()), &font, rect, &strFormat, &solidBrush); // 画焦点框(虚线框) if(m_bIsFocus) { Pen pen(Color(128, 128, 128), 1); pen.SetDashStyle(DashStyleDot); RectF rectFocus(point.x + i * nWidth, point.y, m_sizeImage.cx + 6 + size.Width, size.Height); graphics.DrawRectangle(&pen, rectFocus); } } } } dc.BitBlt(m_rc.left,m_rc.top, m_rc.Width(), m_rc.Height(), &m_memDC, m_enButtonState * nWidth, 0, SRCCOPY); }
//////////////////////////////////////////////////////////////////////////////// // // FUNCTION: DrawItem // // DESCRIPTION: Called in response to draw the button // // NOTES: // // MAINTENANCE: // Name: Date: Version: Notes: // NT ALMOND 210100 1.0 Origin // //////////////////////////////////////////////////////////////////////////////// void CCoolBtn::DrawItem(DRAWITEMSTRUCT* lpDIS) { if (lpDIS->CtlType != ODT_BUTTON) return; CFont *pFont = CFont::FromHandle((HFONT)GetStockObject(DEFAULT_GUI_FONT)); CDC dcMem; CBitmap bmp; CRect btnRect(lpDIS->rcItem); CRect trueRect(btnRect); CDC *pDC = CDC::FromHandle(lpDIS->hDC); //////////////////////////////////////// // Button Background // //////////////////////////////////////// pDC->FillRect(trueRect,&CBrush(GetSysColor(COLOR_BTNFACE))); BOOL bDisabled = ODS_DISABLED & lpDIS->itemState; if (m_bDefaultBtn) btnRect.DeflateRect(1,1); CRect rectFocus(btnRect); rectFocus.DeflateRect(4,4); if (!m_bMenuPushed) rectFocus.OffsetRect(m_bPushed,m_bPushed); rectFocus.right -= nDropBtnWidth; //////////////////////////////////////// // Button in a normal state // //////////////////////////////////////// if (!m_bPushed || m_bMenuPushed) { #ifdef _VISUALSTYLE_XP_H_ if (g_xpStyle.IsAppThemed()) { HTHEME hTheme = g_xpStyle.OpenThemeData(GetSafeHwnd(), L"BUTTON"); g_xpStyle.DrawThemeBackground(hTheme, pDC->GetSafeHdc(), BP_PUSHBUTTON, PBS_NORMAL, &btnRect, 0); g_xpStyle.CloseThemeData(hTheme); } else { #endif pDC->DrawFrameControl(&btnRect,DFC_BUTTON,DFCS_BUTTONPUSH); #ifdef _VISUALSTYLE_XP_H_ } #endif } //////////////////////////////////////// // Default Button State // //////////////////////////////////////// if ((m_bDefaultBtn || m_bPushed) && !bDisabled) { if (m_bPushed && !m_bMenuPushed) { #ifdef _VISUALSTYLE_XP_H_ if (g_xpStyle.IsAppThemed()) { HTHEME hTheme = g_xpStyle.OpenThemeData(GetSafeHwnd(), L"BUTTON"); g_xpStyle.DrawThemeBackground(hTheme, pDC->GetSafeHdc(), BP_PUSHBUTTON, PBS_PRESSED, &btnRect, 0); g_xpStyle.CloseThemeData(hTheme); } else { #endif pDC->FrameRect(&lpDIS->rcItem,CBrush::FromHandle((HBRUSH)GetStockObject(BLACK_BRUSH))); pDC->FrameRect(&btnRect,CBrush::FromHandle((HBRUSH)GetStockObject(BLACK_BRUSH))); #ifdef _VISUALSTYLE_XP_H_ } #endif } } //////////////////////////////////////// // State Hover // //////////////////////////////////////// if (m_bOverControl /*lpDIS->itemState & ODS_HOTLIGHT*/) if (!m_bMenuPushed) { #ifdef _VISUALSTYLE_XP_H_ if (g_xpStyle.IsAppThemed()) { HTHEME hTheme = g_xpStyle.OpenThemeData(GetSafeHwnd(), L"BUTTON"); g_xpStyle.DrawThemeBackground(hTheme, pDC->GetSafeHdc(), BP_PUSHBUTTON, PBS_HOT, &btnRect, 0); g_xpStyle.CloseThemeData(hTheme); } #endif } //////////////////////////////////////// // State Focus // //////////////////////////////////////// if (lpDIS->itemState & ODS_FOCUS || m_bPushed) if (!m_bMenuPushed) pDC->DrawFocusRect(&rectFocus); //////////////////////////////////////// // Action Focus // //////////////////////////////////////// if ((lpDIS->itemAction & ODA_FOCUS)) if (!m_bMenuPushed) pDC->DrawFocusRect(&rectFocus); //////////////////////////////////////// // Draw out bitmap // //////////////////////////////////////// // Draw out bitmap if (m_bLoaded) { if (!bDisabled) { m_IL.DrawIndirect(pDC,0,CPoint(6+m_bPushed,6+m_bPushed), CSize(m_bm.bmWidth, m_bm.bmHeight), CPoint(0,0),ILD_NORMAL); } else { pDC->DrawState(CPoint(6+m_bPushed,6+m_bPushed), CSize(m_bm.bmWidth, m_bm.bmHeight), m_hbmpDisabled, DST_BITMAP | DSS_DISABLED); } } //////////////////////////////////////// // Draw out text // //////////////////////////////////////// pDC->SelectObject(pFont); CRect rectText(rectFocus); rectFocus.left += m_bm.bmWidth + 2; CString strCaption; GetWindowText(strCaption); pDC->SetBkMode(TRANSPARENT); pDC->SetBkColor(GetSysColor(COLOR_BTNFACE)); if (ODS_DISABLED & lpDIS->itemState) { rectFocus.OffsetRect(1,1); pDC->SetTextColor(GetSysColor(COLOR_WINDOW)); pDC->DrawText(strCaption,rectFocus,DT_SINGLELINE|DT_CENTER|DT_VCENTER); rectFocus.OffsetRect(-1,-1); pDC->SetTextColor(GetSysColor(COLOR_GRAYTEXT)); pDC->DrawText(strCaption,rectFocus,DT_SINGLELINE|DT_CENTER|DT_VCENTER); } else { pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT)); pDC->DrawText(strCaption,rectFocus,DT_SINGLELINE|DT_CENTER|DT_VCENTER); } CRect rectSplit(btnRect); rectSplit.DeflateRect(2,2); rectSplit.right -= nDropBtnWidth; //////////////////////////////////////// // Drop down split // //////////////////////////////////////// CPen brFace(PS_SOLID,1,GetSysColor(COLOR_3DSHADOW)); pDC->SelectObject(&brFace); pDC->MoveTo(rectSplit.right, rectSplit.top); pDC->LineTo(rectSplit.right, rectSplit.bottom); CPen brLite(PS_SOLID,1,GetSysColor(COLOR_3DHILIGHT)); pDC->SelectObject(&brLite); pDC->MoveTo(rectSplit.right+1 , rectSplit.top); pDC->LineTo(rectSplit.right+1, rectSplit.bottom); rectSplit.left = rectSplit.right; rectSplit.right += nDropBtnWidth; CPoint pt(rectSplit.CenterPoint()); pt += CPoint(m_bPushed,m_bPushed); CPen penBlack(PS_SOLID, 1, bDisabled ? GetSysColor(COLOR_GRAYTEXT) : GetSysColor(COLOR_WINDOWTEXT)); pDC->SelectObject(&penBlack); DrawArrow(pDC,pt); //////////////////////////////////////// // Drop down state // //////////////////////////////////////// if (m_bMenuPushed && !bDisabled) { #ifdef _VISUALSTYLE_XP_H_ rectSplit.DeflateRect(1,1); #else rectSplit.InflateRect(1,1); #endif pDC->DrawEdge(rectSplit,BDR_SUNKENOUTER, BF_RECT); } }
void CXFMenuButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { // -[Feral]----------------------------------------------------------- // VALIDATION: ASSERT(lpDrawItemStruct != NULL); // -[Feral]----------------------------------------------------------- // Vars and Assignment CRect rect = lpDrawItemStruct->rcItem; CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC); UINT uiState = lpDrawItemStruct->itemState; CPen pen; CPen *ppenOld = NULL; // -[Feral]----------------------------------------------------------- // set the pen color based on if we are disabled or not if( (uiState&ODS_DISABLED) ) { pen.CreatePen(PS_SOLID, 0, ::GetSysColor(COLOR_GRAYTEXT) ); } else { pen.CreatePen(PS_SOLID, 0, ::GetSysColor(COLOR_BTNTEXT) ); } // -[Feral]----------------------------------------------------------- // select the pen into the DC. ppenOld = pDC->SelectObject(&pen); // -[Feral]----------------------------------------------------------- // draw the border if( (uiState&ODS_SELECTED) ) { pDC->DrawFrameControl(rect, DFC_BUTTON, DFCS_BUTTONPUSH|DFCS_PUSHED); } else { pDC->DrawFrameControl(rect, DFC_BUTTON, DFCS_BUTTONPUSH); } // -[Feral]----------------------------------------------------------- // Draw the Arrow... // Something like: // X // XX // XXX // XXXX // XXX // XX // X // In the Center of the button. CSize sizeArrow(4,7); CSize sizeOffsetTop(1,+1); // size of the top stairsteps CSize sizeOffsetBottom(1,-1); // size of the bottom stairsteps CPoint pointCenter( (rect.Width()/2), (rect.Height()/2) ); CPoint pointStart( (pointCenter.x-(sizeArrow.cx/2) ), (pointCenter.y-(sizeArrow.cy/2) ) ); CPoint pointStop ( (pointStart.x), (pointStart.y+sizeArrow.cy) ); // -[Feral]----------------------------------------------------------- // start at the left and work to the right... for(int iInd=0; iInd<sizeArrow.cx; iInd++) { // -[Feral]------------------------------------------------------- // draw the lines pDC->MoveTo(pointStart); pDC->LineTo(pointStop); // -[Feral]------------------------------------------------------- // offset our points. (going right!) pointStart.Offset(sizeOffsetTop); pointStop .Offset(sizeOffsetBottom); } // -[Feral]----------------------------------------------------------- // Draw the focus rectangle if necessary. if( (uiState&ODS_FOCUS) ) { CRect rectFocus(rect); rectFocus.DeflateRect(3,3); // This looked prety close pDC->DrawFocusRect(rectFocus); } // -[Feral]----------------------------------------------------------- // Clean Up and Return pDC->SelectObject(ppenOld); // Restore the pen }