个人mfc总结1-动画绘制

 

   Task 1

1.       Draw image, paint a section of a bmp image, implement animation(Tuesday).
    Conclusion:

1, add a bmp to resource file.

    right click top node in resource view->Add resource->bit map->import ...

2, Draw image. add following code in CView->OnDraw():

//Draw bitmap
        CBitmap bitmap;
        CDC dcMemory;
         bitmap.LoadBitmapW(IDB_BITMAP1); //IDB_BITMAP1 IS id of the bitmap you added.
         dcMemory.CreateCompatibleDC(pDC);
         dcMemory.SelectObject(&bitmap);
         pDC->BitBlt(100,100,12,22, &dcMemory, CMainFrame::image_x_index * 12,0,SRCCOPY)

 

3, about animation.

    add a WM_TIMER EVENT to mainframe(otherwise there will be some problems) in class view -> properies. VS2008 will do following 3 steps:

    a, add ON_WM_TIMER() to BEGIN_MESSAGE_MAP(XX,XX)

    b, add declaration afx_msg void OnTimer(UINT_PTR nIDEvent);  to Mainfram.h

    c, add functon definition void CMainFrame::OnTimer(UINT_PTR nIDEvent)  to Mainframe.cpp

 

   you should do following steps to make a timer work.

    1, in a function(such as in a on_lbuttondown() event call), call SetTimer(1, 1000,NULL) to start timer.

    2, in function OnTimer(UINT_PTR nIDEvent) , add your code to do what you want to do when timer event comes.

 

To make animation alive, add code: this->invalidate() or this->RedrawWindow()  in mainframe::OnTimer(nIDEvent).


     Task 2

2.Click event detection:verify which area does the mouse clicked, and implement a toggle button.
    Conclusion:

1, this task is so easy. nothing to say.

2, toggle button is so easy, i skipped it, is ok?

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MFC可以通过以下步骤实现动画效果: 1. 定义一个计时器(Timer):在MFC中使用SetTimer函数来创建一个计时器,该函数有两个参数,一个是计时器ID,一个是计时器间隔时间,单位为毫秒。 2. 实现OnTimer函数:当计时器到期时,MFC会自动调用OnTimer函数,我们可以在该函数中实现动画效果。在OnTimer函数中,可以使用GDI+绘制动画,也可以使用位图动画。 3. 重绘窗口:在OnTimer函数中实现动画效果后,需要调用Invalidate函数来重绘窗口,从而使动画效果显示在窗口上。 4. 关闭计时器:当动画效果结束时,需要关闭计时器。可以使用KillTimer函数来关闭计时器。 下面是一个简单的例子,演示如何使用MFC实现位图动画: ```c++ // 定义一个类变量 CBitmap m_bitmap; // 在OnInitDialog函数中加载位图 m_bitmap.LoadBitmap(IDB_BITMAP1); // 在OnTimer函数中切换位图 void CMyDialog::OnTimer(UINT_PTR nIDEvent) { static int frame = 0; frame++; if (frame > 3) frame = 0; // 根据帧数切换位图 switch (frame) { case 0: m_bitmap.LoadBitmap(IDB_BITMAP1); break; case 1: m_bitmap.LoadBitmap(IDB_BITMAP2); break; case 2: m_bitmap.LoadBitmap(IDB_BITMAP3); break; case 3: m_bitmap.LoadBitmap(IDB_BITMAP4); break; } // 重绘窗口 Invalidate(); CDialogEx::OnTimer(nIDEvent); } // 在OnPaint函数中绘制位图 void CMyDialog::OnPaint() { CPaintDC dc(this); CDC memDC; memDC.CreateCompatibleDC(&dc); memDC.SelectObject(&m_bitmap); BITMAP bitmap; m_bitmap.GetBitmap(&bitmap); dc.BitBlt(0, 0, bitmap.bmWidth, bitmap.bmHeight, &memDC, 0, 0, SRCCOPY); } ``` 这个例子中,我们定义了一个位图变量m_bitmap,使用LoadBitmap函数在OnInitDialog函数中加载位图。在OnTimer函数中,我们使用switch语句根据帧数切换位图,并在最后调用Invalidate函数来重绘窗口。在OnPaint函数中,我们使用BitBlt函数绘制位图。 注意,以上代码只是一个简单的例子,实际应用中需要根据具体情况进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值