【发布时间】:2011-07-26 18:30:33
【问题描述】:
我有一个 GDI 泄漏的小问题,我想知道有人对如何解决这个问题的看法。假设我有一个包含特定于创建和处理窗口 ex 的数据的类:
class Wnd {
HWND hWnd;
HFONT hFont;
LOGFONT LogFont;
//etc
public:
//constructors and member functions
//The following function atempts to change the font of the window
//pointed to by the hWnd parameter
void ChangeFont (const LOGFONT& lf) {
std::memcpy (&LogFont,&lf,sizeof(LOGFONT));
hFont=CreateFontIndirect (&LogFont);
SendMessage (hWnd,WM_SETFONT,(WPARAM) hFont,(LPARAM) 1);
}
~Wnd () {
//i don't think this would work since i haven't used the SelectObject function
DeleteObject ((HGDIOBJ) hFont);
}
};
所以主要问题是,在销毁时我如何释放分配给 hFont 参数?我是否应该获取窗口的设备上下文并使用 SelectObject () 函数,以便之后我可以释放它调用旧字体的函数并使用 DeleteObject () 来释放内存?非常感谢。
【问题讨论】:
标签: winapi