/** * name: PaintThemeButton * desc: Draws the none themed button * param: ctl - BTNCTRL structure for the button * hdcMem - device context to draw to * rcClient - rectangle of the whole button * return: nothing **/ static void __fastcall PaintButton(BTNCTRL *ctl, HDC hdcMem, LPRECT rcClient) { RECT rcText = { 0, 0, 0, 0 }; TCHAR szText[MAX_PATH] = { 0 }; WORD ccText; // Draw the flat button if (ctl->dwStyle & MBS_FLAT) { HBRUSH hbr = NULL; if (ctl->stateId == PBS_PRESSED || ctl->stateId == PBS_HOT) hbr = GetSysColorBrush(COLOR_3DLIGHT); else { HDC dc; HWND hwndParent; hwndParent = GetParent(ctl->hwnd); if (dc = GetDC(hwndParent)) { hbr = (HBRUSH)SendMessage(hwndParent, WM_CTLCOLORDLG, (WPARAM)dc, (LPARAM)hwndParent); ReleaseDC(hwndParent, dc); } } if (hbr) { FillRect(hdcMem, rcClient, hbr); DeleteObject(hbr); } if (ctl->stateId == PBS_HOT || ctl->bFocus) { if (ctl->pbState) DrawEdge(hdcMem, rcClient, EDGE_ETCHED, BF_RECT | BF_SOFT); else DrawEdge(hdcMem, rcClient, BDR_RAISEDOUTER, BF_RECT | BF_SOFT | BF_FLAT); } else if (ctl->stateId == PBS_PRESSED) DrawEdge(hdcMem, rcClient, BDR_SUNKENOUTER, BF_RECT | BF_SOFT); } else { UINT uState = DFCS_BUTTONPUSH | ((ctl->stateId == PBS_HOT) ? DFCS_HOT : 0) | ((ctl->stateId == PBS_PRESSED) ? DFCS_PUSHED : 0); if (ctl->defbutton&&ctl->stateId == PBS_NORMAL) uState |= DLGC_DEFPUSHBUTTON; DrawFrameControl(hdcMem, rcClient, DFC_BUTTON, uState); // Draw focus rectangle if button has focus if (ctl->bFocus) { RECT focusRect = *rcClient; InflateRect(&focusRect, -3, -3); DrawFocusRect(hdcMem, &focusRect); } } // calculate text rect { SIZE sizeText; HFONT hOldFont; ccText = GetWindowText(ctl->hwnd, szText, _countof(szText)); if (ccText > 0) { hOldFont = (HFONT)SelectObject(hdcMem, ctl->hFont); GetTextExtentPoint32(hdcMem, szText, ccText, &sizeText); if (ctl->cHot) { SIZE sizeHot; GetTextExtentPoint32A(hdcMem, "&", 1, &sizeHot); sizeText.cx -= sizeHot.cx; } SelectObject(hdcMem, hOldFont); rcText.left = (ctl->hIcon) ? 0 : (rcClient->right - rcClient->left - sizeText.cx) / 2; rcText.top = (rcClient->bottom - rcClient->top - sizeText.cy) / 2; rcText.right = rcText.left + sizeText.cx; rcText.bottom = rcText.top + sizeText.cy; if (ctl->stateId == PBS_PRESSED) OffsetRect(&rcText, 1, 1); } } PaintIcon(ctl, hdcMem, &ccText, rcClient, &rcText); // draw text if (ccText > 0) { HFONT hOldFont; hOldFont = (HFONT)SelectObject(hdcMem, ctl->hFont); SetBkMode(hdcMem, TRANSPARENT); SetTextColor(hdcMem, IsWindowEnabled(ctl->hwnd) || !ctl->hThemeButton ? ctl->stateId == PBS_HOT ? GetSysColor(COLOR_HOTLIGHT) : GetSysColor(COLOR_BTNTEXT) : GetSysColor(COLOR_GRAYTEXT)); DrawState(hdcMem, NULL, NULL, (LPARAM)szText, 0, rcText.left, rcText.top, rcText.right - rcText.left, rcText.bottom - rcText.top, IsWindowEnabled(ctl->hwnd) || ctl->hThemeButton ? DST_PREFIXTEXT | DSS_NORMAL : DST_PREFIXTEXT | DSS_DISABLED); SelectObject(hdcMem, hOldFont); } }
// Paint the button void QPictureButton::paintEvent(QPaintEvent*) { PaintButton(); PaintIcon(); PaintText(); }
/** * name: PaintThemeButton * desc: Draws the themed button * param: ctl - BTNCTRL structure for the button * hdcMem - device context to draw to * rcClient - rectangle of the whole button * return: nothing **/ static void __fastcall PaintThemeButton(BTNCTRL *ctl, HDC hdcMem, LPRECT rcClient) { RECT rcText = { 0, 0, 0, 0 }; WCHAR wszText[MAX_PATH] = { 0 }; WORD ccText; // Draw the flat button if ((ctl->dwStyle & MBS_FLAT) && ctl->hThemeToolbar) { int state = IsWindowEnabled(ctl->hwnd) ? (ctl->stateId == PBS_NORMAL && ctl->defbutton ? PBS_DEFAULTED : ctl->stateId) : PBS_DISABLED; if (IsThemeBackgroundPartiallyTransparent(ctl->hThemeToolbar, TP_BUTTON, TBStateConvert2Flat(state))) { if (SUCCEEDED(DrawThemeParentBackground(ctl->hwnd, hdcMem, rcClient))) DrawThemeParentBackground(GetParent(ctl->hwnd), hdcMem, rcClient); } DrawThemeBackground(ctl->hThemeToolbar, hdcMem, TP_BUTTON, TBStateConvert2Flat(state), rcClient, rcClient); } else { // draw themed button background if (ctl->hThemeButton) { int state = IsWindowEnabled(ctl->hwnd) ? (ctl->stateId == PBS_NORMAL && ctl->defbutton ? PBS_DEFAULTED : ctl->stateId) : PBS_DISABLED; if (IsThemeBackgroundPartiallyTransparent(ctl->hThemeButton, BP_PUSHBUTTON, state)) { if (SUCCEEDED(DrawThemeParentBackground(ctl->hwnd, hdcMem, rcClient))) DrawThemeParentBackground(GetParent(ctl->hwnd), hdcMem, rcClient); } DrawThemeBackground(ctl->hThemeButton, hdcMem, BP_PUSHBUTTON, state, rcClient, rcClient); } } // calculate text rect { RECT sizeText; HFONT hOldFont; ccText = GetWindowTextW(ctl->hwnd, wszText, _countof(wszText)); if (ccText > 0) { hOldFont = (HFONT)SelectObject(hdcMem, ctl->hFont); GetThemeTextExtent( ctl->hThemeButton, hdcMem, BP_PUSHBUTTON, IsWindowEnabled(ctl->hwnd) ? ctl->stateId : PBS_DISABLED, wszText, ccText, DST_PREFIXTEXT, NULL, &sizeText); if (ctl->cHot) { RECT rcHot; GetThemeTextExtent(ctl->hThemeButton, hdcMem, BP_PUSHBUTTON, IsWindowEnabled(ctl->hwnd) ? ctl->stateId : PBS_DISABLED, L"&", 1, DST_PREFIXTEXT, NULL, &rcHot); sizeText.right -= (rcHot.right - rcHot.left); } SelectObject(hdcMem, hOldFont); rcText.left = (ctl->hIcon) ? 0 : (rcClient->right - rcClient->left - (sizeText.right - sizeText.left)) / 2; rcText.top = (rcClient->bottom - rcClient->top - (sizeText.bottom - sizeText.top)) / 2; rcText.right = rcText.left + (sizeText.right - sizeText.left); rcText.bottom = rcText.top + (sizeText.bottom - sizeText.top); if (ctl->stateId == PBS_PRESSED) { OffsetRect(&rcText, 1, 1); } } } PaintIcon(ctl, hdcMem, &ccText, rcClient, &rcText); // draw text if (ccText > 0 && ctl->hThemeButton) { HFONT hOldFont = (HFONT)SelectObject(hdcMem, ctl->hFont); DrawThemeText(ctl->hThemeButton, hdcMem, BP_PUSHBUTTON, IsWindowEnabled(ctl->hwnd) ? ctl->stateId : PBS_DISABLED, wszText, ccText, DST_PREFIXTEXT, 0, &rcText); SelectObject(hdcMem, hOldFont); } }