Dynamic curve drawing method

Source: Internet
Author: User

In experiments and production, we often need to monitor the monitored objects in real time, for example, to collect real-time data for environmental factors such as the site temperature, and then transmit the data to the master control computer, it is displayed in a dynamic curve to facilitate people's understanding and control of the site.

2. There are four ways to draw dynamic curves using Visual C ++. (1) Use deactivating. (2) Use redrawing. Based on different principles, repainting is divided into three types.

2.1. In fact, the method of hiding is very simple, mainly using the CDC class member function setrop2. The prototype of this function is int setrop2 (INT ndrawmode ). The ndrawmode parameter is the new draw method. This function is used to set the current painting mode. The painting mode shows how the paint brush and the filled object are combined with the existing color on the screen surface. We use the r2_xorpen method-the combination of paint brush color and screen color, but not both, the final pixel = paint brush XOR screen pixel. To achieve a dynamic curve, you only need to draw it again at the position of the original curve in this way to hide the original curve and then draw a new curve. This is repeated, to form a dynamic curve.

2.2 re-painting. The member function bitblt of the CDC class must be used in the re-painting process. The prototype is bool bitblt (int x, int y, int nwidth, int nheight, CDC * psrcdc, int xsrc, int ysrc, DWORD dwdrop ). This function copies a bitmap from the source device description table to the device description table of the CDC.

2.2.1 Principle 1: Set a background device environment. All the plotting work is completed in the background, and then copied to the current device environment through the bitblt function. This method requires that the background be re-painted every time, including coordinates, character descriptions, curves, and so on.

2.2.2 application example:

The ontimer function redraws at intervals of time.

Void cdrawview: ontimer (uint nidevent)

{

// Todo: add your message handler code here and/or call default

Drawpicture (); // drawing function

}

Void cdrawview: drawpicture ()

{

Pbackdc-> patblt (0, 0, winx, winy, patcopy );

// Draw the coordinate axis and Scale

Drawcoordinate (pbackdc, blackpen, mainwindowleft, mainwindowbottom-mainheight, mainwindowleft + mainwidth, mainwindowbottom );

// Mark the scale value of X coordinate

Writecoordinatex (pbackdc, blackpen, mainwindowleft, mainwindowleft + mainwidth, mainwindowbottom );

// Mark the Y coordinate scale value

Writecoordinatey (pbackdc, blackpen, mainwindowleft, mainwindowbottom );

// Output the actual width value

Writemessage (pbackdc, mainwindowleft, mainwindowbottom, mainheight );

If (pbackdc! = NULL)

{

// Draw a graph

Drawgraph (pbackdc, redpen, mainwindowleft + mainwidth, mainwindowbottom );

CDC * PDC = getdc ();

If (PDC! = NULL)

{

// Call the ondraw () function to display the curve on the screen.

Ondraw (PDC );

Releasedc (PDC );

}

}

}

Void cdrawview: ondraw (CDC * PDC)

{

Cdrawdoc * pdoc = getdocument ();

Assert_valid (pdoc );

// Todo: Add draw code for native data here

If (PDC! = NULL)

PDC-> bitblt (0, 0, winx, winy, pbackdc, 0, 0, srccopy );

}

