如何创建一个波形显示

类别:VC语言 点击:0 评论:0 推荐:

1>目的:创建一个波形显示器
2>步骤:*从CWnd继承一个类,包含四个函数分别是:UINT SetPos(UINT nPos) ,设置位置点;void
         SetRange(UINT nLower,UINT nUpper),设置波形条高度范围;绘制波形条void DrawBar();
  更新绘制void InvalidateCtrl();重载virtual BOOL Create(LPCTSTR lpszClassName,
         LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd,
         UINT nID, CCreateContext* pContext = NULL);映射afx_msg void OnPaint()消息函数。
       
        *定义五个变量UINT m_nVertical;范围最小值UINT  m_nLower;范围最大值UINT  m_nUpper;
  位置UINT  m_nPos;设备CDC m_MemDC;位图CBitmap m_Bitmap;
        
        *初始化数据
CAduioShow::CAduioShow()
      {
 file://初始化数据 高为100,底为0,开始点为0
 m_nPos=0;
 m_nLower=0;
 m_nUpper=100;
      }

       *//下面注册应用类,风格设置为如果窗口的高,宽改变时从绘制整个窗口
BOOL CAduioShow::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const
                        RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
 // TODO: Add your specialized code here and/or call the base class
 file://下面注册类,风格设置为如果窗口的高,宽改变时从绘制整个窗口
 static CString  classname=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW);
      
 return CWnd::CreateEx(WS_EX_CLIENTEDGE|WS_EX_STATICEDGE,classname,NULL,
                    dwStyle,rect.left,rect.top,rect.right-rect.left,
        rect.bottom-rect.top,
                              pParentWnd->GetSafeHwnd(),(HMENU)nID);
file://(HMENU)nID为程序中的图片框id


}

 

      *设置变动条的动态位置,并返回初始位置
UINT  CAduioShow::SetPos(UINT nPos)
{
 file://下面设置位置最大及最小值
 if (nPos>m_nUpper)
  nPos=m_nUpper;
 if(nPos<m_nLower)
        nPos=m_nLower;

 UINT nOld=m_nPos;
      m_nPos=nPos;
    file://绘制变动条
   DrawBar();
  
   Invalidate();;

   return nOld;
}

      *设置变动条的范围
void CAduioShow::SetRange(UINT nLower,UINT nUpper)
{
 m_nLower=nLower;
 m_nUpper=nUpper;

 InvalidateCtrl();
}
 
     *变换坐标
void CAduioShow::OnPaint()
{
 CPaintDC dc(this); // device context for painting
 
 // TODO: Add your message handler code here
 CRect rcClient;
 GetClientRect(rcClient);

 if(m_MemDC.GetSafeHdc()!=NULL)
 {
  dc.BitBlt(0,0,rcClient.Width(),rcClient.Height(),&m_MemDC,0,0,SRCCOPY);
 }

 // Do not call CWnd::OnPaint() for painting messages
}

 

      *绘制条形图
void CAduioShow::DrawBar()
{
 UINT nRange=m_nUpper-m_nLower;
 CRect reClient;
 GetClientRect(reClient);

 if(m_MemDC.GetSafeHdc()!=NULL)
 {
  m_MemDC.BitBlt(0,0,reClient.Width(),reClient.Height(),&m_MemDC,4,0,SRCCOPY);

                CRect reTop(reClient.right-4,0,reClient.right-2,reClient.bottom);
                file://计算条形图的高度
  reTop.top =(long)(((float)(m_nPos-m_nLower)/nRange)*reClient.Height());
                file://计算条形图的宽度
  CRect reRight=reClient;
        reRight.left=reRight.right-4;
                file://绘制
        CBrush   bkBrush(HS_HORIZONTAL,RGB(0,128,128));
  m_MemDC.FillRect(reRight,&bkBrush);
       
  CBrush   bBrush(RGB(0,255,0));
  m_MemDC.FillRect(reTop,&bBrush);
 }

}

       *
void CAduioShow::InvalidateCtrl()
{
 CClientDC dc(this);
 CRect reClient;
 GetClientRect(reClient);
         file://无效更新
 if(m_MemDC.GetSafeHdc()==NULL)
 {
  m_MemDC.CreateCompatibleDC(&dc);
  m_Bitmap.CreateCompatibleBitmap(&dc,reClient.Width(),reClient.Height());
  m_MemDC.SelectObject(m_Bitmap);
  m_MemDC.SetBkColor(RGB(0,0,0));
  CBrush bkBrush(HS_HORIZONTAL,RGB(0,128,0));
  m_MemDC.FillRect(reClient,&bkBrush);
 }
 InvalidateRect(reClient);

}

 


 

本文地址:http://com.8s8s.com/it/it3883.htm