MFC straight line dotted line circle oval rectangular arc

Source: Internet
Author: User

* *** Add the DLG. h header file:

// Add the paint brush and dot variable array to the project: public: cpen m_pen [5]; cpoint m_point [5]; public: void drawline (CDC * PDC ); void drawpolyline (CDC * PDC); void drawpolygon (CDC * PDC); void drawrect (CDC * PDC); void drawroundrect (CDC * PDC); void drawellipse (CDC * PDC ); void drawarc (CDC * PDC); void drawanglearc (CDC * PDC );

* *** Modify the DLG. cpp constructor:

Clinetestdlg: clinetestdlg (cwnd * pparent/* = NULL */): cdialogex (clinetestdlg: IDD, pparent) {m_hicon = afxgetapp ()-> loadicon (idr_mainframe ); // initialize the paint brush and dot variable array m_pen [0]. createpen (ps_solid, 1, RGB (255, 0, 0); // red solid line, 1 pixel width --- parameter: style, width, color m_pen [1]. createpen (ps_solid, 6, RGB (0,255, 0); // green solid line, 6 pixels wide m_pen [2]. createpen (ps_dash, 1, RGB (255, 0, 0); // The red dotted line, which must be a pixel width m_pen [3]. createpen (ps_dot, 1, RGB (255,); // blue dot line, which must be a pixel width m_pen [4]. createpen (ps_dashdotdot, 1, RGB (255, 0, 0); // red dotted line, must be a pixel width // draw a polygon point array m_point [0]. X = 10; m_point [0]. y = 100; m_point [1]. X = 10; m_point [1]. y = 120; m_point [2]. X = 100; m_point [2]. y = 105; m_point [3]. X = 170; m_point [3]. y = 120; m_point [4]. X = 170; m_point [4]. y = 100 ;}
* ** DLG. cpp onpaint Function

Void clinetestdlg: onpaint () {cpaintdc DC (this); // The device context cbrush brush (RGB (190,190,190) used for painting; // The image brush DC. selectObject (& brush); // select dcdrawline (& DC); drawpolyline (& DC); drawpolygon (& DC); drawrect (& DC ); drawroundrect (& DC); drawellipse (& DC); drawarc (& DC); drawanglearc (& DC); If (isiconic () {cpaintdc (this ); // The device context sendmessage (wm_iconerasebkgnd, reinterpret_cast <wparam> (DC. getsafehdc (), 0); // center the icon in the rectangle of the workspace int cxicon = getsystemmetrics (sm_cxicon); int cyicon = getsystemmetrics (sm_cyicon); crect rect; getclientrect (& rect); int x = (rect. width ()-cxicon + 1)/2; int y = (rect. height ()-cyicon + 1)/2; // draw the icon DC. drawicon (X, Y, m_hicon);} else {cdialogex: onpaint ();}}
Define plotting functions:

// Draw the line function void clinetestdlg: drawline (CDC * PDC) {cpen * oldpen = PDC-> SelectObject (& m_pen [0]); // Save the original DC paint brush for (INT I = 0; I <5; I ++) {PDC-> SelectObject (& m_pen [I]); // select dcpdc-> moveTo (20, 20 + I * 15) as the paint brush; // set the current DC point PDC-> lineto (170,20 + I * 15 ); // draw a straight line} PDC-> SelectObject (oldpen); // restore the original DC paint brush} // draw the line void clinetestdlg: drawpolyline (CDC * PDC) {cpen * oldpen = PDC-> SelectObject (& m_pen [0]); // Save the original DC paint brush for (INT I = 0; I <5; I ++) {for (Int J = 0; j <5; j ++) {m_point [J]. Y + = 35; // 35 pixels down each time} PDC-> SelectObject (& m_pen [I]); PDC-> polyline (m_point, 5 );} PDC-> SelectObject (oldpen);} // draw the polygon void clinetestdlg: drawpolygon (CDC * PDC) {for (Int J = 0; j <5; j ++) m_point [J]. Y + = 75; // the point in the array moves down 75 pixels cpen * oldpen = PDC-> SelectObject (& m_pen [0]); // Save the original DC paint brush for (INT I = 0; I <5; I ++) {for (Int J = 0; j <5; j ++) {m_point [J]. Y + = 35; // 35 pixels down each drawing} PDC-> SelectObject (& m_pen [I]); PDC-> polygon (m_point, 5 ); // draw polygon} PDC-> SelectObject (oldpen);} // draw the rectangle void clinetestdlg: drawrect (CDC * PDC) {crect rect (, 20 ); cpen * oldpen = PDC-> SelectObject (& m_pen [0]); For (INT I = 0; I <5; I ++) {rect. offsetrect (80, 0); // right shift of each drawing 80 pixels PDC-> SelectObject (& m_pen [I]); PDC-> rectangle (& rect ); // draw rectangle} PDC-> SelectObject (oldpen);} // draw the rounded rectangle void clinetestdlg: drawroundrect (CDC * PDC) {crect rect (150,150,220,230 ); cpen * oldpen = PDC-> SelectObject (& m_pen [0]); For (INT I = 0; I <5; I ++) {rect. offsetrect (); // each drawing shifts 80 pixels to the right of PDC-> SelectObject (& m_pen [I]); PDC-> roundrect (& rect, cpoint ));} PDC-> SelectObject (oldpen);} // draw the Oval void clinetestdlg: drawellipse (CDC * PDC) {crect rect (150,260,220,310 ); cpen * oldpen = PDC-> SelectObject (& m_pen [0]); For (INT I = 0; I <5; I ++) {rect. offsetrect (80, 0); PDC-> SelectObject (& m_pen [I]); PDC-> ellipse (& rect);} PDC-> SelectObject (oldpen );} // draw the arc void clinetestdlg: drawarc (CDC * PDC) {crect rect (150,400,220,460); cpoint ptstart (170,440); cpoint ptend (210,400 ); cpen * oldpen = PDC-> SelectObject (& m_pen [0]); For (INT I = 0; I <5; I ++) {rect. offsetrect (80, 0); ptstart + = cpoint (80, 0); ptend + = cpoint (80, 0); PDC-> SelectObject (& m_pen [I]); PDC-> arc (& rect, ptstart, ptend);} PDC-> SelectObject (oldpen);} // draw a straight line and an arc void clinetestdlg: drawanglearc (CDC * PDC) {crect rect (150,500,220,560); cpoint ptcenter (185,600); cpoint ptstart (150,630); int r = 30; float anglestart = 0; float angleend = 180; cpen * oldpen = PDC-> SelectObject (& m_pen [0]); For (INT I = 0; I <5; I ++) {rect. offsetrect (80, 0); ptstart + = cpoint (80, 0); PDC-> moveTo (ptstart); PDC-> SelectObject (& m_pen [I]); PDC-> anglearc (ptcenter. x, ptcenter. y, R, anglestart, angleend);} PDC-> SelectObject (oldpen );}


Running effect:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.