Windows Systems Programming: Spring 2002

[ Home | Syllabus | Course Notes | Assignments | Search]


ListBox/Enumeration - the FontView Example 

(source code here)

Important points: 

Let's look at the header file:

class CMyApp : public CWinApp
{
public:
    virtual BOOL InitInstance ( );
};

class CMainWindow : public CWnd
{
protected:
    int m_cxChar;
    int m_cyChar;

    CFont m_fontMain; // New object to explore
    CFont m_fontSample;

    CStatic m_wndLBTitle; // all control objects created programmically
    CListBox m_wndListBox; // important, but complicated, object
    CButton m_wndCheckBox;
    CButton m_wndGroupBox;
    CStatic m_wndSampleText;
    CButton m_wndPushButton;

    void FillListBox ( );

public:
    CMainWindow ( );

	static int CALLBACK EnumFontFamProc (ENUMLOGFONT* lpelf,
		NEWTEXTMETRIC* lpntm, int nFontType, LPARAM lParam); 

protected:
    virtual void PostNcDestroy ( ); 

    afx_msg int OnCreate (LPCREATESTRUCT lpcs); // message in the CWnd base class
    afx_msg void OnPushButtonClicked ( );   // messages for our controls
    afx_msg void OnCheckBoxClicked ( );
    afx_msg void OnSelChange ( ); // called when user selects a font

    DECLARE_MESSAGE_MAP ( )
};

CListBox Control

 Used to create a list from which the user can make one or more selections.


Enumerating List of Fonts

int EnumFontFamilies(
  HDC hdc,                        // handle to DC
  LPCTSTR lpszFamily,             // font family
  FONTENUMPROC lpEnumFontFamProc, // callback function
  LPARAM lParam                   // additional data
);

This function call starts an enumeration process that will call the "callback" function each time a new font is enumerated. 

The prototype for the callback function is

int CALLBACK EnumFontFamProc(
  ENUMLOGFONT *lpelf,    // logical-font data
  NEWTEXTMETRIC *lpntm,  // physical-font data
  DWORD FontType,        // type of font
  LPARAM lParam          // application-defined data
);

The return value is used to control whether or not the enumeration will continue.

NOTE: lParam is used to pass one 32 bit data value between the function that starts the enumeration and the callback function. In this example, we will pass the pointer to the CMainWindow object so that the callback function can access its member functions.


CFont

The most important function is

BOOL CreateFont( int nHeight, int nWidth, int nEscapement, int nOrientation, int nWeight, BYTE bItalic, BYTE bUnderline, BYTE cStrikeOut, BYTE nCharSet, BYTE nOutPrecision, BYTE nClipPrecision, BYTE nQuality, BYTE nPitchAndFamily, LPCTSTR lpszFacename );

// FONTS are complicated due to differences in display characteristics, alignment, design.

There are many different fonts and one has to ask why. What is the essence of say a single Arial character 'a' - is it the bits that are set? what if rescaled by 10%. What is one font family is not present - can you substitute another? One could spend a lot of time understanding the intricacies of fonts design and display. Even questions such as how long a given string of characters will be on the screen is  complicated by proportional fonts.
"iiiiiiiiii" (10 'i's) is short than
"GGGGGGGGGG" (10 'G's)

As usual we will adopt a need to know attitude - although experimentation with the program might prove useful.


Examine the code to see the above in action.

Experiments:

 

 

 

 


Copyright chris wild 1999-2002.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: February 05, 2002.