我是否需要对从 SystemParametersInfo() 检索到的字体调用 DeleteObject()?

Do I need to call DeleteObject() on font retrieved from SystemParametersInfo()?

我得到的系统默认字体是这样的:

NONCLIENTMETRICS ncm;
ncm.cbSize = sizeof(NONCLIENTMETRICS);
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0);
HFONT hFont = CreateFontIndirect(&ncm.lfMessageFont);

然后

改变控件的字体是这样的:

SendMessage(hwnd, WM_SETFONT, (WPARAM) hFont, TRUE);

我的问题是,既然这个字体是由 SystemParametersInfo(), do I need to delete it with DeleteObject() 检索到的?我不确定 OS 拥有那段内存,所以我不需要释放它。

SystemParametersInfo returns LOGFONT 结构。这些结构不需要释放。

创建字体(来自 LOGFONT 或其他)让您负责资源清理。这里的字体对象不是由 SystemParametersInfo 创建的,而是通过调用 CreateFontIndirect.

创建的

正如其他人所说,您不需要在 LOGFONT 结构上调用 DeleteObject()

但是,您需要在从 CreateFontIndirect() 返回的 HFONT 上调用 DeleteObject()

但是更大,......你需要等待在 HFONT 上调用 DeleteObject(),直到你摧毁 window(以及任何其他 window ) 您将字体设置为使用 WM_SETFONT.

伪代码:

  1. 得到你的NONCLIENTMETRICS...
  2. 致电hFont = CreateFontIndirect()
  3. 致电SendMessage(WM_SETFONT)
  4. 等待window被销毁(或应用程序清理)
  5. hFont
  6. 上致电 DeleteObject()

如果您打算将 HFONT 用于多个 windows,请创建它一次,然后等到应用程序关闭以清理它(或者,等到您知道所有windows你用过的已经被毁了)。