Solved

Use of EnumFontFamilies

Posted on 1998-09-06
6
1,044 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now