ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 설치 안 된 폰트 사용하기 - How to Use a Font Without Installing it
    프로그래밍/프로그래밍 관련 2011. 7. 21. 17:55

    Introduction

    Many times, a particular font needs to be used in an application due to the in-house graphics designer's font choice. In order for the application to use the fonts, the font needs to be installed using the installer. Too many fonts on the user machine may slow the system down considerably.

    You can actually get away without installing the font: GDI and GDI+ each provide two ways for you, as a programmer, to add a font for an application to use without installing it. I'll show you how in this article!

    GDI's AddFontResourceEx

    Let me first talk about GDI's two functions for adding fonts to an application for use. I'll then talk about GDI+'s own functions. You can use AddFontResourceEx to add a physical font file for an application to use.

    int AddFontResourceEx(
      LPCTSTR lpszFilename, 	// font file name
      DWORD fl,             	// font characteristics
      PVOID pdv             	// reserved
    );

    Here is an example of how to use AddFontResourceEx:

    CString szFontFile = "D:\\SkiCargo.ttf";
    
    int nResults = AddFontResourceEx(
        m_szFontFile, 		// font file name
        FR_PRIVATE,    	// font characteristics
        NULL);

    To use the font you've added, just specify its name in the CreateFont or CreateFontIndirect function like any other installed font. To know the name of the font, just right click on the TTF extension file in Windows Explorer and select "Open" and you will see its actual name. Or, you can use the TTF and TTC classes which I wrote, to know the font name.

    Note: The font file name ("SkiCargo.ttf") in this article is actually its font name, "SkiCargo"; this is usually not the case! To be on the safe side, use the Windows Explorer right click method, or the TTF and TTC classes I just mentioned, to find out the name!

    CClientDC dc(this);
    
    dc.SetBkMode(TRANSPARENT);
    
    LOGFONT lf;
    memset(&lf, 0, sizeof(lf));
    lf.lfHeight = -MulDiv(24, pDC->GetDeviceCaps(LOGPIXELSY), 72);
    lf.lfWeight = FW_NORMAL;
    lf.lfOutPrecision = OUT_TT_ONLY_PRECIS;
    wcscpy_s(lf.lfFaceName, L"SkiCargo");
    
    // create and select it
    CFont newFont;
    if (!newFont.CreateFontIndirect(&lf))
        return;
    CFont* pOldFont = dc.SelectObject(&newFont);
    
    // use a path to record how the text was drawn
    wchar_t buf[] = _T("The quick brown fox jumps over the lazy dog!");
    dc.TextOut( 10, 10, buf, wcslen(buf));
    
    // Put back the old font
    dc.SelectObject(pOldFont);

    You must remember to call RemoveFontResourceEx before the application exits. You should note that the parameters must be the same as the ones that you fed into AddFontResourceEx!

    BOOL RemoveFontResourceEx(
      LPCTSTR lpFileName,  	// name of font file
      DWORD fl,            	// font characteristics
      PVOID pdv            	// Reserved.
    );
    
    CString szFontFile = "D:\\SkiCargo.ttf";
    
    BOOL b = RemoveFontResourceEx(
        m_szFontFile, 		// name of font file
        FR_PRIVATE,   		// font characteristics
        NULL         		// Reserved.
        );


    출처 - http://www.codeproject.com/KB/GDI/NoInstalledFonts.aspx

    댓글