• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1201
  • Last Modified:

Use of EnumFontFamilies

I need to use the EnumFontFamilies function to retrieve a listing off all of a user's installed fonts.  However, I can't for the life of me figure out how to use EnumFontFamilies.  I'd really appreciate an explanation of what each parameter in EnumFontFamilies work, and a some sample source code.  Relatively urgent.  TIA.
0
Artine
Asked:
Artine
  • 3
  • 2
1 Solution
 
prasanthCommented:
The WORDPAD sample program that comes with VC++ has a great example of how to use EnumFontFamilies in formatba.cpp. They fill a combo box with font names, but you can see ho it works to do whatever you want. Search for WORDPAD in the online help or check in the samples/mfc/ole subdirectory
0
 
ArtineAuthor Commented:
Although the Wordpad sample code does give an example of how to use EnumFontFamilies, it is used with a lot of other code, and, because I have no prior experience with Enumeration or callbacks, I don't know what is the enumeration code, and what is just code for the actual program.
0
 
snoeglerCommented:
The following code reads the available fonts into a CArray:

int CALLBACK EnumTheFonts(
                               LOGFONT *lpLF, TEXTMETRIC *lpTM, DWORD dwTyp, LPARAM lpData)
{
  CArray<CString,CString&> *paFonts=(CArray<CString,CString&>*)lpData;
  paFonts->Add(CString(lpLF->lfFaceName));
  return 1;
}

void GetFonts(CArray<CString,CString&>* aFonts)
{
// Get a temporary DC(for EnumFonts)
  HWND hDskWnd=::GetDesktopWindow();
  HDC     htempDC=::GetDC(hDskWnd);
  HDC     hMyDC=::CreateCompatibleDC(htempDC);
  ::ReleaseDC(hDskWnd,htempDC);
 
  ::EnumFonts(hMyDC,NULL,(FONTENUMPROC)EnumTheFonts,(LPARAM)aFonts);

  ::DeleteDC(hMyDC);
}

Just create a CArray<CString,CString&> and pass a pointer to that array to the
GetFonts() function. After that, the array is filled with the available fonts.
Hope this is what you needed :)

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ArtineAuthor Commented:
Sorry it has taken me so long to grade your answer, I've been away for a while.  I'll give 'ya the points since the code got me a lot farther along, however I don't understand the part about the CArray < CString...  Could you explain that a bit more?  I'm somewhat new to MFC, and Windows programming in general.
0
 
snoeglerCommented:
Thank you for grading :)

CArray<> is a class which just simplifies the handling with arrays.
Usually, if you need a string array, you'll do something like this:
  char **pstrArray;
  pstrArray=new char*[nStrings];
Then you can fill the pstrArray[] with pointers to strings, but it gets difficult when you try to add
a string which would lie beyond the array limits - in this case nStrings. Then you would have
to reallocate memory so that the new index is valid -  and this is what CArray is for.

It relatively simply to use:
CArray<int,int> aInts;  //defines a array of integers.
aInts.Add(5);  // adds a '5' to the array
aInts.Add(6);
int result=aInts[0]+aInts[1]; // -> 11

The argument type you have to pass to Add() or what you get from operator[] is the type
you've specified while declaration.
CArray is defined:

  template<class TYPE, class ARG_TYPE> class CArray

This means, CArray::Add() takes and argument of the type ARG_TYPE, while the elements
are internally stored as 'TYPE'. This is because you can pass data (or get it) by reference,
but if it would be stored internally as reference, the data would only be valid as long as its
reference is.
Another example:

CArray<CString,CString&> strings;
strings.Add(CString("HELLO"));
strings.Add(CString("WORLD!"));
CString result=strings[0]+strings[1]; // "HELLO WORLD!"

The Add() function in this case takes a reference to a CString. Internally, a copy of this string
is made and added to the array.

CString test;
strings.Add(test);
BOOL bEqualPointers=((&strings[0])==(&test));

bEqualPointers will be false if 'strings' is defined as above, because the argument for Add()
is first duplicated and then the duplicate is added to the array.

I hope this helped.
(Perhaps you should read about 'template classes' in the VC docs if you have problems
with what i posted above)

0
 
ArtineAuthor Commented:
Thank you *so* much for the clarification!  I've been trying (off and on in my free time) to enumerate all the fonts for the past month or two.  I got it now.  Thanks again!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now