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

问题描述

我看到的每个示例代码都是一次又一次的GetDC和releaseDC. (或BeginPaint/EndPaint)但是我认为绘图屏幕经常发生(尤其是在游戏中),将它们存储在内存中要比一直获取并释放更好.

Every example codes I saw are GetDC and releaseDC again and again. (or BeginPaint/EndPaint)But I think drawing screen happens very frequently(especially in game), store them in memory is better then get and release all time.

因此,我确实希望将mainDC保持为全局,并只使用它,仅在程序结束时才将其释放.但是为什么人们不这样做呢? (也许获取/发布DC的成本很少?)

So I did like keep mainDC as global and just use it, only release it when program ends. But why people doesn't do like this? (maybe Get/Release DC costs 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消息.使用BeginPaint()/EndPaint()函数正确绘制客户区.当部分客户区无效"时,系统会发送此消息(由于调整大小,从最小化状态还原,移动先前遮盖它的窗口或以编程方式使其无效),WM_PAINT是低优先级消息,在消息队列变空之前收到.
  • 专门绘制部分或整个客户区域,而不在其上放置无效区域.为此使用GetDC()/ReleaseDC().如果要在应用程序(CPU)忙时使更改立即可见,则很有用.

编写一些代码来处理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 whole client area - the application will receive a WM_PAINT message before getting idle. If you want the painting to occur immediately call UpdateWindow() - this bypasses the message-queue.

这篇关于Win32API)为什么我应该一次又一次GetDC和ReleaseDC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 06:10