C++字体安装与卸载

代码下载:http://pan.baidu.com/share/link?shareid=1574173637&uk=721859502

核心代码:

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\\Windows NT\\CurrentVersion\\Fonts”), 0, KEY_ALL_ACCESS, &registryKey);
if (Rlt != ERROR_SUCCESS)
{
MessageBox(_T(“注册表键值打开失败!”), _T(“提示”), MB_OK|MB_ICONERROR);
return;
}

Rlt = RegDeleteValue (registryKey, strFontName);
if (Rlt != ERROR_SUCCESS)
{
MessageBox(_T(“注册表键值删除失败!”), _T(“提示”), MB_OK|MB_ICONERROR);
return;
}

Rlt = RegCloseKey(registryKey);
if (Rlt != ERROR_SUCCESS)
{
MessageBox(_T(“注册表键值关闭失败!”), _T(“提示”), MB_OK|MB_ICONERROR);
return;
}

//从系统字体表移除字体
if (!RemoveFontResource(strSysFile))
{
MessageBox(_T(“RemoveFontResource失败!”), _T(“提示”), MB_OK|MB_ICONERROR);
return;
}

if (!DeleteFile(strSysFile))
{
MessageBox(_T(“DeleteFile失败!”), _T(“提示”), MB_OK|MB_ICONERROR);
return;
}

::SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);

MessageBox(_T(“卸载字体成功!”), _T(“提示”), MB_OK|MB_ICONINFORMATION);
}

CString CFontConfigDlg::GetSysFontPath()
{
TCHAR sWinDir [MAX_PATH];
GetWindowsDirectory(sWinDir,MAX_PATH);
CString sFontDir(sWinDir);
sFontDir += _T(“\\Fonts\\”);

return sFontDir;
}

BOOL CFontConfigDlg::GetTtfFontFile(CString& strFullPath, CString& strFileName, CString& strFontName)
{
strFullPath = _T(“”);
strFileName = _T(“”);

GetDlgItemText(IDC_TTF_PATH, strFullPath);
GetDlgItemText(IDC_FONT_NAME, strFontName);

if (strFullPath.IsEmpty())
{
MessageBox(_T(“请选择字体文件!”), _T(“提示”), MB_OK|MB_ICONWARNING);
return FALSE;
}

if (!PathFileExists(strFullPath))
{
MessageBox(_T(“字体文件不存在!”), _T(“提示”), MB_OK|MB_ICONWARNING);
return FALSE;
}

int nIndex = strFullPath.ReverseFind(‘\\’);
strFileName = strFullPath.Right(strFullPath.GetLength() – nIndex – 1);

return TRUE;
}

此条目发表在C++分类目录。将固定链接加入收藏夹。

发表评论