void MyButton::paintEvent(QPaintEvent *e){ /*绘制按钮的形状*/ QPainter g(this); int w=width(); int h=height(); QPoint pTop(w/2,2); QPoint pBottom(w/2,h-2); QPoint pLeft(2,h/2); QPoint pRight(w-2,h/2); QColor cWhite(255,255,255); QColor cBlack( 0, 0, 0); QBrush bWhite(cWhite); QBrush bBlack(cBlack); QPen penWhite(bWhite,2); QPen penBlack(bBlack,2); /*白色*/ g.setPen(penWhite); g.drawLine(pTop,pLeft); g.drawLine(pTop,pRight); /*黑色*/ g.setPen(penBlack); g.drawLine(pBottom,pLeft); g.drawLine(pBottom,pRight); }
//////////////////////////////////////////////////////////////////////////////// // // 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); } }
//Display tooltip void CToolTip2::DisplayToolTip(const CPoint& rCurrentPoint) { CDC* pDC = GetDC(); CBrush *pOldBrush; CFont *pOldFont; pOldFont = pDC->SelectObject(&m_font); //CSize size = pDC->GetTextExtent(m_szText); //pDC->LPtoDP(&size); int x = 0; int nLargest = 0; CString sLine; CSize size; while(GetLine(m_szText, &sLine, x)) { size = pDC->GetTextExtent(sLine); pDC->LPtoDP(&size); if(size.cx > nLargest) nLargest = size.cx; x++; } int nHeight = size.cy; size.cx = nLargest; size.cy *= x; //Form tooltip rectangle CRect rectToolTip(rCurrentPoint.x, rCurrentPoint.y, rCurrentPoint.x+size.cx+7, rCurrentPoint.y+size.cy+2); //Draw Tooltip Rect and Text // pDC->SetBkMode(TRANSPARENT); CBrush brushToolTip(GetSysColor(COLOR_INFOBK)); pOldBrush = pDC->SelectObject(&brushToolTip); //Create and select thick black pen CPen penBlack(PS_SOLID, 0, COLORREF(RGB(0, 0, 0))); CPen* pOldPen = pDC->SelectObject(&penBlack); //Draw rectangle filled with COLOR_INFOBK pDC->Rectangle(0,0,rectToolTip.Width(),rectToolTip.Height()); //Draw tooltip text pDC->SetTextColor( GetSysColor(COLOR_INFOTEXT) );//Tool Tip color set in control panel settings pDC->SetTextAlign(TA_LEFT); //pDC->TextOut(3,1, m_szText); x = 0; while(GetLine(m_szText, &sLine, x)) { pDC->TextOut(3,1 + (x * nHeight), sLine); x++; } CRect rectWnd = rectToolTip; m_pParentWnd->ClientToScreen(rectWnd); //Convert from client to screen coordinates CPoint ptToolTipLeft = rectWnd.TopLeft(); //Now display tooltip SetWindowPos(&wndTop,ptToolTipLeft.x+1, ptToolTipLeft.y+1, rectWnd.Width(), rectWnd.Height(),SWP_SHOWWINDOW|SWP_NOOWNERZORDER|SWP_NOACTIVATE); //put back old objects pDC->SelectObject(pOldBrush); pDC->SelectObject(pOldPen); pDC->SelectObject(pOldFont); ReleaseDC(pDC); }
void WidgetArea::paintEvent(QPaintEvent *event) { QFrame::paintEvent(event); if (!m_show_grid && m_grid == 1 && !m_show_bookmk && m_marks.empty() && m_placementLines.isEmpty() && m_bookmarks.empty()) return; QPainter painter(this); if(m_show_grid && m_grid > 1) { QVarLengthArray<QPoint, 8000> points((width()/m_grid)*(height()/m_grid)); for(QPoint p; p.y() < height(); p.ry() += m_grid) for(p.rx() = 0; p.x() < width(); p.rx() += m_grid) points.append(p + m_grid_offset); painter.drawPoints(points.data(), points.size()); } if(!m_marks.empty()) { painter.setPen(Qt::red); painter.setBrush(QBrush(Qt::red, Qt::SolidPattern)); for(mark_map::iterator itr = m_marks.begin(); itr != m_marks.end(); ++itr) painter.drawRect(*itr); } if(!m_placementLines.isEmpty()) { painter.setPen(Qt::blue); painter.setBrush(QBrush(Qt::blue, Qt::SolidPattern)); painter.drawLines(m_placementLines); } if(m_show_bookmk && !m_bookmarks.empty()) { QPen penBlack(Qt::black); QPen penYellow(QColor("#FFFFA3")); penYellow.setWidth(2); QBrush noBrush(Qt::NoBrush); QBrush fill(penYellow.color(), Qt::SolidPattern); QRect r = rect(); r.setWidth(r.width() - penYellow.width()*2); r.setHeight(r.height() - penYellow.width()*2); QFont f = painter.font(); f.setBold(true); painter.setFont(f); for(size_t i = 0; i < m_bookmarks.size(); ++i) { area_bookmark& p = m_bookmarks[i]; if(p.text.x() == -1) { p.text.rx() = painter.fontMetrics().width(p.keyseq)*1.5; p.text.ry() = painter.fontMetrics().height(); } QPoint base(p.main.x()+penYellow.width(), p.main.y()+penYellow.width()); painter.setPen(penYellow); painter.setBrush(noBrush); painter.drawRect(base.x(), base.y(), r.width(), r.height()); painter.setBrush(fill); painter.drawRect(base.x(), base.y(), p.text.x(), p.text.y()); painter.setPen(penBlack); painter.drawText(base.x(), base.y(), p.text.x(), p.text.y(), Qt::AlignCenter, p.keyseq); } } }
void CDockingBar::OnPaint() { CPaintDC dc(this); // device context for painting COLORREF rgb_background= ::GetSysColor(COLOR_APPWORKSPACE); CMemoryDC mem_dc(dc, this, rgb_background); mem_dc.SelectStockObject(DEFAULT_GUI_FONT); CRect rect; GetClientRect(rect); mem_dc.Draw3dRect(rect, ::GetSysColor(COLOR_3DSHADOW), ::GetSysColor(COLOR_3DHILIGHT)); rect.DeflateRect(1, 1); mem_dc.Draw3dRect(rect, ::GetSysColor(COLOR_3DDKSHADOW), rgb_background); rect.DeflateRect(1, 0); rect.top++; const int GAP= rect.Height() / 2; const int ICON= 18; COLORREF rgb_black= ::GetSysColor(COLOR_BTNTEXT); COLORREF rgb_dark= ::GetSysColor(COLOR_3DDKSHADOW); COLORREF rgb_light= ::GetSysColor(COLOR_3DHILIGHT); COLORREF rgb_gray= GetLighterColor(rgb_background, 13.0); // 13.0% brighter CPen penBlack(PS_SOLID, 0, rgb_black); CPen penDark(PS_SOLID, 0, rgb_dark); CPen penLight(PS_SOLID, 0, rgb_light); CPen penGray(PS_SOLID, 0, rgb_gray); CBrush brGray(::GetSysColor(COLOR_3DFACE)); CBrush* brush= mem_dc.SelectObject(&brGray); mem_dc.SetBkMode(OPAQUE); CalcWidth(tabs_, mem_dc, rect); for (int index= 0; index < tabs_.size(); ++index) { CTab& tab= tabs_[index]; CRect text_rect= tab.location_rect_; text_rect.left += ICON; COLORREF rgb_tab= tab.active_ ? ::GetSysColor(COLOR_3DFACE) : rgb_background; if (tab.active_) // active tab--taller, gray backgnd { CRect rect= tab.location_rect_; rect.top++; rect.right++; rect.bottom = rect.top + 1; // draw two lines on the top mem_dc.FillSolidRect(rect, rgb_dark); rect.OffsetRect(0, 1); mem_dc.FillSolidRect(rect, rgb_light); rect.top++; rect.bottom = tab.location_rect_.bottom + 1; mem_dc.FillSolidRect(rect, rgb_tab); text_rect.top = rect.top; rect.top--; int bottom= tab.location_rect_.bottom - 1; int gap= (bottom - rect.top) / 2; POINT vptLeftTriangle[]= { { rect.left, rect.top }, { rect.left, bottom + 2 }, { rect.left - gap - 1, bottom + 2 }, { rect.left, rect.top } }; mem_dc.SelectStockObject(NULL_PEN); mem_dc.Polygon(vptLeftTriangle, array_count(vptLeftTriangle)); POINT vptRightTriangle[]= { { rect.right, rect.top }, { rect.right, bottom + 2 }, { rect.right + gap, bottom + 2 }, { rect.right + gap, bottom }, { rect.right, rect.top } }; mem_dc.Polygon(vptRightTriangle, array_count(vptRightTriangle)); // draw slanted line on the right side CPen* pen= mem_dc.SelectObject(&penBlack); mem_dc.MoveTo(rect.right, rect.top); mem_dc.LineTo(rect.right + gap, bottom); mem_dc.LineTo(rect.right + gap + 1, bottom); // draw slanted line on the left side mem_dc.SelectObject(&penDark); int left= rect.left - 1; mem_dc.MoveTo(left, rect.top); mem_dc.LineTo(left - gap, bottom); mem_dc.LineTo(left - gap + 1, bottom); // draw light slanted line on the left side mem_dc.SelectObject(&penLight); left++; mem_dc.MoveTo(left, rect.top); mem_dc.LineTo(left - gap, bottom); mem_dc.LineTo(left - gap, bottom + 2); } else // inactive tab (smaller, dark background) { CRect rect= tab.location_rect_; rect.left++; rect.right++; rect.top += 2; rect.bottom = rect.top + 1; // draw two lines on the top mem_dc.FillSolidRect(rect, rgb_dark); rect.OffsetRect(0, 1); mem_dc.FillSolidRect(rect, rgb_gray); // draw slanted line on the right side CPen* pen= mem_dc.SelectObject(&penBlack); int right= rect.right - 0; mem_dc.MoveTo(right, rect.top); int bottom= tab.location_rect_.bottom - 1; int gap= (bottom - rect.top) / 2; mem_dc.LineTo(right + gap, bottom + 1); // draw short slanted line on the left side mem_dc.SelectObject(&penDark); int left= rect.left - 1; mem_dc.MoveTo(left, rect.top); if (index == 0) // first tab? { mem_dc.LineTo(left - gap, bottom); // in the first tab whole edge is visible mem_dc.LineTo(left - gap, bottom + 1); // draw light slanted line on the left side mem_dc.SelectObject(&penGray); mem_dc.MoveTo(left + 1, rect.top); mem_dc.LineTo(left - gap + 1, bottom); mem_dc.LineTo(left - gap + 1, bottom + 1); } else { int left_half= left - gap / 2; int bottom_half= (rect.top + bottom) / 2; mem_dc.LineTo(left_half, bottom_half); mem_dc.LineTo(left_half, bottom_half + 1); // draw light slanted line on the left side mem_dc.SelectObject(&penGray); mem_dc.MoveTo(left + 1, rect.top); mem_dc.LineTo(left_half, bottom_half + 2); // previous tab is not active? // if (index > 0 && !tabs_[index - 1].active_) // mem_dc.LineTo(left_half, bottom_half + 3); } mem_dc.SelectObject(pen); text_rect.top = rect.top + 1; } mem_dc.SetBkColor(rgb_tab); mem_dc.DrawText(tab.name_, text_rect, DT_LEFT | DT_TOP | DT_SINGLELINE | DT_NOPREFIX | DT_EXPANDTABS | DT_END_ELLIPSIS); int offset= tab.active_ ? 0 : 12; CPoint icon(tab.location_rect_.left + 1, text_rect.top); if (tab.icon_ >= 0) image_list_.Draw(&mem_dc, offset + tab.icon_, icon, ILD_NORMAL); } mem_dc.SelectObject(brush); mem_dc.BitBlt(); }