MFC 根据定时器 ICON 移动  定时器的应用_新窗口

▲ 会从做向右跑动

构造函数:

void CMFCApplication1View::OnDraw(CDC* pDC)
{
    CMFCApplication1Doc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;

    // 绘制代码
    pDC->DrawIcon(x1, y1, icon[0]);
    pDC->DrawIcon(x2, y2 + 50, icon[1]);
    pDC->DrawIcon(x3, y3 + 100, icon[2]);
    pDC->DrawIcon(x4, y4 + 150, icon[3]);
}

实现:

int CMFCApplication1View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CView::OnCreate(lpCreateStruct) == -1)
        return -1;

    SetTimer(1, 500, NULL); // 定时器

    return 0;
}


void CMFCApplication1View::OnTimer(UINT_PTR nIDEvent)
{
    switch (nIDEvent)
    {
    case 1:
    {
        x1 += 20;
        x2 += 40;
        x3 += 60;
        x4 += 80;
        Invalidate(); // 更新窗口

        // 获取客户区范围
        CRect rect;
        GetClientRect(&rect);
        if (x1 > rect.Width()) {
            x1 = 30;
        }
        if (x2 > rect.Width()) {
            x2 = 30;
        }
        if (x3 > rect.Width()) {
            x3 = 30;
        }
        if (x4 > rect.Width()) {
            x4 = 30;
        }
    }
    break;
    default:
        break;
    }


    CView::OnTimer(nIDEvent);
}


void CMFCApplication1View::On32771()
{
    x1 = x2 = x3 = x4 = 30;
    y1 = y2 = y3 = y4 = 30;

    // 刷新窗口
    Invalidate();
}