【问题标题】:Animating a CImage to fade in on creation为 CImage 设置动画以在创建时淡入
【发布时间】:2013-06-09 01:36:38
【问题描述】:

我目前有一个CView,它由一个集中的CImage 组成,它会在我的应用程序立即加载时显示。

我想通过在启动时使CImage 淡入而不是简单地显示来美化它(我已经有一个启动屏幕,所以我不问如何为CWnd 设置动画)。

为了显示我的图片,我重写了CView::OnDraw方法,如下:

void CWelcomeView::OnDraw( _In_ CDC* pDC ) 
{
    CView::OnDraw( pDC );

    static CImage backgroundImage;
    static bool bImageLoaded = false;

    if( !bImageLoaded )
    {
        backgroundImage.LoadFromResource( AfxGetInstanceHandle(), IDB_WELCOMESCREEN );

        bImageLoaded = true;
    }

    CRect clientRect;
    GetClientRect( &clientRect );

    // The detination rectangle should be the same size as the image:
    POINT pointTopLeft = {(clientRect.Width()-backgroundImage.GetWidth())/2, (clientRect.Height()-backgroundImage.GetHeight())/2};
    CRect destRect( pointTopLeft, CSize( backgroundImage.GetWidth(), backgroundImage.GetHeight() ) );

    // Draw the image in the right space:
    backgroundImage.Draw( pDC->GetSafeHdc(), destRect );
}

现在我不完全确定如何在创作时淡化它。我目前的行动计划是创建一个基于std::chrono::steady_clock 的计时器,并根据当前时间点和时钟开始时间之间的差异更改CImage 的alpha 值(我将作为成员保留)多变的)。

但是,我不确定如何更改整个图像的 alpha 值?

提前致谢!

【问题讨论】:

    标签: c++ image winapi mfc


    【解决方案1】:

    不要使用backgroundImage.Draw(),而是使用AlphaBlend

    backgroundImage.AlphaBlend(pDC->GetSafeHdc(), destRect
        , CRect(0, 0, backgroundImage.GetWidth(), backgroundImage.GetHeight())
        , bySrcAlpha);
    

    bySrcAlpha 的值从 255 开始,并在计时器函数中将其递减适当的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-28
      • 1970-01-01
      • 2011-06-30
      • 1970-01-01
      • 1970-01-01
      • 2015-03-04
      • 2020-05-30
      • 1970-01-01
      相关资源
      最近更新 更多