[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Use of EnumFontFamilies

Posted on 1998-09-06
6
Medium Priority
?
1,165 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 450 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Loops Section Overview
Suggested Courses
Course of the Month19 days, 5 hours left to enroll

834 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