添加系统字体mfc

void CFontConfigDlg::OnBnClickedBtnInstall()
{
//安装字体
CString strFullPath = _T(“”);
CString strFileName = _T(“”);
CString strFontName = _T(“”);
if (!GetTtfFontFile(strFullPath, strFileName, strFontName))
{
return;
}
CString strSysPath = GetSysFontPath();
CString strSysFile = strSysPath + strFileName;
if (PathFileExists(strSysFile))
{
MessageBox(_T(“系统字体已存在!”), _T(“提示”), MB_OK|MB_ICONWARNING);
return;
}
if (!CopyFile(strFullPath, strSysFile, FALSE))
{
MessageBox(_T(“复制字体文件失败!”), _T(“提示”), MB_OK|MB_ICONERROR);
return;
}
//添加字体到系统字体表
int nFontNum = AddFontResource(strSysFile);
if (nFontNum <= 0)
{
MessageBox(_T(“AddFontResource失败!”), _T(“提示”), MB_OK|MB_ICONERROR);
return;
}
//永久字体注册
HKEY phkResult;
DWORD RegResult;
LONG Rlt = RegCreateKeyEx(HKEY_LOCAL_MACHINE,_T(“SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts”),
0, _T(“REG_SZ”), 0, KEY_WRITE, 0, &phkResult, &RegResult);
if (Rlt != ERROR_SUCCESS)
{
MessageBox(_T(“注册表键值创建失败!”), _T(“提示”), MB_OK|MB_ICONERROR);
return;
}
//CString转BYTE*
int nLength = strFileName.GetLength();
string sFileName = strFileName.GetBuffer(nLength);
BYTE* m_byte=(BYTE*)sFileName.c_str();
Rlt = RegSetValueEx(phkResult, strFontName, 0, REG_SZ, m_byte, nLength);
if (Rlt != ERROR_SUCCESS)
{
MessageBox(_T(“注册表键值设置失败!”), _T(“提示”), MB_OK|MB_ICONERROR);
return;
}
Rlt = RegCloseKey(phkResult);
if (Rlt != ERROR_SUCCESS)
{
MessageBox(_T(“注册表键值关闭失败!”), _T(“提示”), MB_OK|MB_ICONERROR);
return;
}
::SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
MessageBox(_T(“注册字体成功!”), _T(“提示”), MB_OK|MB_ICONINFORMATION);
}
void CFontConfigDlg::OnBnClickedBtnUnins()
{
//卸载字体
CString strFullPath = _T(“”);
CString strFileName = _T(“”);
CString strFontName = _T(“”);
if (!GetTtfFontFile(strFullPath, strFileName, strFontName))
{
return;
}
CString strSysPath = GetSysFontPath();
CString strSysFile = strSysPath + strFileName;
if (!PathFileExists(strSysFile))
{
MessageBox(_T(“系统字体文件不存在!”), _T(“提示”), MB_OK|MB_ICONWARNING);
return;
}
//删除注册表
HKEY registryKey;
LONG Rlt = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
_T(“SOFTWARE\\Microsoft\

  

posted on 2020-03-13 15:55  lydstory  阅读(416)  评论(0)    收藏  举报

导航