为什么我要一次又一次地调用`GetDC`和`ReleaseDC`? [英] Why should I call `GetDC` and `ReleaseDC` again and again?

查看:46
本文介绍了为什么我要一次又一次地调用`GetDC`和`ReleaseDC`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到的每个示例代码都是一次又一次地调用 GetDCReleaseDC.或者 BeginPaintEndPaint.

Every example code I have seen are GetDC and ReleaseDC being called again and again. Or BeginPaint and EndPaint.

但我认为在屏幕上绘图非常频繁(尤其是在游戏中),因此将绘图存储在内存中比一直获取和释放设备上下文要好.

But I think drawing on the screen happens very frequently (especially in a game), so storing the drawing in memory is better than getting and releasing device contexts all time.

所以我走的是获取 DC 并保留它"的路线,只有在程序结束时才释放它.但为什么人们不这样做呢?是不是因为GetDCReleaseDC成本很低?

So I went the route of getting a DC and "keeping it", only releasing it when the program ends. But why don't people do it like this? Is it because GetDC and ReleaseDC cost very little?

case WM_CREATE:
    hdc = GetDC(hWnd); //hdc is declared as global HDC
    MyBitmap = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BITMAP1));
    return 0;
case WM_PAINT:
    MemDC = CreateCompatibleDC(hdc);
    OldBitmap = (HBITMAP)SelectObject(MemDC, MyBitmap);
    BitBlt(hdc, 0, 0, 300, 300, MemDC, 0, 0, SRCCOPY);
    SelectObject(MemDC, OldBitmap);
    DeleteDC(MemDC);
    return 0;

推荐答案

您在此处提供的代码是错误的.首先,您需要阅读更多的文档.这是一个有用的链接:绘画和绘图.基本上有两种方法可以更新窗口:

The code you presented here is wrong. First off, you need to read a little more of the documentation. Here is a useful link: Painting and Drawing. Basically there are two ways to update a window:

  • 响应 WM_PAINT 消息.使用BeginPaintEndPaint 函数正确绘制客户区.当客户区的一部分无效"时,该消息由系统发送.(作为调整大小、从最小化状态恢复、移动先前被遮挡的窗口或以编程方式使其无效的结果.)WM_PAINT 是低优先级消息,在消息队列变空之前接收.
  • 专门绘制一部分或整个客户区,而其上没有无效区域.为此,请使用 GetDCReleaseDC.如果您想在应用程序 (CPU) 繁忙时立即使更改可见,则非常有用.
  • In response to the WM_PAINT message. Use the BeginPaint and EndPaint functions to paint the client area properly. This message is sent by the system when a part of the client area is "invalidated" (as a result of resizing, restoring from minimized state, moving a window previously obscured, or programmatically invalidating it.) WM_PAINT is a low-priority message, received just before the message-queue gets empty.
  • Specifically drawing a part or the whole client area without having an invalidated region on it. Use GetDC and ReleaseDC for this. Useful if you want to make changes immediately visible, when the application (CPU) is busy.

编写一些代码来处理 WM_PAINT 消息通常几乎是强制性的,而具体绘制也是可选的,具体取决于您的应用程序的要求.

Writing some code to process the WM_PAINT message is normally almost mandatory, while specifically drawing as well is optional, depending on the requirements of your application.

永远不要自己发送或发布 WM_PAINT 消息,而是使部分或客户区无效 - 应用程序将在空闲之前收到 WM_PAINT 消息.如果您希望立即进行绘制,请调用 UpdateWindow - 这会绕过消息队列.

Never send or post a WM_PAINT message yourself, instead invalidate a part or the client area - the application will receive a WM_PAINT message before becoming idle. If you want the painting to occur immediately call UpdateWindow - this bypasses the message queue.

这篇关于为什么我要一次又一次地调用`GetDC`和`ReleaseDC`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