void CFileDlg::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: 在此处添加消息处理程序代码 // 不为绘图消息调用 CDialog::OnPaint() CRect rect; GetClientRect(rect); // dc.FillSolidRect(rect,RGB(255,255,255)); CClientDC aDC(this); //CClientDC的构造函数需要一个参数,这个参数是指向绘图窗口的指针,我们用this指针就可以了 CPoint startPoint; CPoint endPoint; startPoint.x = -1; startPoint.y = 38; endPoint.x = rect.Width()+1; endPoint.y = 38; COLORREF m_Color(RGB(160,160,255)); CPen pen(PS_SOLID,1,m_Color); ////建立一个画笔类对象,构造时设置画笔属性 aDC.SelectObject(&pen); aDC.MoveTo(startPoint); aDC.LineTo(endPoint); startPoint.x = -1; startPoint.y = 40; endPoint.x = rect.Width()+1; endPoint.y = 40; COLORREF m_Color3(RGB(255,255,255)); CPen pen3(PS_SOLID,2,m_Color3); ////建立一个画笔类对象,构造时设置画笔属性 aDC.SelectObject(&pen3); aDC.MoveTo(startPoint); aDC.LineTo(endPoint); startPoint.x = 218; startPoint.y = 38; endPoint.x = 218; endPoint.y = rect.Height()+2; COLORREF m_Color2(RGB(190,190,190)); CPen pen2(PS_SOLID,1,m_Color2); aDC.SelectObject(&pen2); aDC.MoveTo(startPoint); aDC.LineTo(endPoint); }
void CttView::LineCai(const CPoint &x, const CPoint &y){ int c[2][4]; int cnum[2]; CDC* pDC = GetWindowDC(); CPen pen1(0, 0, RGB(255, 0, 0)); CPen pen2(0, 3, RGB(0, 0, 0)); CPen pen3(0, 2, RGB(0, 0, 0)); GetC(c[0],cnum[0],x); GetC(c[1],cnum[1],y); if (cnum[0] & cnum[1]){ pDC->SelectObject(&pen1); pDC->MoveTo(x); pDC->LineTo(y); return; } if ((cnum[0] | cnum[1])==0){ pDC->SelectObject(&pen2); pDC->MoveTo(x); pDC->LineTo(y); return; } pDC->SelectObject(&pen1); pDC->MoveTo(x); pDC->LineTo(y); CPoint a[2]; int an = 0; if (c[0][0] != c[1][0])//与下边有交 { int jx = x.x + (1.0*m_d - x.y) / (y.y - x.y) *(y.x - x.x); if (jx > m_l && jx < m_r) a[an++] = CPoint(jx, m_d); } if (c[0][1] != c[1][1])//与上边有交 { int jx = x.x + (1.0*m_u - x.y) / (y.y - x.y) *(y.x - x.x); if (jx > m_l && jx < m_r) a[an++] = CPoint(jx, m_u); } if (c[0][2] != c[1][2])//与右边有交 { int jy = x.y + (1.0*m_r - x.x) / (y.x - x.x) *(y.y - x.y); if (jy < m_d && jy > m_u) a[an++] = CPoint(m_r, jy); } if (c[0][3] != c[1][3])//与左边有交 { int jy = x.y + (1.0*m_l - x.x) / (y.x - x.x) *(y.y - x.y); if (jy < m_d && jy > m_u) a[an++] = CPoint(m_l, jy); } if (an == 1){ if (cnum[0] == 0)a[an++] = x; else a[an++] = y; } pDC->SelectObject(&pen2); pDC->MoveTo(a[0]); pDC->LineTo(a[1]); }
void CMFCView::OnDraw(CDC* pDC) { CMFCDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); CRect r; GetClientRect(&r); // array of font name CString font[4]={"Arial","Tahoma","Times New Roman", "Consolas"}; CBrush brush; // init brush.CreateSolidBrush(RGB(255,0,0)); // red brush CBrush *pTempBrush = NULL; CBrush origBrush; pTempBrush = (CBrush*) pDC->SelectObject(&brush); // save original brush origBrush.FromHandle((HBRUSH)pTempBrush); // paint upper left corner with red brush pDC->Rectangle(0,0,r.Width()/2, r.Height()/2); try { CBrush brush2(RGB(rand()%255,rand()%255,rand()%255)); CBrush brush3(RGB(rand()%255,rand()%255,rand()%255)); CBrush brush4(RGB(rand()%255,rand()%255,rand()%255)); pTempBrush = (CBrush*)pDC->SelectObject(&brush2); // Paint upper right corner with red brush. pDC->Rectangle(r.Width() / 2, 0, r.Width(), r.Height() / 2); pTempBrush = (CBrush*)pDC->SelectObject(&brush3); // Paint lower left corner with green hatched brush. pDC->Rectangle(0, r.Height() / 2, r.Width() / 2, r.Height()); pTempBrush = (CBrush*)pDC->SelectObject(&brush4); // Paint lower right corner with resource brush. pDC->Rectangle(r.Width() / 2, r.Height() / 2, r.Width(), r.Height()); } catch(CResourceException* e) { e->ReportError(); e->Delete(); } pDC->SelectObject(&origBrush); // set font for every times we create new single view CFont *oldf, f; // select a random font in font array. f.CreateFont(rand()%80, rand()%30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, font[rand()%4]); oldf= pDC->SelectObject(&f); pDC->SetTextAlign(TA_CENTER); // align text to the center of the screen pDC->SetTextColor(RGB(rand()%255,rand()%255,rand()%255)); pDC->TextOut(r.CenterPoint().x, r.CenterPoint().y, "hello world"); // draw new rectangle CPen pen(PS_SOLID, 10, RGB(rand()%255,rand()%255,rand()%255)); CPen *pOldPen = pDC->SelectObject(&pen); pDC->Rectangle(50, 50, 250, 250); pDC->SelectObject(pOldPen); // draw new ellipse CPen pen2(PS_SOLID, 10, RGB(rand()%255,rand()%255,rand()%255)); CPen *pOldPen2 = pDC->SelectObject(&pen2); pDC->Ellipse(50, 50, 250, 250); pDC->SelectObject(pOldPen2); // duong cheo chinh CPen pen3(PS_SOLID, 10, RGB(rand()%255,rand()%255,rand()%255)); CPen *pOldPen3 = pDC->SelectObject(&pen3); pDC->MoveTo(50, 50); pDC->LineTo(250, 250); pDC->SelectObject(pOldPen3); // duong cheo phu CPen pen4(PS_SOLID, 10, RGB(rand()%255,rand()%255,rand()%255)); CPen *pOldPen4 = pDC->SelectObject(&pen4); pDC->MoveTo(250, 50); pDC->LineTo(50, 250); pDC->SelectObject(pOldPen4); // release object when it is no longer in use f.DeleteObject(); pen.DeleteObject(); pen2.DeleteObject(); pen3.DeleteObject(); pen4.DeleteObject(); }
void Plot::paintEvent(QPaintEvent *event) { QWidget::paintEvent(event); if (softMinimum == softMaximum) return; for (const auto l : legend) if (l->pen() != Qt::transparent) goto draw; return; draw: QPainter painter(this); // set coordinate system QSize s; s.setHeight(height() - padding_top - padding_bottom); // TODO bug - wykres nie działa, jeśli wartości brzegowe nie są całkowite - problem z setWindow double minval = 0;//double minval = qFloor(minimax.minimum(softMinimum, softMaximum)); double maxval = 0;//qCeil(minimax.maximum(softMinimum, softMaximum)); for (auto i : session->chart->series()) if (legend[i]->pen() != Qt::transparent) for (int j = softMinimum; j <= softMaximum && j < reinterpret_cast<QtCharts::QLineSeries*>(i)->count(); j++) maxval = qMax(maxval, reinterpret_cast<QtCharts::QLineSeries*>(i)->at(j).y()); if (minval == maxval) minval--; double delta = tickSpan(minval, maxval, s.height(), 24); /* TODO do pewnej wysokości nie powinno się rozrzedzać podziałki, tylko ją zwyczajnie skalować */ double ut = maxval = qCeil(maxval / delta) * delta;//double ut = qFloor(maxval / delta) * delta; double lt = qCeil(minval / delta) * delta; int scale = 1; while (delta * scale < 1) scale *= 10; s.setWidth(width() - 50 - 20); painter.setViewport(50 + 10, height() - padding_bottom, s.width(), -s.height()); painter.setWindow(softMinimum, minval, softMaximum - softMinimum, (maxval - minval) * scale); auto transform = painter.combinedTransform(); // draw axis painter.setRenderHint(QPainter::Antialiasing); QPen pen1(QBrush("#808080"), 1.); pen1.setCosmetic(true); painter.setPen(pen1); painter.drawLine(softMinimum, minval * scale, softMaximum, minval * scale); int gap = tickSpan(painter.fontMetrics().width(QString::number(softMaximum)) + 20, width() - 50); painter.setViewTransformEnabled(false); for (int i = softMinimum + (gap - (softMinimum % gap)) % gap; i <= softMaximum; i += gap) { auto tick = transform.map(QPointF(i, minval)).toPoint(); painter.drawLine(tick, tick + QPoint(0, 5)); painter.drawText(QRect(tick + QPoint(0, 9), QSize()), Qt::AlignHCenter | Qt::AlignTop | Qt::TextDontClip, QString::number(i)); } for (qreal i = lt; i <= ut; i += delta) painter.drawText(QRect(transform.map(QPointF(softMinimum, i * scale)).toPoint() - QPoint(9, 0), QSize()), Qt::AlignRight | Qt::AlignVCenter | Qt::TextDontClip, QString::number(i)); painter.setViewTransformEnabled(true); pen1.setStyle(Qt::DashLine); pen1.setColor("#333333"); painter.setPen(pen1); for (qreal i = (lt != minval ? lt : lt + delta); i <= ut; i += delta) painter.drawLine(softMinimum, i * scale, softMaximum, i * scale); painter.save(); painter.scale(1, scale); // plot data for (auto i : session->chart->series()) { auto interval = reinterpret_cast<QtCharts::QLineSeries*>(i)->pointsVector().mid(softMinimum, softMaximum - softMinimum + 1); if (interval.isEmpty()) continue; interval.prepend({interval.first().x(), 0}); interval.append({interval.last().x(), 0}); QLinearGradient gradient(0, minval, 0, maxval); gradient.setColorAt(0, Qt::transparent); gradient.setColorAt(1, legend[i]->brush()); painter.setPen(Qt::NoPen); painter.setBrush(gradient); painter.drawPolygon(interval); QPen pen2(legend[i]->pen(), 2.); pen2.setCosmetic(true); painter.setPen(pen2); painter.drawPolyline(&interval[1], interval.size() - 2); } painter.restore(); if (softMinimum <= value() && value() <= softMaximum) { painter.setViewTransformEnabled(false); int crs = transform.map(QPointF(value(), 0)).x(); QPen pen3(Qt::white, 2.); pen3.setJoinStyle(Qt::MiterJoin); painter.setPen(pen3); painter.setBrush(QBrush(Qt::white)); painter.drawLine(crs, padding_top - 5, crs, height() - padding_bottom); painter.drawConvexPolygon(QPolygon({{crs, padding_top - 2}, {crs - 3, padding_top - 5}, {crs + 3, padding_top - 5}})); } }
void Hokuyo::paintEvent(QPaintEvent *event) // display the data on the winder { flag = 0; int center_h = (int) (this->parentWidget()->height() /2); int center_w = (int) (this->parentWidget()->width() /2); QPainter painter(this); //*************************************************************** QPen pen2(Qt::black, 2, Qt::SolidLine); painter.setPen(pen2); painter.setBrush(Qt::red); QRectF rectangle(center_w - 2, center_h-2, 4, 4); painter.drawEllipse(rectangle); //******************************************************* if(showLRF) // if show laser range finder { QPen pen(Qt::black, 2, Qt::DashDotDotLine); painter.setPen(pen); painter.drawLine(20, 40, 120, 40); } else if(showIR) // if show small infrared sensor { QPen pen(Qt::black, 2, Qt::SolidLine); painter.setPen(pen); painter.drawLine(20, 40, 20 + center_h, 40); } //************************************************************** if(showIR) { painter.setBrush(Qt::cyan); QPen pen1(Qt::black, 2, Qt::SolidLine); QPointF points_front[3]; QPointF points_back[3]; points_front[0] = QPointF(center_w,center_h); points_front[1] = QPointF(center_w - 50, center_h + (float)this->IR01_update * center_h); points_front[2] = QPointF(center_w + 50, center_h + (float)this->IR01_update * center_h); painter.setPen(pen1); painter.drawPolygon(points_front,3); painter.setBrush(Qt::darkCyan); points_back[0] = QPointF(center_w, center_h); points_back[1] = QPointF(center_w - 50, center_h - (float)this->IR02_update * center_h); points_back[2] = QPointF(center_w + 50, center_h - (float)this->IR02_update * center_h); painter.drawPolygon(points_back,3); } else if(showLRF) { QPen pen3(Qt::black, 2, Qt::DashDotDotLine); painter.setPen(pen3); int count = 0; if(hokuyo_points!=NULL) { for(int it=0;it<683;it++) { painter.drawPoint(hokuyo_points[it].x * 100 + 300, -(hokuyo_points[it].y * 100) + 300); if(showlines) painter.drawLine(300,300,hokuyo_points[it].x * 100 + 300, -(hokuyo_points[it].y * 100) + 300); count ++; } } flag = 1; } }
void CChartAreaWnd::DrawTaskBar( CDC* pDC, int x, int y, int w, int h, int fHeight, char* wbuff ) { int x1,y1,x2,y2; CBrush* pOldBrush; CPen* pOldPen; CBrush myBrush1; char wbuff2[BUFF_SIZE]; x1 = x; y1 = y; x2 = x + w; y2 = y + h; COLORREF col = RGB( 190, 190, 190 ); CPen pen1( PS_SOLID, 1, col ); myBrush1.CreateSolidBrush( col ); pDC->SetBkColor( col ); pOldPen = pDC->SelectObject( &pen1 ); pOldBrush = pDC->SelectObject( &myBrush1 ); pDC->Rectangle( x1, y1, x2, y2 ); pDC->MoveTo( x1, y1 ); pDC->LineTo( x2, y1 ); pDC->LineTo( x2, y2 ); pDC->LineTo( x1, y2 ); pDC->LineTo( x1, y1 ); pDC->SelectObject( pOldPen ); pDC->SelectObject( pOldBrush ); x1 = x1+1; y1 = y1+1; x2 = x2-1; y2 = y2-1; CPen pen2( PS_SOLID, 1, RGB(255, 255, 255) ); pOldPen = pDC->SelectObject( &pen2 ); pDC->MoveTo( x1, y1 ); pDC->LineTo( x2, y1 ); pDC->MoveTo( x1, y1 ); pDC->LineTo( x1, y2 ); pDC->SelectObject( pOldPen ); CPen pen3( PS_SOLID, 1, RGB(0, 0, 0) ); pOldPen = pDC->SelectObject( &pen3 ); pDC->MoveTo( x2, y2 ); pDC->LineTo( x2, y1 ); pDC->MoveTo( x2, y2 ); pDC->LineTo( x1, y2 ); pDC->SelectObject( pOldPen ); int slen = strlen(wbuff); for(;;) { if ( (fHeight/2)*slen > w ) slen = slen - 2; else break; } if ( slen > 0 ){ strncpy( wbuff2, wbuff, slen ); wbuff2[slen] = '\0'; pDC->TextOut( x1 + (w-(fHeight/2)*strlen(wbuff2))/2, y1 + (h-(fHeight))/2 , wbuff2, strlen(wbuff2) ); } }
void CttView::OnDraw(CDC* pDC) { CttDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; //pDC->MoveTo(10,10); //pDC->LineTo(10,400); //pDC->LineTo(1024,400); //pDC->LineTo(1024,10); //pDC->LineTo(10,10); if (m_animeON){ CRect rect; GetClientRect(&rect); if (m_cls) pDC->FillRect(rect, RGB(0, 0, 0)); double as = fabs(sin(m_x / 50.0)); double ac = fabs(cos(m_y / 50.0)); double as2 = sin(2 * 3.1415926 *m_x / rect.Width()); double ac2 = sin(2 * 3.1415926 *m_y / rect.Height()); int rc = min((int)(((as2 + 1.0) / 2.0) * 0xff), 0xff); int gc = min((int)(((ac2 + 1.0) / 2.0) * 0xff), 0xff); int bc = min(255, 512 - rc - gc); COLORREF color = RGB(rc, gc, bc); CPen pen(1, 5, color); CBrush brush(color); pDC->SelectObject(&pen); pDC->SelectObject(&brush); int bigx = abs(sin(m_x / 50.0)) * 50 + 1; int bigy = abs(cos(m_y / 50.0)) * 50 + 1; pDC->Ellipse(m_x - bigx, m_y - bigy, m_x + bigx, m_y + bigy); } if (m_gameON){ CRect rect; GetClientRect(&rect); int &px = m_player_x; int &py = m_player_y; int bigx = 30; int bigy = 30; COLORREF color = RGB(23, 123, 123); COLORREF enemy_color = RGB(255, 0, 0); CPen pen(1, 5, color); CBrush brush(RGB(23,123,223)); CPen pen2(1, 3, enemy_color); CBrush brush2(RGB(0,0,0)); CPen pen3(1, 2, RGB(123,255,23)); CBrush brush3(RGB(155, 225, 123)); pDC->FillRect(rect, RGB(0, 0, 0)); int cnt = m_enemy.size(); pDC->SelectObject(&pen2); pDC->SelectObject(&brush2); for (int i = 0; i < cnt; i++){ int ex = m_enemy[i].x; int ey = m_enemy[i].y; pDC->Ellipse(ex-10,ey-10,ex+10,ey+10); } pDC->SelectObject(&pen3); pDC->SelectObject(&brush3); cnt = m_bullet.size(); for (int i = 0; i < cnt; i++){ int ex = m_bullet[i].first.x; int ey = m_bullet[i].first.y; pDC->Ellipse(ex - 10, ey - 10, ex + 10, ey + 10); } pDC->SelectObject(&pen); pDC->SelectObject(&brush); pDC->Ellipse(px - bigx, py - bigy, px + bigx, py + bigy); } // TODO: 在此处为本机数据添加绘制代码 }
void LinkStatusWidget::paintEvent(QPaintEvent *event){ initLines(); QPainter painter(this); QPixmap pic(QString::fromUtf8(":/computer_white.png")); QRectF dest(0,0,pic.width(),pic.height()); QPixmap pic2(QString::fromUtf8(":/honeywell.png")); QRectF dest2(0,0,pic2.width(),pic2.height()); //220*180 QRectF target_0(this->left_left,this->left_top,this->left_width,this->left_height); QRectF target_1(this->width()-this->right_right-this->right_width,this->right_top,this->right_width,this->right_height); QRectF target_2(this->width()-this->right_right-this->right_width,this->right_top+this->right_height+this->right_space,this->right_width,this->right_height); painter.setPen(Qt::white); painter.drawPixmap(target_0,pic2,dest2); painter.drawText(this->left_left+this->left_width+this->left_space,this->left_top+painter.fontMetrics().height(),Config::LINKSTATUS_LABEL.split("#").at(2)); painter.drawPixmap(target_1,pic,dest); QString text=Config::LINKSTATUS_LABEL.split("#").at(0); int f_w=painter.fontMetrics().width(text); painter.drawText(this->width()-this->right_right-this->right_width-this->right_space*2-f_w,this->right_top+painter.fontMetrics().height()+this->right_space,text); painter.drawPixmap(target_2,pic,dest); text=Config::LINKSTATUS_LABEL.split("#").at(1); f_w=painter.fontMetrics().width(text); painter.drawText(this->width()-this->right_right-this->right_width-this->right_space*2-f_w,this->right_top+this->right_height+this->right_space*2+painter.fontMetrics().height(),text); //draw base line painter.save(); //#353942 QPen pen1(Qt::white,8,Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin); pen1.setColor(QColor(53, 57, 66)); painter.setPen(pen1); painter.drawLines(lines); painter.restore(); if(!this->isConn){ return; } int centerHeight=this->right_top+this->right_height+this->right_space/2; QColor co1 =QColor(146, 196, 15) ; QColor co2 =QColor(0, 255, 0, 0); QColor co3 =QColor( 255,0, 0,200); QColor co4 =QColor( 255,0,0, 0); int mleft=this->width()-this->right_right-this->right_width-this->right_space-this->countMain; int mright=mleft+this->line_width_main; if(this->isBroken_1){ QLinearGradient linearGradient = QLinearGradient(mleft,centerHeight,mright,centerHeight); linearGradient.setColorAt(0,co3); linearGradient.setColorAt(1,co4); QPen pen2(linearGradient,5); pen2.setCapStyle(Qt::RoundCap); painter.setPen(pen2); }else{ QLinearGradient linearGradient = QLinearGradient(mleft,centerHeight,mright,centerHeight); linearGradient.setColorAt(0,co1); linearGradient.setColorAt(1,co2); QPen pen2(linearGradient,5); pen2.setCapStyle(Qt::RoundCap); painter.setPen(pen2); } painter.drawLine(mleft,centerHeight,mright,centerHeight); int sbottom= this->right_top+this->right_height/2+this->countSub; int stop= sbottom-this->line_width_sub; int sright=this->width()-this->right_right+this->right_space; //message 2 if(this->isBroken_2){ QLinearGradient linearGradient2 = QLinearGradient(sright,stop,sright,sbottom); linearGradient2.setColorAt(0,co4); linearGradient2.setColorAt(1,co3); QPen pen3(linearGradient2,4); pen3.setCapStyle(Qt::RoundCap); painter.setPen(pen3); }else{ QLinearGradient linearGradient2 = QLinearGradient(sright,stop,sright,sbottom); linearGradient2.setColorAt(0,co2); linearGradient2.setColorAt(1,co1); QPen pen3(linearGradient2,4); pen3.setCapStyle(Qt::RoundCap); painter.setPen(pen3); } painter.drawLine(sright,stop,sright,sbottom); }