BitBlt 问题 GDI

发布于 2024-11-09 17:01:04 字数 762 浏览 4 评论 0原文

我在这个程序中使用 BitBlt 时遇到问题。您调整窗口的大小,椭圆也会随之调整。当然,使用普通的 hdc 方法,会出现断断续续和闪烁的情况。我尝试了 BitBlt 方法,但这也不起作用(可能是因为我做错了)。 有人可以解决我做错的事情吗?谢谢

    bool sizing; //global   
case WM_PAINT:
    {
        RECT rect;
        GetClientRect(hwnd, &rect);
        hdc = BeginPaint(hwnd, &ps);
        mem = CreateCompatibleDC(hdc);
        SelectObject(mem, GetStockObject(HOLLOW_BRUSH));
        if(sizing)
        {
        Ellipse(mem,rect.left, rect.top, rect.right, rect.bottom);
        }
    BitBlt(hdc, rect.left, rect.top, rect.left - rect.right, rect.top -rect.bottom , mem, rect.left, rect.top, SRCCOPY);
        DeleteDC(mem);
        EndPaint(hwnd, &ps);
        break;
    }
    case WM_SIZE:
        sizing = true;
        break;

I am having trouble with using BitBlt in this program. You resize the window and the ellipse resizes with it. Of course, with the normal hdc method, It is choppy and flickery. I tried the BitBlt method, but that doesn't work either (probably because im doing it wrong).
Can someone fix my what Im donig wrong? thanx

    bool sizing; //global   
case WM_PAINT:
    {
        RECT rect;
        GetClientRect(hwnd, &rect);
        hdc = BeginPaint(hwnd, &ps);
        mem = CreateCompatibleDC(hdc);
        SelectObject(mem, GetStockObject(HOLLOW_BRUSH));
        if(sizing)
        {
        Ellipse(mem,rect.left, rect.top, rect.right, rect.bottom);
        }
    BitBlt(hdc, rect.left, rect.top, rect.left - rect.right, rect.top -rect.bottom , mem, rect.left, rect.top, SRCCOPY);
        DeleteDC(mem);
        EndPaint(hwnd, &ps);
        break;
    }
    case WM_SIZE:
        sizing = true;
        break;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぇ气 2024-11-16 17:01:04

看起来您正在尝试绘制基于内存的位图,然后将其位传输到屏幕,以避免闪烁?

这里的第一个问题是处理闪烁:首先您需要重写 WM_ERASEBKGND,正如 Hans 指出的那样 - 否则 Windows 将使用任何窗口画笔(来自 RegisterClass)擦除背景,而擦除是闪烁的常见原因。

这里的下一个问题是您使用的是“空”DC:CreateCompatibleDC 为您提供了一个 DC - 这只是一个绘图上下文 - 但该上下文包含一个 1 像素 x 1 像素位图。要在屏幕外绘制,您需要一个 DC 和一个位图。请花些时间阅读CreateCompatible 的 MSDN 页面 - 它指出了这一点确切的问题。

如果您对此不熟悉,请将位图视为您在其上绘制的实际画布 - DC 只是执行该绘制的支撑结构。正如您的代码所示,您已经设置了画架和画笔 - 但您没有在任何东西上绘画。

这里通常的方法是:

  • CreateCompatibleDC 创建 DC
  • CreateCompatibleBitmap 创建位图,您将实际绘制到
  • SelectObject 将新位图绘制到内存 DC
  • 绘制到内存 DC - 绘制您从
  • BitBlt 中选择的位图把内存DC(也就是你的位图,选进去)从WM_PAINT中
  • 清除掉:SelectObject把原来的位图放回内存DC,并删除位图和DC。

It looks like you're trying to draw to a memory-based bitmap, and then bitblt that to the screen, to avoid flicker?

First issue here is dealing with flicker: first you need to override WM_ERASEBKGND as Hans points out - otherwise Windows will erase the background with whatever the window brush is (from RegisterClass), and that erasing is the usual cause of flicker.

The next problem here is that you're using an 'empty' DC: CreateCompatibleDC gives you a DC - which is just a drawing context - but the context contains a 1 pixel by 1 pixel bitmap. To draw offscreen, you need a DC and a bitmap. Do take time to read the MSDN page for CreateCompatible - it calls out this exact issue.

If you're new to this, think of a bitmap as the actual canvas that you draw on - the DC is just the support structure to do that drawing. As your code stands, you've got the easel and paint brushes set up - but you're not painting on anything.

The usual approach here is:

  • CreateCompatibleDC to create the DC
  • CreateCompatibleBitmap to create the bitmap that you will actually draw to
  • SelectObject your new bitmap into the memory DC
  • Draw to the memory DC - which draws on the bitmap that you've selected into it
  • BitBlt from the memory DC (ie, your bitmap, which is selected into it) to the one from WM_PAINT
  • Clean up: SelectObject the original bitmap back into the memory DC, and delete the bitmap and DC.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文