GDI Font problem

Started by
1 comment, last by WhiteRaptor 22 years, 4 months ago
I''m having a problem with changing Fonts with a simple GDI TextOut method. The font will display in the intended size/style that I called for with the CreateFont for a brief period. Then, after maybe a minute or so, it reverts to a different (default?) font/size. I''ve tried creating the Font inside and out of the function (code is below), but nothing''s changed. Has anyone else encountered this? ////////////////////////////////////////////////////////////// int Draw_FText_GDI(char * text, int x,int y, COLORREF color, LPDIRECTDRAWSURFACE4 lpdds, HFONT fnt) { HDC xdc; // the working dc if (FAILED(lpdds->GetDC(&xdc))) return(0); SetTextColor(xdc,color); SetBkMode(xdc, TRANSPARENT); SelectObject (xdc, fnt); TextOut(xdc,x,y,text,strlen(text)); lpdds->ReleaseDC(xdc); return(1); }
Advertisement
I''m pretty sure what''s happening is that it''s leaking resources until it can no longer select the font into the DC. Try this:

//////////////////////////////////////////////////////////////
int Draw_FText_GDI(char * text, int x,int y,
COLORREF color, LPDIRECTDRAWSURFACE4 lpdds, HFONT fnt)
{
HDC xdc; // the working dc

if (FAILED(lpdds->GetDC(&xdc)))
return(0);

SetTextColor(xdc,color);

SetBkMode(xdc, TRANSPARENT);

// Record the old font.
HFONT oldfnt = SelectObject (xdc, fnt);

TextOut(xdc,x,y,text,strlen(text));

// Restore the old font.
SelectObject(xdc, oldfnt);

lpdds->ReleaseDC(xdc);

return(1);
}
Thanks for pointing me in the right direction. The code below is what I got to work for me. My problem of the font changing on it''s own has vanished. Thanks again.

//////////////////////////////////////////////////////////
int Draw_FontText_GDI(char * text, int x,int y,
COLORREF color, LPDIRECTDRAWSURFACE4 lpdds, HFONT fnt)
{
HDC xdc; // the working dc

if (FAILED(lpdds->GetDC(&xdc)))
return(0);

SetTextColor(xdc,color);

SetBkMode(xdc, TRANSPARENT);

HFONT oldfnt = (HFONT)SelectObject(xdc, fnt);

SelectObject(xdc,fnt);

TextOut(xdc,x,y,text,strlen(text));

SelectObject(xdc, oldfnt);

DeleteObject(fnt);
DeleteObject(oldfnt);

lpdds->ReleaseDC(xdc);

return(1);
}

This topic is closed to new replies.

Advertisement