[ Home | Syllabus | Course Notes | Assignments | Search]
Important points:
CListBox control
CFont
Enumeration and Callback
Does not use resource editor
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 ( ) };
F1: This is a callback function used in enumerating the set of fonts currently installed on this computer - explained later
F2: Called by the default OnNcDestroy member function after the window has been destroyed. Derived classes can use this function for custom cleanup such as the deletion of the this pointer.
Used to create a list from which the user can make one or more selections.
Creation: Object created by constructor, then call member function "Create" which creates the window showing the object and setting its style
BOOL Create(
DWORD dwStyle, const RECT& rect,
CWnd* pParentWnd, UINT nID );
// dwStyle: lots of choices like multiple columns, sorted list,
multiple/single item selection
// rect: how big and where placed
// pParentWnd: pointer to the parent window
// nID: control ID (used for command message ID or to identify this control
object)
Adding item to the List: Can insert in a specific place or just add
int AddString(
LPCTSTR lpszItem );
// adds string at end or in sorted order depending on style
// lpszItem (long pointer to a string zero terminated)
// returns index where inserted
Single Item Selection: First get index of item selected (using 0-based array of list items), then retrieve value of the selected item using the index.
int GetCurSel(
) const;
// returns index of item selected or LB_ERR if error
int GetText( int
nIndex, LPTSTR lpszBuffer ) const;
void GetText( int nIndex,
CString& rString ) const;
// nIndex: index of item to be retrieved
// lpszBuffer: character array big enough to hold text retrieved
// rString: string to contain text of item retrieved.
ON_LBN_SELCHANGE : Sent whenever there is to be a change in the user selection
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.
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:
Change the parameters of the CreateFont
Add a check box for "bold" "italic" other aspects of a font
Add a CEdit control to change the sample string