2011-07-26 61 views
0

我有GDI泄漏的小事,我想知道别人的关于如何解决this.Say我有enfolds具体的数据来创建和处理窗口前的一类观点:的Win32字体资源泄漏

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); 
    } 
}; 

所以主要的问题是,在销毁时,我如何释放分配给h0Font参数的内存?我应该获得窗口的设备上下文并使用SelectObject()函数,以便在此之后我可以释放它调用函数为旧的字体和使用DeleteObject()释放内存?非常感谢。

回答

2

所以主要的问题是,在销毁时我该如何释放分配给hFont参数的 内存?

您使用DeleteObject()每个文档CreateFontIndirect()WM_SETFONT message

,我应该得到的窗口的设备上下文,并使用选择对象 ()函数,以便之后,我可以释放它调用函数 的老字号,并使用DeleteObject的()来释放内存?

这不应该是必要的,只要你的绘画程序正确地恢复旧字体后,例程完成字体。

+0

非常感谢您的回答。 –