// 保存父窗口背景。父窗口尺寸变化时需要重新保存。 void CurPokerWnd::SaveParentBackground(void) { // 取父窗口DC HDC parentDC = GetDC(m_hWndParent); assert(parentDC != NULL); SaveParentBackground(parentDC); // 释放父窗口DC ReleaseDC(m_hWndParent, parentDC); }
// 绘图 void CurPokerWnd::OnPaint(HDC hdc) { #ifdef PARENT_PAINT_CHILD UNREFERENCED_PARAMETER(hdc); // Parent window will paint me. Do nothing... #else if (m_bReloadBkBitmap == TRUE) { SaveParentBackground(); m_bReloadBkBitmap = FALSE; } if (m_bHorizontal == TRUE) { PaintHorizontally(hdc); } else { PaintVertically(hdc); } #endif }
//============================================================================= void CXButtonXP::DrawItem(LPDRAWITEMSTRUCT lpDIS) //============================================================================= { CDC *pDC = CDC::FromHandle(lpDIS->hDC); CRect rectItem = lpDIS->rcItem; // set up for double buffering CDC memDC; memDC.CreateCompatibleDC(pDC); CBitmap bmp; bmp.CreateCompatibleBitmap(pDC, rectItem.Width(), rectItem.Height()); CBitmap *pOldBitmap = memDC.SelectObject(&bmp); // initialize dc from existing dc memDC.BitBlt(0, 0, rectItem.Width(), rectItem.Height(), pDC, 0, 0, SRCCOPY); memDC.SetBkMode(TRANSPARENT); if (m_bFirstTime) { // open theme for Button m_bFirstTime = FALSE; if (ThemeHelper.IsThemeLibAvailable()) { if (m_hTheme) ThemeHelper.CloseThemeData(m_hTheme); m_hTheme = NULL; if (m_bDrawToolbar) { m_hTheme = ThemeHelper.OpenThemeData(m_hWnd, _T("Toolbar")); } else { m_hTheme = ThemeHelper.OpenThemeData(m_hWnd, _T("Button")); } } SaveParentBackground(); } CFont *pFont = GetParent()->GetFont(); if (pFont) memDC.SelectObject(pFont); // button state // get the button's title CString strTitle = _T(""); GetWindowText(strTitle); BOOL bHasText = !strTitle.IsEmpty(); BOOL bIsFocused = lpDIS->itemState & ODS_FOCUS; BOOL bIsDisabled = lpDIS->itemState & ODS_DISABLED; BOOL bDrawFocusRect = !(lpDIS->itemState & ODS_NOFOCUSRECT); BOOL bIsPressed = lpDIS->itemState & ODS_SELECTED; BOOL bIsDefault = IsDefault(); BOOL bIsThemed = IsThemed(); BOOL bCustomColors = (m_crBackground != XBUTTONXP_NO_COLOR) || (m_crText != XBUTTONXP_NO_COLOR); if (bCustomColors) { bIsThemed = FALSE; } if (m_bIsToggle) { if (m_bLButtonDown && !m_bMouseOverButton) { // left mouse button is down outside button bIsPressed = !m_bToggled; } else { bIsPressed = m_bToggled; } } if (m_bDrawToolbar) { // restore parent window bits memDC.BitBlt(0, 0, m_rectButton.Width(), m_rectButton.Height(), &m_dcParent, 0, 0, SRCCOPY); bIsFocused = FALSE; bIsDefault = FALSE; } if (bIsThemed) { DWORD state = bIsPressed ? PBS_PRESSED : PBS_NORMAL; if (bIsDisabled) { state = PBS_DISABLED; } else if (state == PBS_NORMAL) { if (bIsFocused || bIsDefault) { state = PBS_DEFAULTED; } if (m_bMouseOverButton) state = PBS_HOT; } ThemeHelper.DrawThemeParentBackground(m_hWnd, memDC.m_hDC, &rectItem); ThemeHelper.DrawThemeBackground(m_hTheme, memDC.m_hDC, BP_PUSHBUTTON, state, &rectItem, NULL); } else // not themed { CBrush brBackground; if (m_crBackground == XBUTTONXP_NO_COLOR) brBackground.CreateSolidBrush(GetSysColor(COLOR_BTNFACE)); else brBackground.CreateSolidBrush(m_crBackground); if (!m_bDrawToolbar || bCustomColors) { // fill in background, if custom color or not toolbar CRect rectClient(rectItem); InflateRect(&rectClient, -1, -1); memDC.FillRect(&rectClient, &brBackground); } if (bIsFocused || bIsDefault) { if (!m_bIsToggle || !bIsPressed) { CBrush brush(RGB(0,0,0)); memDC.FrameRect(&rectItem, &brush); } InflateRect(&rectItem, -1, -1); } // draw the traditional pushbutton edge if (bIsPressed) { memDC.DrawEdge(&rectItem, EDGE_SUNKEN, BF_RECT); } else if (!m_bDrawToolbar) { memDC.DrawEdge(&rectItem, EDGE_RAISED, BF_RECT); } } CRect rectDraw = lpDIS->rcItem; // draw the icon DrawIcon(&memDC, bHasText, rectItem, rectDraw, bIsPressed, bIsThemed, bIsDisabled); // draw the button title (if any) if (bHasText) { DrawText(&memDC, strTitle, rectDraw, bIsPressed, bIsThemed, bIsDisabled); } // draw the focus rect if (bIsFocused && bDrawFocusRect) { CRect rectFocus = rectItem; if (m_pDropDownMenu) rectFocus.right = rectFocus.right - MENUBTN_WIDTH + 1; if (!bIsThemed && bIsPressed) rectFocus.OffsetRect(1, 1); rectFocus.InflateRect(-3, -3); memDC.DrawFocusRect(&rectFocus); } if (m_pDropDownMenu) { DrawSplit(&memDC, rectItem, bIsPressed, bIsThemed, bIsDisabled); DrawArrow(&memDC, rectItem, bIsPressed, bIsThemed, bIsDisabled); } // end double buffering pDC->BitBlt(0, 0, rectItem.Width(), rectItem.Height(), &memDC, 0, 0, SRCCOPY); // swap back the original bitmap if (pOldBitmap) memDC.SelectObject(pOldBitmap); if (bmp.GetSafeHandle()) bmp.DeleteObject(); memDC.DeleteDC(); }