MFC(VS2010)字体对话框

void CTwo::OnBnClickedFornt()
{
	CString strFontName;    // 字体名称   
	LOGFONT lf;             // LOGFONT变量   

	// 将lf所有字节清零   
	memset(&lf, 0, sizeof(LOGFONT));   

	// 将lf中的元素字体名设为“宋体”   
	_tcscpy_s(lf.lfFaceName, LF_FACESIZE, _T("宋体"));   

	// 构造字体对话框,初始选择字体名为“宋体”   
	CFontDialog fontDlg(&lf);   

	if (IDOK == fontDlg.DoModal())     // 显示字体对话框   
	{   
		// 如果m_font已经关联了一个字体资源对象,则释放它   
		if (m_font.m_hObject)   
		{   
			m_font.DeleteObject();   
		}   
		// 使用选定字体的LOGFONT创建新的字体   
		m_font.CreateFontIndirect(fontDlg.m_cf.lpLogFont);   
		// 获取编辑框IDC_FONT_EDIT的CWnd指针,并设置其字体   
		GetDlgItem(IDC_FRONT1)->SetFont(&m_font);   

		// 如果用户选择了字体对话框的OK按钮,则获取被选择字体的名称并显示到编辑框里   
		strFontName = fontDlg.m_cf.lpLogFont->lfFaceName;   
		SetDlgItemText(IDC_FRONT1, strFontName);   
	}   
}

猜你喜欢

转载自blog.csdn.net/weixin_42386373/article/details/89236319
今日推荐