📄 fontedit.cpp
字号:
if(pThis && lplf->lfFaceName[0] != '@')
pThis->m_cmbFont.AddString(lplf->lfFaceName);
return TRUE;
}
//================================================================================
//= 初始化对话框
//================================================================================
BOOL CdlgCharEdit::OnInitDialog()
{
CDialog::OnInitDialog();
CString strFontName;
CWinApp *pApp = AfxGetApp();
// 装入数据
m_data.LoadFromProfile();
// 绑定控件!
this->m_Spin_H.SetBuddy(this->GetDlgItem(IDC_ED_H));
this->m_Spin_W.SetBuddy(this->GetDlgItem(IDC_ED_W));
// 绑定数据范围
this->m_Spin_H.SetRange(0,256);
this->m_Spin_W.SetRange(0,256);
// 取得数据
this->m_CharH = pApp->GetProfileInt(_T("TFont"),_T("FontH"),24);
this->m_CharW = pApp->GetProfileInt(_T("TFont"),_T("FontW"),24);
// 计算当前数值
if(this->m_CharH >= 0 && this->m_CharH < 256)
this->m_Spin_H.SetPos(this->m_CharH);
else
this->m_Spin_H.SetPos(24);
if(this->m_CharW >= 0 && this->m_CharW < 256)
this->m_Spin_W.SetPos(this->m_CharW);
else
this->m_Spin_W.SetPos(24);
// 枚举字体名称
int ret = EnumFonts(
this->GetDC()->GetSafeHdc(), // 你程序的设备DC
(LPCTSTR)NULL, // 默认选择的字体
(FONTENUMPROC)tsEnumFontsProc, // 枚举字体的回调函数
(LPARAM)this // 程序支持, 比如CListCtrl指针, 传递给回调函数
);
// 取得设置
strFontName = pApp->GetProfileString(_T("TFont"),_T("FontName"),_T("System"));
// 设置默认
this->m_cmbFont.SetWindowText(strFontName);
// 粗体字
if(this->m_data.m_bFontBold)
this->m_chkFontBold.SetCheck(1);
else
this->m_chkFontBold.SetCheck(0);
// 保存以下
pApp->WriteProfileString(_T("TFont"),_T("FontName"),strFontName);
// 更新数据
this->UpdateData();
return TRUE;
}
//================================================================================
//= 初始化字体
//================================================================================
BYTE CdlgCharEdit::SetFontArray(BYTE * pArr)
{
int iSize,i;
// 无效就返回
if(NULL == pArr)
return 0;
// 计算大小
iSize = (pArr[0] * pArr[1] + 7) / 8 + 3;
// 无效值
if(iSize <= 3)
return 0;
this->m_CharW = pArr[0];
this->m_CharH = pArr[1];
// 释放数据
if(this->m_FontArr != NULL)
{
free(this->m_FontArr);
this->m_FontArr = NULL;
}
// 分配数据
this->m_FontArr = (BYTE *)malloc(iSize);
// 备份数据
for(i = 0; i < iSize ; i++)
{
this->m_FontArr[i] = pArr[i];
}
// 完成返回 1;
return 1;
}
//================================================================================
//= 释放内存
//================================================================================
void CdlgCharEdit::OnDestroy()
{
CDialog::OnDestroy();
CString strFontName;
CWinApp *pApp = AfxGetApp();
// 取得字体名称
this->m_cmbFont.GetWindowText(strFontName);
// 取得大小值
this->m_CharW = this->m_Spin_W.GetPos();
this->m_CharH = this->m_Spin_H.GetPos();
// 保存字体特性.
pApp->WriteProfileString(_T("TFont"),_T("FontName"),strFontName);
pApp->WriteProfileInt(_T("TFont"),_T("FontW"),this->m_CharW);
pApp->WriteProfileInt(_T("TFont"),_T("FontH"),this->m_CharH);
// 保存数据
this->m_data.SaveToProfile();
}
//================================================================================
//= 返回字体数据!( 注意:这里返回的是分配的数据的副本,源数据会被修改或释放!!)
//================================================================================
BYTE * CdlgCharEdit::GetFontArray(void)
{
BYTE *pReturn = NULL;
int i,iSize;
// 无效返回
if(!this->m_FontArr)
return NULL;
// 计算尺寸(宽度*高度/8位+3个字节开头, 加 7 四舍五入)
iSize = (this->m_FontArr[0] * this->m_FontArr[1] + 7) / 8 + 3;
// 无效尺寸就返回空
if(iSize <= 3)
return NULL;
// 分配内存
pReturn = (BYTE *)malloc(iSize);
// 分配失败
if(!pReturn)
return NULL;
// 拷贝数据,然后返回.
for(i = 0; i< iSize; i ++)
{
pReturn[i] = this->m_FontArr[i];
}
// 释放掉原来数据
this->m_FontArr = NULL;
// 返回有效数据
return pReturn;
}
//================================================================================
//= 根据字体创建字体位图,然后生成字符!
//================================================================================
BYTE * CdlgCharEdit::MakeCharArray(CString & pString, UINT nCharW, UINT nCharH, CString & pFontName)
{
CFont vFont,*pFont,*pOldFont;
CDC *pdc = NULL;
// 字体大小
CSize cFontSize;
// 显示位置
CPoint p;
BYTE *pBitArr = NULL;
UINT iSize = 0;
UINT i,j;
UINT OldX = 0;
UINT iWeight = 0;
BOOL bOK = 0;
// 前景颜色
COLORREF bCo = 0;
COLORREF fCo = 0;
// 计算位置(控件位置)
RECT rClient;
RECT rParent;
// 点阵字体!
pdc = this->GetDC();
// 无效的设备环境就返回.
if(!pdc)
return NULL;
// 取得显示框架位置
CWnd *pWnd = this->GetDlgItem(IDC_PIC_DRAW);
// 控件位置
pWnd->GetClientRect(&rClient);
pWnd->ClientToScreen(&rClient);
// 窗口位置
this->GetClientRect(&rParent);
this->ClientToScreen(&rParent);
// 字体重量
if(this->m_data.m_bFontBold)
iWeight = FW_BOLD;
else
iWeight = FW_REGULAR;
// 创建字体
bOK = vFont.CreateFont(
nCharH, // nHeight
nCharW, // nWidth
0, // nEscapement
0, // nOrientation
iWeight, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
DEFAULT_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY , // nQuality
DEFAULT_PITCH , // nPitchAndFamily
pFontName); // lpszFacename
if(bOK == FALSE)
{
TRACE("字体创建失败!\n");
return NULL ;
}
pFont = &vFont;
pOldFont = pdc->SelectObject(pFont);
// 取得文字大小.
cFontSize = pdc->GetTextExtent(pString);
nCharW = cFontSize.cx;
nCharH = cFontSize.cy;
// 是否字节对齐
if(this->m_data.m_bAutoAdjust)
{
nCharW = ((nCharW + 7) / 8) * 8;
}
// 计算面积(根据面积知道需要的空间大小).
iSize = (nCharW * nCharH + 7) / 8 + 3;
// 空间限制.
if(iSize > MAX_MALLOC_SIZE)
{
TRACE("文件太大!\n");
return NULL;
}
// 分配空间
pBitArr = (BYTE *)malloc(iSize);
// 分配空间失败
if(!pBitArr)
return NULL;
// 计算位置
p.x = (rClient.right - rClient.left - nCharW) / 2 + (rClient.left - rParent.left );
p.y = (rClient.bottom - rClient.top - nCharH) / 2 + (rClient.top - rParent.top );
// 填充背景
pdc->FillSolidRect(p.x,p.y,nCharW,nCharH,0xffffff);
// 备份数据
OldX = p.x;
// 对齐方式
if(this->m_data.m_bAutoAdjust)
{
// 对齐模式(中间对齐)
switch(this->m_data.m_iAdjustMode)
{
case 1: // 居 中
p.x += (nCharW - cFontSize.cx) >> 1;
break;
case 2: // 右对齐
p.x += (nCharW - cFontSize.cx) ;
break;
default:
break;
}
}
// 显示文字
pdc->TextOut(p.x,p.y,pString);
// 还原数据
p.x = OldX;
// 保存相关信息
pBitArr[0] = nCharW;
pBitArr[1] = nCharH;
pBitArr[2] = nCharW >> 3;//就是除以8.
// 背景色
bCo = 0;
for(i = 0;i < nCharH; i++)
{
for(j = 0; j< nCharW; j++)
{
fCo = pdc->GetPixel(j + p.x,i + p.y);
/* [使用颜色差异区分两种颜色]
if( (fCo & 0x0000ff) - (bCo & 0x0000ff) < 0x000022 ||
(fCo & 0x00ff00) - (bCo & 0x00ff00) < 0x002200 ||
(fCo & 0xff0000) - (bCo & 0xff0000) < 0x220000 )
*/
if( fCo == bCo)
{
// fill bits for array
pBitArr[3 + (i * nCharW + j) / 8] |= 0x80 >> ((i * nCharW + j) % 8);
}
else
{
// cleran bits for arry
pBitArr[3 + (i * nCharW + j) / 8] &= ~(0x80 >> ((i * nCharW + j) % 8));
}
}
}
// 撤销字体
pdc->SelectObject(pOldFont);
// 删除字体
pFont->DeleteObject();
// 返回数组
return pBitArr;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -