Hi, I would like to have the DrawText function draw the text using the same default font, but I want a larger version of the font to be used, anyone know how to do that easily?
// Always select the old font back into the DC
SelectObject(hdc, hOldFont);
DeleteObject(hNewFont);
The above code does not do any error-checking.
jstewart
November 14th, 2003, 08:27 AM
you need to get the font for m_hwnd, and put it into a LOGFONT structure. In MFC this is easy, in Win32 its more complicated...
Win32 example:
There a two ways to do this - get the name of the font in the DC and fill the font structure yourself, or get the name of the font and have windows fill the structure for you.
To do it yourself:
// get the font in the DC
char faceName[100];
GetTextFace(hDC,faceName,100);
// create and clear a font structure
LOGFONT lf;
memset(&lf,0,sizeof(LOGFONT));
// set the font name in the font structure
lstrcpy( (LPSTR)&lf.lfFaceName,faceName );
// now fill the rest of the LOGFONT structure yourself
// for example, to set the font height to 15 point, use this:
lfFontData.lfHeight = -MulDiv(15,GetDeviceCaps(hDC,LOGPIXELSY, 72);
// once the structure is ready, use CreateFontIndirect() to get a font handle:
HFONT myFont = CreateFontIndirect(&lf);
Once you have an HFONT, use SelectObject to load it into the DC for drawing text:
HFONT oldFont = (HFONT)SelectObject(hDC,myFont);
// do your drawing...
// put the original font back
SelectObject(hDC,oldFont);
Finally, when you close your program remember to destroy the font:
DeleteObject(myFont);
Second option, have windows fill the data for you. This is more complex than the first, but means you don't have to worry about filling the LOGFONT structure...
declare a HFONT somewhere else, this needs to live beyond the scope of the drawing function
put this code somewhere, you should only need to call it once before you draw - please note this is put together from information in the MSDN library, it may need debugging
void GetDCFont(HDC hDC, HFONT* hMyFont)
{
// identifies the font in the dc and puts it in hMyFont
// first you need the name of the current font in the DC
char faceName[256];
GetTextFace(hDC, faceName, 256);
// now you need to enumerate the fonts on your machine
// to get the font info for the font in the DC into a LOGFONT
// this just enumerates the ANSI character set of the font in
// the DC
LOGFONT lf;
lf.lfCharset = ANSI_CHARSET;
lstrcpy( (LPSTR)&lf.lfFaceName, faceName);
lf.lfPitchAndFamily = 0;
// the font data you want can go into here:
LOGFONT lfFontData;
memset(&lfFontData,0,sizeof(LOGFONT));
// copy the face name to it so we can match
lstrcpy( (LPSTR)&lfFontData.lfFaceName, faceName);
// you need a callback function, we'll call it myEnumFontFamExCallbackFunc
if( EnumFontFamiliesEx(hDC, &lf, myEnumFontFamExCallbackFunc, (LPARAM)&lfFontData,0) == 0)
{
// enumeration stopped, check the font parameters
// the height will do
if(lfFontData.lfHeight != 0)
{
// make a font from the LOGFONT
// choose a point size, I'll use 15 point
lfFontData.lfHeight = -MulDiv(15,GetDeviceCaps(hDC,LOGPIXELSY, 72);
// and create a font
*hMyFont = CreateFontIndirect(&lfFontData);
// and finish
return;
}
}
// could not determine the current font
*hMyFont = NULL;
return;
}
heres the call back function:
int CALLBACK EnumFontFamExProc(
ENUMLOGFONTEX *lpelfe, // logical-font data
NEWTEXTMETRICEX *lpntme, // physical-font data
DWORD FontType, // type of font
LPARAM lParam // application-defined data
)
{
// lpelfe has a LOGFONT member, so we can use this to compare
// but what do we compare it with?
LOGFONT* pFont = (LOGFONT*)lParam;
// compare
if(strcmp(pFont->lfFaceName, lpelfe->elfLogFont.lfFaceName) == 0)
{
// fonts match, copy the data
memcpy(pFont,&lpelfe->elfLogFont,sizeof(LOGFONT));
Cool! Thanks for those responses. I am going to try your solutions tonight and will post my results tomorrow. Thanks!
Hobbieman
Hobbieman
November 14th, 2003, 06:02 PM
Alright I lied... I could not wait to test it! :-) So, I tested the solutions and they work. I am going to use the first one since it seems less complex, and perfectly suiting for a greenhorn like me.
Oh yeah, the only thing I had to add to get the first response to work was to cast the return value of GetStockObject() to HFONT.
Thanks!
Hobbieman
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.