c - 如何正确释放GDI资源?

标签 c winapi gdi

我试图了解如何正确释放 GDI 资源,在阅读了许多 GDI 教程后,我认为这是如何完成的:

假设我已经创建了一个内存 DC,并创建并选择了许多 GDI 对象(HPENHBRUSHHBITMAP 等)进去。现在要释放 GDI 资源,我将执行以下操作:

  1. 将所有旧的 GDI 对象选择到内存 DC 中(因此 选择所有新创建的 GDI 对象)。
  2. 删除新创建的 GDI 对象。
  3. 删除内存 DC。

请注意,第二步和第三步可以按相反的顺序执行。即内存DC被删除为GDI对象。

如果我使用的是普通 DC(我使用 GetDC() 或 BeginPaint() 获得的),我会用释放 DC 来代替删除 DC .

我做对了吗,还是我遗漏了什么?

最佳答案

  1. Select all of the old GDI object into the memory DC (and hence selecting out all of the newly created GDI objects).
  2. Delete the newly created GDI objects.
  3. Delete the memory DC.

Note that the second and third steps can be performed in reverse order. That is the memory DC is deleted for the GDI objects are.

Did I get this right?

是的,你做到了。

And if I am working with a normal DC (that I got using GetDC() or BeginPaint()), I would replace deleting the DC with just releasing it.

嗯,GetDC 将与 ReleaseDC 匹配,BeginPaint 将与 EndPaint 匹配。至于 GetDC,请注意 documentation 中的以下内容:

After painting with a common DC, the ReleaseDC function must be called to release the DC. Class and private DCs do not have to be released.

关于c - 如何正确释放GDI资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33497466/

相关文章:

c - Pascal 到 C 的翻译

c++ - 使用 C API 覆盖 Lua 中的赋值运算符

c - 计算给定范围内偶数数量的最简单方法

c# - 如何检测父进程的关闭?

python - 用 Winapi 替换 WMI 调用

c++ - 以逻辑单位表示的 CDC 区域大小?

c - 为什么内存损坏会存在……只有当我们不打印被损坏的变量时?

c - Linux;如果您不是 root,请将 UID 更改为 root

python - 手动打开 excel 文件允许公式运行,使用 VBScript 或 PowerShell 或 Python 的 win32com 打开 excel 文件则不允许

c++ GDI位图不想加载