使用HDC画音频波形图

时间:2021-12-09 10:24:23
//第一段:(循环处理)HDC hdc = ::GetDC(m_PlaySt.GetSafeHwnd());
DrawWaveR(0, 0, m_rectBoard.Width(), m_rectBoard.Height(), hdc, (short*)m_audio->getCapBuf(), m_audio->getCapBufLen(), 0);
::ReleaseDC(m_PlaySt.GetSafeHwnd(), hdc);//必须和GetDC配对//第二段:画图函数void WaveCanvas::DrawWaveR(HDC hDC/*CDC *pDC*/, /*CRect rectBoard, */short* pData, DWORD dwDrawLength, int type){if (NULL == pData){return;}int m_beginX = 100;int m_beginY = 100;//创建一个刷子HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 0));HBRUSH hOldBrush = (HBRUSH)SelectObject(hDC, hBrush);//绘制矩形Rectangle(hDC, m_x, m_y, m_width, m_height);//创建实线,宽度为1HPEN hPen = CreatePen(PS_SOLID, 1, RGB(0, 255, 0));//将笔选入DCHPEN hOldPen = (HPEN)SelectObject(hDC, hPen);int size = dwDrawLength / 2;int yy = /*rectBoard.Height()*/m_height / 2;//pDC->MoveTo(rectBoard.left/*m_beginX*/, int(rectBoard.Height() / 2 + rectBoard.top/*m_beginY*/));MoveToEx(hDC, m_x, m_height / 2, NULL);int h = yy;int x, y;short sample;for (int register i = 0; i <(long)size; i++) //to draw first channel{sample = ((*(pData + i))*h) / (65535 / 2);x = int(((float)i / size)*(/*rectBoard.Width()*/m_width));y = yy - sample;//pDC->LineTo(rectBoard.left/*m_beginX*/ + x, y + rectBoard.top/*m_beginY*/);LineTo(hDC, m_x + x, m_y + y);}//pDC->SelectObject(&oldPen1);//恢复绘图对象SelectObject(hDC, hOldPen);//删除绘图对象DeleteObject(hPen);DeleteObject(hBrush);}