Question about deleteobject.
I have the following code for changing font to bold in a control.
If I dont delete the object I get memory leaks.Code:void CtrlWindow::FontBold(bool showBold) { LOGFONT newLFont; LOGFONT oldLFont; HFONT newFont; HFONT oldFont; oldFont=(HFONT)SendMessage(m_hwnd,WM_GETFONT,0,0); //GetTextMetrics(GetDC(m_hwnd),&textM); GetObject(oldFont,sizeof(LOGFONT),&oldLFont); newLFont.lfCharSet=oldLFont.lfCharSet; newLFont.lfClipPrecision=oldLFont.lfClipPrecision; newLFont.lfEscapement=oldLFont.lfEscapement; ZeroMemory((void*)newLFont.lfFaceName,32); CopyMemory((void*)newLFont.lfFaceName,oldLFont.lfFaceName,32); newLFont.lfHeight=oldLFont.lfHeight; newLFont.lfItalic=oldLFont.lfItalic; newLFont.lfOrientation=oldLFont.lfOrientation; newLFont.lfOutPrecision=oldLFont.lfOutPrecision; newLFont.lfPitchAndFamily=oldLFont.lfPitchAndFamily; newLFont.lfQuality=oldLFont.lfQuality; newLFont.lfStrikeOut=oldLFont.lfStrikeOut; newLFont.lfUnderline=oldLFont.lfUnderline; if (showBold==true) { newLFont.lfWeight=700; } else { newLFont.lfWeight=400; } newLFont.lfWidth=oldLFont.lfWidth; newFont=CreateFontIndirect(&newLFont); SendMessage(m_hwnd,WM_SETFONT, (WPARAM)newFont, MAKELPARAM(TRUE,0)) ; //DeleteObject(newFont); }
If I do delete the object the bold text gets garbled. on second call the text is completly removed.
Am I going about this the wrong way?
Is it because the sendmessage doesnt have the time to set the font before it is deleted?
Thanks
Packetvb




Reply With Quote