MFCpicture waveform drawing

Let’s spit out CSDN first. After the update, there are various bugs, the copied things can’t be undone, and the deletion is incomplete. There are often problems with the format when it is written, the webpage will die, and then it needs to be rewritten. It is not as good as the previous old version, except that it can copy pictures, it is useless. It's not that we don't want to write good articles. It takes time to write good articles. If someone like you has written an article for a few hours like this, who is interested in writing it a second time?

First add a picture control to the interface.
initialization
  // Construct random number generator with time as seed   
    srand((unsigned)time(NULL));   
  
    // Start the timer, the ID is 1, and the timing time is 200ms   
    SetTimer(1, 200, NULL);
Generate timer events via Class Wizard
void Cpicture绘制Dlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: add message handler code and/or call defaults here

	CStatic *m_picDraw = (CStatic*)GetDlgItem(IDC_STATIC); //Load picture control  

	 CRect rectPicture;   
  
    // Move all elements in the array forward by one unit, discard the first element   
    for (int i=0; i<POINT_COUNT-1; i++)   
    {   
        m_nzValues[i] = m_nzValues[i+1];   
    }   
    // Assign a random number (integer) within 80 to the last element   
    m_nzValues[POINT_COUNT-1] = rand() % 80;   
  
    // Get the client area coordinates of the drawing control   
    // (The client area coordinates take the upper left corner of the window as the origin, which is different from the screen coordinates that take the upper left corner of the screen as the origin)   
    m_picDraw->GetClientRect(&rectPicture);   
  // this->GetClientRect(&rectPicture);
    // draw waveform   
    drawwave(m_picDraw->GetDC(), rectPicture);   
   //drawwave(this->GetDC(), rectPicture);
	CDialogEx::OnTimer(nIDEvent);
}
Write a drawing function
void Cpicture绘制Dlg::drawwave(CDC *pDC, CRect &rectPicture)
{

	float fDeltaX; // coordinate distance between two adjacent drawing points on the x-axis   
    float fDeltaY; // the coordinate value corresponding to each logical unit of the y-axis   
    int nX; // used to store the abscissa of the drawing point when connecting   
    int nY; // used to store the ordinate of the drawing point when connecting   
    CPen newPen; // for creating a new pen   
    CPen *pOldPen; // used to store the old pen   
    CBrush newBrush; // used to create a new brush   
    CBrush *pOldBrush; // used to store old brushes   
  
    // Calculate fDeltaX and fDeltaY   
    fDeltaX = (float)rectPicture.Width() / (POINT_COUNT - 1);   
    fDeltaY = (float)rectPicture.Height() / 80;   
  
    // Create new black brush   
    newBrush.CreateSolidBrush(RGB(100,100,100));   
    // Select the new brush and save the pointer of the old brush to pOldBrush   
    pOldBrush = pDC->SelectObject(&newBrush);   
    // Fill the drawing control with black with a black brush to form a black background   
    pDC->Rectangle(rectPicture);   
    // restore old brush   
    pDC->SelectObject(pOldBrush);   
    // delete the new brush   
    newBrush.DeleteObject();   
  
    // Create a solid brush with a thickness of 1 and a color of green   
    newPen.CreatePen(PS_SOLID, 1, RGB(255,100,50));   
    // Select the new brush and save the pointer of the old brush to pOldPen   
    pOldPen = pDC->SelectObject(&newPen);   
  
    // Move the current point to the lower left corner of the drawing control window as the starting point of the waveform   
    pDC->MoveTo(rectPicture.left, rectPicture.bottom);   
    // Calculate the coordinate position corresponding to each point in the m_nzValues ​​array, and connect them in turn to finally form a curve   
    for (int i=0; i<POINT_COUNT; i++)   
    {   
        nX = rectPicture.left -1+ (int)(i * fDeltaX);   
		
        nY = rectPicture.bottom -1- (int)(m_nzValues[i] * fDeltaY);   
        pDC-> LineTo (nX, nY);   
    }   
  
    // restore old brush   
    pDC->SelectObject(pOldPen);   
    // delete the new brush   
    newPen.DeleteObject();   

}

Effect



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325566986&siteId=291194637