下面的代码演示如何从 CModalDialog 派生对话框类和使用 SetFont() 成员函数:
/*
* Compiler options needed: None
*/
class CMyAboutBox : public CDialog
{
CFont m_font;
public:
// Constructor -- This code assumes a dialog box
// template named "ABOUTDLG" in the application's .RC file.
CMyAboutBox(CWnd* pParentWnd = NULL) :
CModalDialog("ABOUTDLG", pParentWnd) {};
BOOL OnInitDialog();
};
// OnInitDialog() function -- Called after Windows creates
// the dialog box but before it is painted on the screen.
BOOL CMyAboutBox::OnInitDialog()
{
LOGFONT lf; // Used to create the CFont.
CDialog::OnInitDialog(); // Call default ::OnInitDialog
memset(&lf, 0, sizeof(LOGFONT)); // Clear out structure.
lf.lfHeight = 20; // Request a 20-pixel-high font
strcpy(lf.lfFaceName, "Arial"); // with face name "Arial".
m_font.CreateFontIndirect(&lf); // Create the font.
// Use the font to paint a control. This code assumes
// a control named IDC_TEXT1 in the dialog box.
GetDlgItem(IDC_TEXT1)->SetFont(&m_font);
return TRUE;
}