2.2.3 Principle 2: Set a background device Environment (2) with the same size as the current device Environment (1 ). However, the main plotting work is performed in the current device environment. For example, set the environment size of the current device to 200x100, the spacing offset between curve points to 5, and the coordinates of the top left Vertex on the screen to 200,100 ). When the x-axis of the curve is less than 400, it is in the current device environment.Draw a curveWhen the x-axis is greater than 400, the lower right part of the coordinate point (205,100) is copied to the rectangle area by using the bitblt function) in the background device environment, then clear the current device environment, and then use the bitblt function to copy the curve from the background device environment to the current device environment with a rectangle range of (200,100,195,100, finally, draw the current data point at the end of the curve.

(200,100) (0, 0)

200x100 200x100

Figure 1: Current Device environment Figure 2: Background device Environment

2.2.4 application example:

Void canimatelineview: ontimer (uint nidevent)

{

// Todo: add your message handler code here and/or call default

Cclientdc DC (this );

Static int x = 200;

Static int y = 200;

Cpen pen1 (ps_solid, 0, RGB (255,255,255 ));

Cpen * oldpen1 = NULL;

Oldpen1 = Dc. SelectObject (& pen1 );

X = x + offsetx;

If (x <= 400)

{

DC. moveTo (X-offsetx, y );

Y = 200-rand () % 90;

DC. lineto (x, y );

}

Else {

Crect rect (0, 200,100,400,200 );

Cbrush bkbrush (hs_cross, RGB (0,128, 0 ));

M_dc.bitblt (195,100, 205,100, & DC, srccopy );

DC. setbkcolor (RGB (0, 0 ));

DC. fillrect (rect, & bkbrush );

DC. bitblt (200,100,195,100, & m_dc, srccopy );

DC. moveTo (395, y );

Y = 200-rand () % 90;

DC. lineto (400, Y );}

DC. SelectObject (oldpen1 );

Cview: ontimer (nidevent );

}

2.2.5 Principle 3: Set a background device environment, which is twice the width of the current device environment and has the same height. Plotting is performed in the background device environment. For example, if the current device environment is set to 200x100 (1), the background device environment is 400x100 (2), and the distance between curve points is offset = 10, the coordinates of the top-left Vertex on the screen are (200,100 ). ① When the horizontal coordinate of the curve drawn in the background device environment is 0 <x <200, you only need to copy the first half of the area to the current device environment; ② when the X coordinate is 200 <x <400, move the rectangular area with a width of 200 to the left half of the area and copy the 10 units to the current device environment. ③ when x> 400, copy the second half to the current device environment and clear the first half. ④ When 0 <x <2nd is performed for 200 times, 10 units are moved to the second half of the rectangle area to the current device environment, draw the real-time curve in the first half and copy it to the end of the current device environment; ⑤ when 200 <x <400, clear the second half, move the First Half of the rectangle area to the current device environment, draw a real-time curve in the second half, and then copy it to the end of the current device environment; ⑥ when x> 400 again, return to ④, so that the dynamic curve can be formed after the loop.

(200,100)

200 × 100

Figure 1: Current Device Environment

200 in width and keeps moving to the right

(0, 0) 200 400

(200 + 200) x 100

Figure 2: Background device Environment

2.2.6 application example:

Void clineview: ontimer (uint nidevent)

{

// Todo: add your message handler code here and/or call default

Static int x = 0;

Static int y = 0;

Static int control = 0;

Cclientdc DC (this );

Crect rect (0, 0, 200,100 );

Crect rect1 (200,0, 400,100 );

Cbrush bkbrush (hs_cross, RGB (0,128, 0 ));

X = x + offset;

Cpen pen1 (ps_solid, 0, RGB (255,255,255 ));

Cpen * oldpen1 = NULL;

Oldpen1 = m_dc.selectobject (& pen1 );

If (x <= 200)

{

M_dc.moveto (X-offset, y );

Y = 100-rand () % 90;

M_dc.lineto (x, y );

If (control = 0)

DC. bitblt (200,100,200,100, & m_dc, srccopy );

Else

{

DC. bitblt (100,-X, & m_dc, x +, 0, srccopy );

DC. bitblt (400-x, 100, X, 100, & m_dc, srccopy );

}

}

If (x <= 400 & x> 200)

{

If (control = 1)

{

DC. bitblt (200,100,200,100, & m_dc, x-200, 0, srccopy );

M_dc.fillrect (rect1, & bkbrush );

Control = 0;

}

M_dc.moveto (X-offset, y );

Y = 100-rand () % 90;

M_dc.lineto (x, y );

DC. bitblt (200,100,200,100, & m_dc, x-200, 0, srccopy );

}

If (x> 400)

{

X = 0;

DC. bitblt (100,-X, & m_dc, x +, 0, srccopy );

M_dc.setbkcolor (RGB (0, 0, 0 ));

M_dc.fillrect (rect, & bkbrush );

Control = 1;

}

DC. SelectObject (oldpen1 );

Cview: ontimer (nidevent );

}

2.3 conclusion

Though simple, The method does not apply to real-time monitoring. When the graphic area requires parameter description and time to move at the same time, it is troublesome to implement the 2nd and 3rd methods in the re-painting. The 1st re-painting methods have strong applicability and the best 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.