gpt4 book ai didi

c++ - GDI::DeleteObject 是如何工作的

转载 作者:行者123 更新时间:2023-11-28 02:22:35 32 4
gpt4 key购买 nike

根据 MSDN

The DeleteObject function deletes a logical pen, brush, font, bitmap, region, or palette, freeing all system resources associated with the object. After the object is deleted, the specified handle is no longer valid.

所以一个人(我自己)会认为一旦执行了 DeleteObject,HANDLE 就不再有效了。但是,一旦我在使用其他 WinAPI 调用保存对象之前删除对象,由::GetObject() 检索到的对象会发生什么情况?

        HFONT hFont = reinterpret_cast<HFONT>(::SendMessage(hwndCtrl, WM_GETFONT, 0, 0));
if (nullptr == hFont)
{
LOG_ERROR(L"Invalid font specified");
return false;
}

LOGFONT font = { 0 };
if (0 == ::GetObject(hFont, sizeof(font), &font))
{
LOG_ERROR(L"Failed getting font");
return false;
}

font.lfHeight = nSize;



::DeleteObject(hFont);
HFONT hFontEx = ::CreateFontIndirect(&font);
LPARAM lparam = MAKELPARAM(TRUE, 0);
WPARAM wparam = (WPARAM)(hFontEx);
SendMessage(hwndCtrl, WM_SETFONT, wparam, lparam);

如以下示例所示,如果我决定删除我的 HFONT,在通过 SendMessage 发送新消息之前,我会检索到一些意想不到的结果,其他控件的字体也会发生变化,就好像我生成了某种句柄泄漏。

最佳答案

So one(myself) would think that once DeleteObject is executed, the HANDLE is no longer valid. but what happens to the objects retrieved by ::GetObject() once i delete the object before saving them with other WinAPI calls?

使用 GetObject 可以获得对象的描述,而不是新对象。句柄删除后保持不变。

As in the following example, if i decide to delete my HFONT, before sending the new message via SendMessage, i'd retrieve some unexpected results, where other controls gets their font changed, as if i'd generated some kind of a handle leak.

如果你想发送一条删除了 HFONT 的消息,我想如果你用任何其他垃圾发送它,结果将是相同的。

关于c++ - GDI::DeleteObject 是如何工作的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31850200/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com