我需要在WS_OVERLAPPED
窗口中制作小鸟动画(如下图)。动画由8张图片表示:
图像中的蓝色(即 RGB(0, 255, 255)
)必须是透明的(见下面的屏幕截图)。
我想使用带有 WS_EX_LAYERED
参数的 CreateWindowEx()
(鸟将由分层窗口表示)来执行此操作。不幸的是,鸟必须是 WS_CHILD
。混合 WS_EX_LAYERED | WS_CHILD
is not legal在 Windows 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.
最终效果应该是这样的(我已经绘制了窗口的背景 - 唯一的问题是鸟):
我怎样才能达到这个效果?如何在父窗口中为小鸟设置动画?
如果您有任何想法如何实现透明背景颜色的小鸟动画,请分享。
最佳答案
由于即使没有与窗口交互也会完成动画,因此我们需要一个计时器:
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/