Results 1 to 14 of 14

Thread: Getting the fonts

  1. #1
    Guest

    Post

    Im trying to make a font viewer and i need to fill a list box with the fonts. How do i get which fonts are installed?

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The EnumFontFamiliesEx function should be useful for this:
    EnumFontFamiliesEx
    The EnumFontFamiliesEx function enumerates all fonts in the system that match the font characteristics specified by the LOGFONT structure. EnumFontFamiliesEx enumerates fonts based on typeface name, character set, or both.

    int EnumFontFamiliesEx(
    HDC hdc, // handle to DC
    LPLOGFONT lpLogfont, // font information
    FONTENUMPROC lpEnumFontFamExProc, // callback function
    LPARAM lParam, // additional data
    DWORD dwFlags // not used; must be 0
    );
    Parameters
    hdc
    [in] Handle to the device context.
    lpLogfont
    [in] Pointer to a LOGFONT structure that contains information about the fonts to enumerate. The function examines the following members. Member Description
    lfCharset If set to DEFAULT_CHARSET, the function enumerates all fonts in all character sets. If set to a valid character set value, the function enumerates only fonts in the specified character set.
    lfFaceName If set to an empty string, the function enumerates one font in each available typeface name. If set to a valid typeface name, the function enumerates all fonts with the specified name.
    lfPitchAndFamily Must be set to zero for all language versions of the operating system.


    lpEnumFontFamExProc
    [in] Pointer to the application defined–callback function. For more information, see the EnumFontFamExProc function.
    lParam
    [in] Specifies an application–defined value. The function passes this value to the callback function along with font information.
    dwFlags
    This parameter is not used and must be zero.
    Return Values
    The return value is the last value returned by the callback function. This value depends on which font families are available for the specified device.

    Remarks
    The EnumFontFamiliesEx function does not use tagged typeface names to identify character sets. Instead, it always passes the correct typeface name and a separate character set value to the callback function. The function enumerates fonts based on the the values of the lfCharset and lfFacename members in the LOGFONT structure.

    As with EnumFontFamilies, EnumFontFamiliesEx enumerates all font styles. Not all styles of a font cover the same character sets. For example, Fontorama Bold might contain ANSI, Greek, and Cyrillic characters, but Fontorama Italic might contain only ANSI characters. For this reason, it's best not to assume that a specified font covers a specific character set, even if it is the ANSI character set. The following table shows the results of various combinations of values for lfCharSet and lfFaceName.

    Values Meaning
    lfCharSet = DEFAULT_CHARSET
    lfFaceName = '\0' Enumerates all fonts in all character sets.
    lfCharSet = DEFAULT_CHARSET
    lfFaceName = a specific font Enumerates all character sets and styles in a specific font.
    lfCharSet =a specific character set
    lfFaceName = '\0' Enumerates all styles of all fonts in the specific character set.
    lfCharSet =a specific character set
    lfFaceName = a specific font Enumerates all styles of a font in a specific character set.


    The following code sample shows how these values are used.

    //to enumerate all styles and charsets of all fonts:
    lf.lfFaceName[0] = '\0';
    lf.lfCharSet = DEFAULT_CHARSET;

    //to enumerate all styles and character sets of the Arial font:
    lstrcpy( (LPSTR)&lf.lfFaceName, "Arial" );
    lf.lfCharSet = DEFAULT_CHARSET;

    //to enumerate all styles of all fonts for the ANSI character set
    lf.lfFaceName[0] = '\0';
    lf.lfCharSet = ANSI_CHARSET;

    //to enumerate all styles of Arial font that cover the ANSI charset
    lstrcpy( (LPSTR)&lf.lfFaceName, "Arial" );
    lf.lfCharSet = ANSI_CHARSET;
    The callback functions for EnumFontFamilies and EnumFontFamiliesEx are very similar. The main difference is that the ENUMLOGFONTEX structure includes a script field.

    Note, based on the values of lfCharSet and lfFaceName, EnumFontFamiliesEx will enumerate the same font as many times as there are distinct character sets in the font. This can create an extensive list of fonts which can be burdensome to a user. For example, the Century Schoolbook font can appear for the Baltic, Western, Greek, Turkish, and Cyrillic character sets. To avoid this, an application should filter the list of fonts.

    Requirements
    Windows NT/2000: Requires Windows NT 4.0 or later.
    Windows 95/98: Requires Windows 95 or later.
    Header: Declared in Wingdi.h; include Windows.h.
    Library: Use Gdi32.lib.
    Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000.

    See Also
    Fonts and Text Overview, Font and Text Functions, EnumFontFamExProc, LOGFONT

    Built on Wednesday, February 09, 2000Requirements
    Windows NT/2000: Requires Windows NT 4.0 or later.
    Windows 95/98: Requires Windows 95 or later.
    Header: Declared in Wingdi.h; include Windows.h.
    Library: Use Gdi32.lib.
    Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000.
    See Also
    Fonts and Text Overview, Font and Text Functions, EnumFontFamExProc, LOGFONT
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Guest
    Quick question, how do i use that?!?!?!?!?!

  4. #4
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Something like this:
    Code:
    //declare the function
    int CALLBACK EnumFontFamProc(
      ENUMLOGFONT FAR *lpelf,  // pointer to logical-font data
      NEWTEXTMETRIC FAR *lpntm,  // pointer to physical-font data
      int FontType,            // type of font
      LPARAM lParam            // pointer to application-defined data
    );
    
    
    int CALLBACK EnumFontFamProc(ENUMLOGFONT FAR *lpelf,NEWTEXTMETRIC FAR *lpntm,int FontType,LPARAM lParam )
    {
    //you may add the font names to a listbox
    list1.AddString(lpelf->elfLogFont.lfFaceName);
    return 1;
    }
    
    //call the EnumFontFamiliesEx API function
    LOGFONT lf;
    lf.lfCharSet = DEFAULT_CHARSET;
     EnumFontFamiliesEx(HDC to use,&lf,(FONTENUMPROC)EnumFontFamProc,(LPARAM)this,0);
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  5. #5
    Guest
    Ok, i got it to list the fonts, but for some reason it will list the same font like 10 times in a row.

  6. #6
    Guest
    Ok, it WAS working, but now it gives an error and forces me to close the app at the line of code with the arrow....

    Code:
             LOGFONT lf;
             lf.lfCharSet = DEFAULT_CHARSET;
             HDC hhdc = GetWindowDC(hMainDialog);
    -------> EnumFontFamiliesEx(hhdc, &lf, (FONTENUMPROC) EnumFontFamExProc, (LPARAM)NULL, 0);
             ReleaseDC(hMainDialog, hhdc);

  7. #7
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Use
    Code:
    EnumFontFamiliesEx(hhdc, &lf,(FONTENUMPROC) EnumFontFamProc,...
    //not
    EnumFontFamiliesEx(hhdc, &lf,(FONTENUMPROC) EnumFontFamExProc,...
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  8. #8
    Guest
    Its still not working.

  9. #9
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    You said it worked the first time and then not. What have you changed. This works for me.
    Code:
    //declare the function
    int CALLBACK EnumFontFamProc(
      ENUMLOGFONT FAR *lpelf,  // pointer to logical-font data
      NEWTEXTMETRIC FAR *lpntm,  // pointer to physical-font data
      int FontType,            // type of font
      LPARAM lParam            // pointer to application-defined data
    );
    
    
    int CALLBACK EnumFontFamProc(ENUMLOGFONT FAR *lpelf,NEWTEXTMETRIC FAR *lpntm,int FontType,LPARAM lParam )
    {
    //you may add the font names to a listbox
    list1.AddString(lpelf->elfLogFont.lfFaceName);
    return 1;
    }
    
    //call the EnumFontFamiliesEx API function
    LOGFONT lf;
    lf.lfCharSet = DEFAULT_CHARSET;
     EnumFontFamiliesEx(HDC to use,&lf,(FONTENUMPROC)EnumFontFamProc,(LPARAM)this,0);
    Post the errors if you still have some.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  10. #10
    Guest
    There are no compile errors, when i run the debug version, a box apears and says it had an error and i must close. The release version, when i run it, just has nothing apear at all. It just exits itself.

  11. #11
    Guest
    Ok, i figured it out, it wasnt the code. On the main dialog i put a spin control, and that was screwing it up for some reason. sorry.

  12. #12
    Guest
    Ok, now that that works, do you know how to get the text from whatever item is selected. And do you know what message is sent when someone clicks on an item?

  13. #13
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Do you mean from the list box. In that case here you are:
    Code:
    #include <windowsx.h>
    ...
    int selected = ListBox_GetCurSel(hwndlb)  ;
    int textlen = ListBox_GetTextLen(hwndlb, selected) ;  
    char *lpszBuffer = new TCHAR[textlen];
    ListBox_GetText(hwndlb, selected, lpszBuffer) ;
    //lpszBuffer contains text
    The message is i think WM_COMMAND with LPARAM the hwnd of the list box
    Code:
    WM_COMMAND 
    wNotifyCode = HIWORD(wParam); // notification code 
    wID = LOWORD(wParam);         // item, control, or accelerator identifier 
    hwndCtl = (HWND) lParam;      // handle of control
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  14. #14
    Guest
    That was exactly what i was looking for, thanks. Now, im using the create font to use the information ive gatherd from the user to show the sample font. This is my code...

    Code:
    hFont = CreateFont(-MulDiv(Size, GetDeviceCaps(hDC, LOGPIXELSY), 72),     // nHeight
    				0,    // nWidth
    				0,    // nEscapement
    				0,    // nOrientation
    				Weight,    // fnWeight
    				Italic,    // fdwItalic
    				FALSE,    // fdwUnderline
    				FALSE,    // fdwStrikeOut
    				ANSI_CHARSET,    // fdwCharSet
    				OUT_DEFAULT_PRECIS,    // fdwOutputPrecision
    				CLIP_DEFAULT_PRECIS,    // fdwClipPrecision
    				PROOF_QUALITY,    // fdwQuality
    				DEFAULT_PITCH | FF_DONTCARE,    // fdwPitchAndFamily
    				FontName);
    The problem im having with is the width variable. I got a formula to change a point size in to the proper height, but not for the width. If i leave it a zero, then the size never changes. Any ideas?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width