MFC封裝類:CFontDialog
核心代碼如下:
1 void CGraphicView::OnFont() 2 { 3 CFontDialog dlg; 4 if( IDOK == dlg.DoModal() ) 5 { 6 if( m_font.m_hObject ) // 如果m_font對象已經和某個字體資源相關聯,要先釋放這個資源,之后才能和新的資源進行關聯,否則會報錯 7 m_font.DeleteObject(); 8 m_font.CreateFontIndirectW( dlg.m_cf.lpLogFont ); 9 m_strFontName = dlg.m_cf.lpLogFont->lfFaceName; 10 Invalidate(); // 使窗口無效,就會調用OnDraw函數實現重繪,在OnDraw函數中實現重繪操作; 11 } 12 }
效果如下: