Discussion:
CreateFontIndirect() problem
(too old to reply)
ByB
2005-05-12 10:32:09 UTC
Permalink
Hello,

I'm trying to change the size of the font used to display data in a
CListCtrl control, and using for it the method found here :

http://www.codeguru.com/Cpp/G-M/gdi/fonthandlinganddetection/article.php/c155/

However, each time I call the CreateFontIndirect() method, everything
compiles well, but I get an error at running.
The error is about a "Debug Assertion failed" provocking a "Breakpoint
exception" (but I have no breakpoints set) .

Apparently, the pb is in the wingdi.c file (from the MFC ?) when
calling an Attach() or Detach() method in an hObject ...

I do not understand well hat is the problem, even when checking the
CreateFontIndirect() page in MSDN ...

Should I set a particular property in the CListCtrl control to allow
changing the font ? Or is the problem completely different ?

Thank you for any hint, as I am something new at fonts managing.
--
Ceci est une signature automatique de MesNews.
Site : http://www.mesnews.net
RainMan
2005-05-12 11:21:03 UTC
Permalink
Without knowing exactly where this assert occurs it is impossible to give you
a valid solution. Also some code snipped illustrating how do you use
CreateFontIndirect.

Most likely you are trying to create font using CFont object that already
has font (GDI font) attached. Another words, object has a valid font handle
and attempt to attach another GDI object will cause ASSERT.

I think a method used in the article you have mentioned is a little too
complicated.
There are many other ways to create font.
Post by ByB
Hello,
I'm trying to change the size of the font used to display data in a
http://www.codeguru.com/Cpp/G-M/gdi/fonthandlinganddetection/article.php/c155/
However, each time I call the CreateFontIndirect() method, everything
compiles well, but I get an error at running.
The error is about a "Debug Assertion failed" provocking a "Breakpoint
exception" (but I have no breakpoints set) .
Apparently, the pb is in the wingdi.c file (from the MFC ?) when
calling an Attach() or Detach() method in an hObject ...
I do not understand well hat is the problem, even when checking the
CreateFontIndirect() page in MSDN ...
Should I set a particular property in the CListCtrl control to allow
changing the font ? Or is the problem completely different ?
Thank you for any hint, as I am something new at fonts managing.
--
Ceci est une signature automatique de MesNews.
Site : http://www.mesnews.net
ByB
2005-05-12 12:52:08 UTC
Permalink
Post by RainMan
Without knowing exactly where this assert occurs it is impossible to give you
a valid solution. Also some code snipped illustrating how do you use
CreateFontIndirect.
Most likely you are trying to create font using CFont object that already
has font (GDI font) attached. Another words, object has a valid font handle
and attempt to attach another GDI object will cause ASSERT.
I think a method used in the article you have mentioned is a little too
complicated.
There are many other ways to create font.
Here is an example of my code :


---------------------------------------------------

METHODS I FOUND AT CODEGURU article

static const TCHAR* pszL = NULL;
CString _S(int i)
{
CString sTemp; AfxExtractSubString(sTemp, pszL, i, TCHAR(','));
return sTemp;
}

#ifdef _UNICODE
#define _L(i) _wtol(_S(i))
#else
#define _L(i) atol(_S(i))
#endif

#define _B(i) (BYTE)_L(i)
#define StartConversion(s) pszL = s
void FillLogFont(LOGFONT& logFont, LPCTSTR lpszFontString )
{
StartConversion(lpszFontString);
logFont.lfHeight = _L(0);
logFont.lfWidth = _L(1);
logFont.lfEscapement = _L(2);
logFont.lfOrientation = _L(3);
logFont.lfWeight = _L(4);
logFont.lfItalic = _B(5);
logFont.lfUnderline = _B(6);
logFont.lfStrikeOut = _B(7);
logFont.lfCharSet = _B(8);
logFont.lfOutPrecision = _B(9);
logFont.lfClipPrecision = _B(10);
logFont.lfQuality = _B(11);
logFont.lfPitchAndFamily = _B(12);
memcpy(logFont.lfFaceName, _S(13), LF_FACESIZE);
}

-----------------------------------------------------------------------

How I use them :

statusFont is a CFont variable, declared in the CDialog class.


LOGFONT logFont;
FillLogFont(logFont, "-12,0,0,0,400,0,0,0,0,3,2,1,34,Arial" );
statusFont.CreateFontIndirect( &logFont ) ;

The problem happens when calling CreateFontIndirect(), and has
apparently something to do with the GDI (as the exception occurs in
wingdi.c )
--
Ceci est une signature automatique de MesNews.
Site : http://www.mesnews.net
ByB
2005-05-12 13:07:29 UTC
Permalink
Post by ByB
Post by RainMan
Without knowing exactly where this assert occurs it is impossible to give
you a valid solution. Also some code snipped illustrating how do you use
CreateFontIndirect.
Most likely you are trying to create font using CFont object that already
has font (GDI font) attached. Another words, object has a valid font handle
and attempt to attach another GDI object will cause ASSERT.
I think a method used in the article you have mentioned is a little too
complicated.
There are many other ways to create font.
---------------------------------------------------
METHODS I FOUND AT CODEGURU article
static const TCHAR* pszL = NULL;
CString _S(int i)
{
CString sTemp; AfxExtractSubString(sTemp, pszL, i, TCHAR(','));
return sTemp;
}
#ifdef _UNICODE
#define _L(i) _wtol(_S(i))
#else
#define _L(i) atol(_S(i))
#endif
#define _B(i) (BYTE)_L(i)
#define StartConversion(s) pszL = s
void FillLogFont(LOGFONT& logFont, LPCTSTR lpszFontString )
{
StartConversion(lpszFontString);
logFont.lfHeight = _L(0);
logFont.lfWidth = _L(1);
logFont.lfEscapement = _L(2);
logFont.lfOrientation = _L(3);
logFont.lfWeight = _L(4);
logFont.lfItalic = _B(5);
logFont.lfUnderline = _B(6);
logFont.lfStrikeOut = _B(7);
logFont.lfCharSet = _B(8);
logFont.lfOutPrecision = _B(9);
logFont.lfClipPrecision = _B(10);
logFont.lfQuality = _B(11);
logFont.lfPitchAndFamily = _B(12);
memcpy(logFont.lfFaceName, _S(13), LF_FACESIZE);
}
-----------------------------------------------------------------------
statusFont is a CFont variable, declared in the CDialog class.
LOGFONT logFont;
FillLogFont(logFont, "-12,0,0,0,400,0,0,0,0,3,2,1,34,Arial" );
statusFont.CreateFontIndirect( &logFont ) ;
The problem happens when calling CreateFontIndirect(), and has apparently
something to do with the GDI (as the exception occurs in wingdi.c )
The problem happens in the following method of wingdi.cpp :

BOOL CGdiObject::Attach(HGDIOBJ hObject)
{
ASSERT(m_hObject == NULL); // only attach once, detach on destroy
if (hObject == NULL)
return FALSE;
CHandleMap* pMap = afxMapHGDIOBJ(TRUE); // create map if not exist
ASSERT(pMap != NULL);
pMap->SetPermanent(m_hObject = hObject, this);
return TRUE;
}

(the ASSERT in the first line is the one)
--
Ceci est une signature automatique de MesNews.
Site : http://www.mesnews.net
ByB
2005-05-12 13:14:49 UTC
Permalink
Post by ByB
Post by ByB
Post by RainMan
Without knowing exactly where this assert occurs it is impossible to give
you a valid solution. Also some code snipped illustrating how do you use
CreateFontIndirect.
Most likely you are trying to create font using CFont object that already
has font (GDI font) attached. Another words, object has a valid font
handle and attempt to attach another GDI object will cause ASSERT.
I think a method used in the article you have mentioned is a little too
complicated.
There are many other ways to create font.
---------------------------------------------------
METHODS I FOUND AT CODEGURU article
static const TCHAR* pszL = NULL;
CString _S(int i)
{
CString sTemp; AfxExtractSubString(sTemp, pszL, i, TCHAR(','));
return sTemp;
}
#ifdef _UNICODE
#define _L(i) _wtol(_S(i))
#else
#define _L(i) atol(_S(i))
#endif
#define _B(i) (BYTE)_L(i)
#define StartConversion(s) pszL = s
void FillLogFont(LOGFONT& logFont, LPCTSTR lpszFontString )
{
StartConversion(lpszFontString);
logFont.lfHeight = _L(0);
logFont.lfWidth = _L(1);
logFont.lfEscapement = _L(2);
logFont.lfOrientation = _L(3);
logFont.lfWeight = _L(4);
logFont.lfItalic = _B(5);
logFont.lfUnderline = _B(6);
logFont.lfStrikeOut = _B(7);
logFont.lfCharSet = _B(8);
logFont.lfOutPrecision = _B(9);
logFont.lfClipPrecision = _B(10);
logFont.lfQuality = _B(11);
logFont.lfPitchAndFamily = _B(12);
memcpy(logFont.lfFaceName, _S(13), LF_FACESIZE);
}
-----------------------------------------------------------------------
statusFont is a CFont variable, declared in the CDialog class.
LOGFONT logFont;
FillLogFont(logFont, "-12,0,0,0,400,0,0,0,0,3,2,1,34,Arial" );
statusFont.CreateFontIndirect( &logFont ) ;
The problem happens when calling CreateFontIndirect(), and has apparently
something to do with the GDI (as the exception occurs in wingdi.c )
BOOL CGdiObject::Attach(HGDIOBJ hObject)
{
ASSERT(m_hObject == NULL); // only attach once, detach on destroy
if (hObject == NULL)
return FALSE;
CHandleMap* pMap = afxMapHGDIOBJ(TRUE); // create map if not exist
ASSERT(pMap != NULL);
pMap->SetPermanent(m_hObject = hObject, this);
return TRUE;
}
(the ASSERT in the first line is the one)
OK, it seems that you were right, and that I need to delete the GDI
font attached to my CFont object before trying to attach another one.
If I use :

statusFont.DeleteObject()

before I create and attach the new font, everything runs correctly.

Thank you !
--
Ceci est une signature automatique de MesNews.
Site : http://www.mesnews.net
Naren
2005-05-12 14:09:01 UTC
Permalink
hi,
u need not to delete the object before selecting another object. make sure
that the CreateFontIndirect() doesn't execute for multiple times(becasue it
is already in device context, from the second execution it ll start to give
assertions).

naren
Post by ByB
Post by ByB
Post by ByB
Post by RainMan
Without knowing exactly where this assert occurs it is impossible to give
you a valid solution. Also some code snipped illustrating how do you use
CreateFontIndirect.
Most likely you are trying to create font using CFont object that already
has font (GDI font) attached. Another words, object has a valid font
handle and attempt to attach another GDI object will cause ASSERT.
I think a method used in the article you have mentioned is a little too
complicated.
There are many other ways to create font.
---------------------------------------------------
METHODS I FOUND AT CODEGURU article
static const TCHAR* pszL = NULL;
CString _S(int i)
{
CString sTemp; AfxExtractSubString(sTemp, pszL, i, TCHAR(','));
return sTemp;
}
#ifdef _UNICODE
#define _L(i) _wtol(_S(i))
#else
#define _L(i) atol(_S(i))
#endif
#define _B(i) (BYTE)_L(i)
#define StartConversion(s) pszL = s
void FillLogFont(LOGFONT& logFont, LPCTSTR lpszFontString )
{
StartConversion(lpszFontString);
logFont.lfHeight = _L(0);
logFont.lfWidth = _L(1);
logFont.lfEscapement = _L(2);
logFont.lfOrientation = _L(3);
logFont.lfWeight = _L(4);
logFont.lfItalic = _B(5);
logFont.lfUnderline = _B(6);
logFont.lfStrikeOut = _B(7);
logFont.lfCharSet = _B(8);
logFont.lfOutPrecision = _B(9);
logFont.lfClipPrecision = _B(10);
logFont.lfQuality = _B(11);
logFont.lfPitchAndFamily = _B(12);
memcpy(logFont.lfFaceName, _S(13), LF_FACESIZE);
}
-----------------------------------------------------------------------
statusFont is a CFont variable, declared in the CDialog class.
LOGFONT logFont;
FillLogFont(logFont, "-12,0,0,0,400,0,0,0,0,3,2,1,34,Arial" );
statusFont.CreateFontIndirect( &logFont ) ;
The problem happens when calling CreateFontIndirect(), and has apparently
something to do with the GDI (as the exception occurs in wingdi.c )
BOOL CGdiObject::Attach(HGDIOBJ hObject)
{
ASSERT(m_hObject == NULL); // only attach once, detach on destroy
if (hObject == NULL)
return FALSE;
CHandleMap* pMap = afxMapHGDIOBJ(TRUE); // create map if not exist
ASSERT(pMap != NULL);
pMap->SetPermanent(m_hObject = hObject, this);
return TRUE;
}
(the ASSERT in the first line is the one)
OK, it seems that you were right, and that I need to delete the GDI
font attached to my CFont object before trying to attach another one.
statusFont.DeleteObject()
before I create and attach the new font, everything runs correctly.
Thank you !
--
Ceci est une signature automatique de MesNews.
Site : http://www.mesnews.net
Loading...