Solved

Use of EnumFontFamilies

Posted on 1998-09-06
6
1,081 Views
Last Modified: 2013-11-20
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
Comment
Question by:Artine
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:prasanth
ID: 1321786
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
 

Author Comment

by:Artine
ID: 1321787
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
 
LVL 6

Accepted Solution

by:
snoegler earned 150 total points
ID: 1321788
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 

Author Comment

by:Artine
ID: 1321789
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
 
LVL 6

Expert Comment

by:snoegler
ID: 1321790
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
 

Author Comment

by:Artine
ID: 1321791
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

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question