- 在视类头文件中定义
- // 属性
- public:
- int m_x;//图片左上位置
- int offset;//图片位移
- BITMAP bmp;
- int m_nx;
- 初始化构造函数
- C**View::C**View()
- {
- // TODO: 在此处添加构造代码
- m_x=0;
- offset=1;
- }
- 重绘OnDraw函数
- void CMoveFontView::OnDraw(CDC* pDC)
- {.....
- // TODO: 在此处为本机数据添加绘制代码
- CBitmap bit;
- bit.LoadBitmap(IDB_BITMAP2);//图片资源id
- bit.GetBitmap(&bmp);
- CDC dc;
- dc.CreateCompatibleDC(pDC);
- dc.SelectObject(&bit);
- pDC->BitBlt(m_x,0,bmp.bmWidth,bmp.bmHeight,&dc,0,0,SRCCOPY);
- }
- 在OnCreate创建计时器
- int C**View::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {......
- // TODO: Add your specialized creation code here
- SetTimer(0,10,NULL); //创建计时器
- Invalidate();
- return 0;
- }
- 在计时器OnTimer中
- void C**View::OnTimer(UINT_PTR nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- if (m_nx == m_x)//图片左边边界等于客户区右边边界时,图片从头开始
- {
- m_x = 0;
- }
- m_x += offset;
- InvalidateRect(CRect(m_x-offset,0,m_x+bmp.bmWidth,bmp.bmHeight));
- //Invalidate();
- CView::OnTimer(nIDEvent);
- }
- 利用OnSize获取客户区宽度
- void C**View::OnSize(UINT nType, int cx, int cy)
- {......
- // TODO: Add your message handler code here
- m_nx = cx;//获取客户区宽度
- }
- //该片段来自于http://www.codesnippet.cn/detail/171020136464.html
来源: http://www.codesnippet.cn/detail/171020136464.html