dcsimg

Easy Way to Set the LOGFONT Structure

WEBINAR:
On-Demand

Application Security Testing: An Integral Part of DevOps


Environment: Windows 95/98/NT, VC4.2+

To set the new font for a window or control a CFont object is often used. In order to use this object a LOGFONT structure must be passed to it to create the font object in memory. Filling the members of the LOGFONT structure can be time consuming and prone to errors so I created a short project which allows a programmer to select a desired font, which creates a string containing values for all the members of the LOGFONT structure. Next, the programmer uses the following functions to create a new CFont object.


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 to generate the font string:

Run the GetF executable, select a font, and then copy the string created at the bottom of the dialog into the clipboard. Paste this string into your code as the second parameter of the FillLogFont(...) function.

Download source - 16KB



Comments

  • Alternative conversion

    Posted by Legacy on 01/21/2003 12:00am

    Originally posted by: E. Kanjanski

    Nice tool.
    
    

    I have an app that allows the user to customise the font,
    and then stores the settings as text. Restoring the
    LOGFONT the next time it is loaded.

    Here's the alternative conversion, making use of sscanf and
    sprintf:

    void LogFont2String(LOGFONT& lf, CString& s, BOOL bToStr)
    {
    #define _STR2(x) #x
    #define _STR(x) _STR2(x)
    static const TCHAR szLogFontMatch[] = _T("%d,%d,%d,%d,%d,%u,%u,%u,%u,%u,%u,%u,%u,%s");
    static const TCHAR szLogFontMatch2[] = _T("%d,%d,%d,%d,%d,%u,%u,%u,%u,%u,%u,%u,%u,%" _STR(LF_FACESIZE) "[^\0]s");
    #undef _STR2
    #undef _STR

    if (bToStr) {
    _stprintf(s.GetBuffer(10*13 + LF_FACESIZE), szLogFontMatch,
    lf.lfHeight,
    lf.lfWidth,
    lf.lfEscapement,
    lf.lfOrientation,
    lf.lfWeight,
    lf.lfItalic,
    lf.lfUnderline,
    lf.lfStrikeOut,
    lf.lfCharSet,
    lf.lfOutPrecision,
    lf.lfClipPrecision,
    lf.lfQuality,
    lf.lfPitchAndFamily,
    lf.lfFaceName);
    s.ReleaseBuffer();
    }
    else {
    #ifdef _DEBUG
    int nMatch =
    #endif
    _stscanf(s, szLogFontMatch2,
    &lf.lfHeight,
    &lf.lfWidth,
    &lf.lfEscapement,
    &lf.lfOrientation,
    &lf.lfWeight,
    &lf.lfItalic,
    &lf.lfUnderline,
    &lf.lfStrikeOut,
    &lf.lfCharSet,
    &lf.lfOutPrecision,
    &lf.lfClipPrecision,
    &lf.lfQuality,
    &lf.lfPitchAndFamily,
    lf.lfFaceName);
    ASSERT(nMatch == 14);
    }
    }

    Reply
  • Very Good tool

    Posted by Legacy on 11/30/2001 12:00am

    Originally posted by: Aykut KILI�

    Thanks for saving us entering 14 parameters each time!!

    Reply
  • Outstanding!

    Posted by Legacy on 10/24/2001 12:00am

    Originally posted by: Bill

    Thanks a lot for this very useful utility. It has saved me a ton of time.

    Reply
  • Problems with the Terminal Font

    Posted by Legacy on 10/03/2000 12:00am

    Originally posted by: Michael Scott

    I have a problem with the CFont class. I can't set the Terminal font for a Editbox. I need this font because its the only one with oem/dos character set. I hope you can help me

    Reply
  • How use CDC::GetGlyphOutline API

    Posted by Legacy on 09/30/2000 12:00am

    Originally posted by: cxy

    How are you used this API.
    Please give me a E-mail.

    Reply
  • Good tool~

    Posted by Legacy on 09/26/2000 12:00am

    Originally posted by: fluorite

    Thanks Mihai Filimon.
    This simple, and funny :) tool makes programmer save time~~

    P.S.
    I laughed when I saw this article at first
    cuz this article was so clever thing. :)

    Reply
  • how use CDC::GetGlyphOutline

    Posted by Legacy on 03/27/2000 12:00am

    Originally posted by: plsoft

    How are you used this API. 
    
    Please give me a E-mail.

    what is error????? Please reply,Thank you.

    void CWordWriterView::OnTest()
    {
    // TODO: Add your command handler code here
    long yyy=50,hhh=0;
    for(WORD ty='A';ty<='Z';ty++){
    CClientDC clientdc(this);

    CFont fnt,*pfnt;
    fnt.CreatePointFont(720,"Arial");
    clientdc.TextOut(0,0,_T("Arial"));
    pfnt=clientdc.SelectObject(&fnt);
    CDC dc;
    dc.CreateCompatibleDC(&clientdc);

    GLYPHMETRICS gm;
    DWORD cbBuffer=NULL;
    LPVOID lpBuffer=NULL;
    MAT2 mat2;
    IdentityMat(&mat2);
    cbBuffer=::GetGlyphOutline(clientdc.m_hDC,ty, GGO_BITMAP , &gm, cbBuffer, lpBuffer, &mat2 );
    lpBuffer=malloc(cbBuffer);
    if(::GetGlyphOutline(clientdc.m_hDC,ty, GGO_BITMAP , &gm, cbBuffer, lpBuffer, &mat2 )!=cbBuffer)
    MessageBox("ERROR!");
    CBitmap bmp;
    bmp.CreateBitmap(gm.gmBlackBoxX,gm.gmBlackBoxY ,1,1,lpBuffer);
    dc.SelectObject(bmp);
    clientdc.BitBlt( yyy , hhh , gm.gmBlackBoxX, gm.gmBlackBoxY, &dc, 0, 0, SRCCOPY);

    clientdc.SelectObject(pfnt);
    free(lpBuffer);
    if(yyy>600){hhh+=25;yyy=0;}
    yyy+=gm.gmBlackBoxX;
    if(hhh>480){
    Invalidate( TRUE );
    yyy=50;hhh=0;
    }
    }
    }

    Reply
  • what if your HDC is to a printer?

    Posted by Legacy on 12/09/1999 12:00am

    Originally posted by: Steve Hazel

    So this stuff assumes an HDC that's going to the screen,
    
    right?
    It won't work for printing a certain font's
    typeface/pointsize to a printer, right?

    Reply
  • Problems with WIN95

    Posted by Legacy on 07/02/1999 12:00am

    Originally posted by: KAA

    I'm trying to use a 'Courier' font with a PrintDC. I'm developing in NT, and it works fine on NT, but when I run the same program on 95 It doesn't use the correct font. I've tried this on numerous 95 machines and they all have 'Courier' loaded. Any ideas?

    Reply
  • SUPERB!

    Posted by Legacy on 03/24/1999 12:00am

    Originally posted by: Larry Yates

    What we need are MORE tools like this!
    
    This is GREAT STUFF!!!

    Reply
  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Discover the best practices from HPE’s IT Advisory Consulting Services for migrating and transforming applications in Hybrid IT by capitalizing on innovative platforms, modern application architectures, agile development tools and proven methodologies. There are a number of challenges our customers face when migrating and transforming applications for a Hybrid IT environment. This guide provide proven strategies and application approaches that can help them understand and reduce risks and complexity.

  • Mobility is reaching an inflection point in enterprise. Smart enterprises are moving away from tactical point solutions for mobile device management, looking instead for a secure, comprehensive, unified, and future-proof mobile-app platform. Enterprise applications are now at the heart of mobile productivity, data is shared outside the enterprise walls, and the data itself may reside on multiple devices and clouds. Read this guide to learn how a unified endpoint management (UEM) solution is critical to …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date
×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.