140上位机VC MFC绘制动态的背景图
140上位机VC MFC绘制动态的背景图
功能展示
通过设备上下文CDC绘制动态的背景节省空间的同时,也会吸引用户的眼光,给程序增添色彩,我们当前例程通过一数组动态画线,实现动态的图片,效果如图; 要点提示 根据数组内的坐标点画线,得结合定时器才能够实现动态效果,我们可以触发一个200毫秒的定时器,在定时器内绘制不同坐标的线,就可以实现线的动态效果;
实现功能 1.新建基于对话框的应用程序 2.添加三个变量private: BOOL m_bChangeDisplay; //显示切换 UINT m_PointNum;//数组点数
CPoint m_PointArray[40];//数组 并在对话框的OnInitDialog()函数中初始化 - m_PointNum = 40;
- m_bChangeDisplay = FALSE;
- double lpi = 0;
- int nX = 140;
- m_PointArray[0] = CPoint(nX,0);
- for(int i=1;i<m_PointNum;i++)
- {lpi+=(2*PI/m_PointNum);
- if(lpi<=2*PI/4)
- {
- m_PointArray[i] = CPoint(nX+nX*sin(2*i*PI/m_PointNum),nX-nX*cos(2*i*PI/m_PointNum));
- }
- if(lpi>2*PI/4 && lpi<=2*PI/2)
- {
- m_PointArray[i] = CPoint(nX+nX*sin(PI-2*i*PI/m_PointNum),nX+nX*cos(PI-2*i*PI/m_PointNum));
- }
- if(lpi>2*PI/2 && lpi<=2*PI*3/4)
- {
- m_PointArray[i] = CPoint(nX-nX*sin(2*i*PI/m_PointNum-2*PI/2),nX+nX*cos(2*i*PI/m_PointNum-2*PI/2));
- }
- if(lpi>2*PI*3/4 && lpi<=2*PI)
- {
- m_PointArray[i] = CPoint(nX-nX*sin(2*PI-2*i*PI/m_PointNum),nX-nX*cos(2*PI-2*i*PI/m_PointNum));
- }
- }
- SetTimer(1,150,NULL);
复制代码- 添加定时器处理消息,在里面实现绘制线
- void CGkbc8Dlg::OnTimer(UINT nIDEvent)
- {
- CRect rc;
- GetClientRect(&rc);
- CDC* pDC = GetDC();
- pDC->FillRect(&rc,CBrush::FromHandle((HBRUSH )GetStockObject(BLACK_BRUSH)));
- CPen pen,*pOldPen;
- pen.CreatePen(PS_SOLID,1,RGB(255,0,0));
- pOldPen=pDC->SelectObject(&pen);
- if(m_bChangeDisplay)
- {
- for(int j=0;j<m_PointNum;j=j+2)
- {
- for(int k=0;k<=j;k=k+2)
- {
- pDC->MoveTo(m_PointArray[j]);
- pDC->LineTo(m_PointArray[k]);
- }
- }
- }
- else
- {
- for(int j=1;j<=m_PointNum;j=j+2)
- {
- for(int k=1;k<=j;k=k+2)
- {
- pDC->MoveTo(m_PointArray[j]);
- pDC->LineTo(m_PointArray[k]);
- }
- }
- }
- m_bChangeDisplay = !m_bChangeDisplay;
- pDC->SelectObject(pOldPen);
- ReleaseDC(pDC);
- CDialog::OnTimer(nIDEvent);
- }
复制代码
绘制给用到一三角函数得添加头文件#include <math.h> #define PI 2.0*asin(1.0) 我们来演示下功能实现的整个过程
|