winapi - 在父窗口上绘制带有图像的半透明子窗口

标签 winapi animation transparency layered-windows ws-ex-layered

我需要在WS_OVERLAPPED 窗口中制作小鸟动画(如下图)。动画由8张图片表示:

Animation

图像中的蓝色(即 RGB(0, 255, 255))必须是透明的(见下面的屏幕截图)。

我想使用带有 WS_EX_LAYERED 参数的 CreateWindowEx()(鸟将由分层窗口表示)来执行此操作。不幸的是,鸟必须是 WS_CHILD。混合 WS_EX_LAYERED | WS_CHILD is not legalWindows 7 中:

Windows 8: The WS_EX_LAYERED style is supported for top-level windows and child windows. Previous Windows versions support WS_EX_LAYERED only for top-level windows.

最终效果应该是这样的(我已经绘制了窗口的背景 - 唯一的问题是鸟):

enter image description here

我怎样才能达到这个效果?如何在父窗口中为小鸟设置动画?
如果您有任何想法如何实现透明背景颜色的小鸟动画,请分享。

最佳答案

由于即使没有与窗口交互也会完成动画,因此我们需要一个计时器:

case WM_CREATE:
    // load resources
    SetTimer(hwnd, 0, 250, NULL); // set timer to 250 ms
return 0;

...

case WM_DESTROY:
    KillTimer(hwnd, 0);
    // release the resources
return 0;

我们可以在每次计时器滴答时使整个窗口无效,但最好只重绘需要的部分。我们还将在此处更新当前帧数:

case WM_TIMER:
    frame_number++;
    if (frame_number >= 8)
        frame_number = 0;

    RECT rc = { 30, 30, 80, 80 }; // a rectangle from (30,30) to (80,80)
    InvalidateRect(hwnd, &rc, FALSE);
return 0;

然后,我们在 WM_PAINT 处理程序中绘制当前帧:

case WM_PAINT:
    // draw the sky

    SelectObject(hDCMem, hBird);
    TransparentBlt(hDC, 30, 30, 50, 50, hDCMem, frame_number * 51, 0, 50, 50, RGB(0, 255, 255)); // 51 is 50 (side of a bird frame) + 1 (gap between the frames)

    // draw the rest
return 0;

关于winapi - 在父窗口上绘制带有图像的半透明子窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15324807/

相关文章:

c# - 从内存中用 C# 播放 MIDI 文件

python - 在 Python 中使用 WlanScan 强制 wifi 扫描

ios - SWIFT ImageView 上的随机/不规则脉冲动画

opengl-es-2.0 - 如何停止透​​明纹理上的 OpenGL 背景流血

java - 为什么我的 'transparent' 叠加 View 不透明?

TEXT 宏中的 C++ 字符串变量

windows - 如何人为地将CPU负载到某个百分点?

react-native - 动画从 360 度旋转到 0 度 - iOS 传感器

javascript - ajax 显示新帖子向下滑动

iOS SpriteKit 如何使描边颜色透明?