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?
Printable View
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?
The EnumFontFamiliesEx function should be useful for this:
Quote:
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
Quick question, how do i use that?!?!?!?!?!
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);
Ok, i got it to list the fonts, but for some reason it will list the same font like 10 times in a row.
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);
Use
Code:EnumFontFamiliesEx(hhdc, &lf,(FONTENUMPROC) EnumFontFamProc,...
//not
EnumFontFamiliesEx(hhdc, &lf,(FONTENUMPROC) EnumFontFamExProc,...
Its still not working.
You said it worked the first time and then not. What have you changed. This works for me.
Post the errors if you still have some.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);
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.
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.
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?
Do you mean from the list box. In that case here you are:
The message is i think WM_COMMAND with LPARAM the hwnd of the list boxCode:#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
Code:WM_COMMAND
wNotifyCode = HIWORD(wParam); // notification code
wID = LOWORD(wParam); // item, control, or accelerator identifier
hwndCtl = (HWND) lParam; // handle of control
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...
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?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);