📄 fontedit.cpp
字号:
// CellEdit.cpp : 实现文件
//
#include "stdafx.h"
#include "TFont.h"
#include "FontEdit.h"
#include "Normal.h"
// 显示字符阵列
void DrawFont(CDC *dc,UINT cx,UINT cy,UINT cw,UINT ch,ULONG co,BYTE *arr);
// CdlgCharEdit 对话框
IMPLEMENT_DYNAMIC(CdlgCharEdit, CDialog)
CdlgCharEdit::CdlgCharEdit(CWnd* pParent /*=NULL*/)
: CDialog(CdlgCharEdit::IDD, pParent)
, m_CharId(0)
, m_CharW(0)
, m_CharH(0)
, m_FontArr(NULL)
, m_Font(NULL)
{
// 字节对齐
}
CdlgCharEdit::~CdlgCharEdit()
{
// 有数据就要释放
if(this->m_FontArr)
{
free(this->m_FontArr);
this->m_FontArr = NULL;
}
}
void CdlgCharEdit::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_SPIN_H, m_Spin_H);
DDX_Control(pDX, IDC_SPIN_W, m_Spin_W);
DDX_Control(pDX, IDC_CB_FONT, m_cmbFont);
DDX_Control(pDX, IDC_CHK_FONT_BOLD, m_chkFontBold);
}
BEGIN_MESSAGE_MAP(CdlgCharEdit, CDialog)
ON_WM_PAINT()
ON_WM_DESTROY()
END_MESSAGE_MAP()
// CdlgCharEdit 消息处理程序
//================================================================================
//= 显示图片
//================================================================================
BYTE CdlgCharEdit::ShowPicture(CDC *pdc)
{
RECT r,rParent;
int font_w,font_h;
int font_x,font_y;
r.left = r.right = 0;
r.top = r.bottom = 0;
// 取得显示框架位置
CWnd *pWnd = this->GetDlgItem(IDC_PIC_DRAW);
// 是否有效
if(NULL == pWnd || NULL == this->m_FontArr)
{
TRACE(("pWnd is NULL or this->m_FontArr is Null\n"));
return 0;
}
// 控件位置和大小
pWnd->GetClientRect(&r);
pWnd->ClientToScreen(&r);
// 窗口大小
this->GetClientRect(&rParent);
this->ClientToScreen(&rParent);
// 计算后居中
r.left -= rParent.left ;
r.top -= rParent.top ;
r.right -= rParent.left ;
r.bottom -= rParent.top ;
// 计算显示位置(居中显示)
font_w = this->m_FontArr[0];
font_h = this->m_FontArr[1];
font_x = r.left + ((r.right - r.left) - font_w) / 2;
font_y = r.top + ((r.bottom - r.top) - font_h) / 2;
// 显示当前数据
DrawFont(pdc,font_x,font_y,0,0,0xff0000,(BYTE *)(this->m_FontArr));
static CString strMessage;
strMessage.Format(_T("Width:\n%d\nHeight:\n%d\nByte:\n%d."),font_w,font_h,((font_w * font_h + 7) / 8 + 3));
this->SetDlgItemText(IDC_TXT_NOTE,strMessage);
return 0;
}
//================================================================================
//= 刷新窗口时重新绘制界面
//================================================================================
void CdlgCharEdit::OnPaint()
{
CPaintDC dc(this);
// 显示阵列图
this->ShowPicture(&dc);
}
//================================================================================
//= 命令处理
//================================================================================
BOOL CdlgCharEdit::OnCommand(WPARAM wParam, LPARAM lParam)
{
// 临时数据
BYTE *pBuffer = NULL;
// 显示的文字
CString strShowText;
// 字体名称
CString strFontName;
// 显示窗口
CStatic *pPictrue = (CStatic *)this->GetDlgItem(IDC_PIC_DRAW);
int i;
CFileDialog dlgOpenFile(TRUE,_T(""),_T(""),
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
_T("All Picture Files (*.bmp,*.jpg,*.wmf,*.emf,*.gif,*.ico,*.tiff)")
_T("|*.bmp;*.jpg;*.jpeg;*.wmf;*.emf;*.gif;*.ico;*.tiff|")
_T("Bmp File (*.bmp)|*.bmp|")
_T("Jpeg File (*.Jpeg,*.Jpg)|*.jpg;*.jpeg|")
_T("Gif File (*.gif)|*.gif|")
_T("Icon File (*.ico)|*.ico|")
_T("Tiff File (*.tiff)|*.tiff|")
_T("wmf File (*.wmf)|*.wmf|")
_T("Any File (*.*)|*.*|"),this);
TRACE(_T("OnCommand(%d, %ld)\n"),wParam,lParam);
switch(wParam)
{
case IDC_OK:
//--------------------------------------------------------------------
// 确定后就保存数据
//--------------------------------------------------------------------
this->EndDialog(IDC_OK);
break;
case IDC_CANCEL:
//--------------------------------------------------------------------
// 取消后不保存数据
//--------------------------------------------------------------------
this->EndDialog(IDC_CANCEL);
break;
case IDC_BT_TEXT:
//--------------------------------------------------------------------
// 取得需要显示的文字
//--------------------------------------------------------------------
this->GetDlgItemText(IDC_ED_TEXT,strShowText);
// 粗体字
this->m_data.m_bFontBold = this->m_chkFontBold.GetCheck();
// 取得字体名称
this->m_cmbFont.GetWindowText(strFontName);
// 特殊符号表示
if(strShowText[0] == '{' && strShowText[strShowText.GetLength()-1] == '}')
{
CString m_strNum;
for(i = 1; i < strShowText.GetLength() - 1;i++ )
{
m_strNum.AppendChar(strShowText[i]);
}
strShowText = MakeFormatString(m_strNum);
}
// 生成字符阵列
pBuffer = this->MakeCharArray(strShowText,this->m_Spin_W.GetPos(),this->m_Spin_H.GetPos(),strFontName);
// 生成成功
if(pBuffer != NULL)
{
// 释放指针
if(this->m_FontArr)
{
free(this->m_FontArr);
this->m_FontArr = NULL;
}
// 修改指针
this->m_FontArr = pBuffer;
// 重绘
this->Invalidate();
}
break;
case IDC_BT_PICTURE:
//--------------------------------------------------------------------
// 选择位图文件
//--------------------------------------------------------------------
int iRet =dlgOpenFile.DoModal();
if(iRet)
{
BYTE *pArray = this->MakeBmpArray(dlgOpenFile.GetFileName(),this->GetDC(),0,this->m_data.m_bAutoAdjust);
if(pArray)
{
if(this->m_FontArr)
free(this->m_FontArr);
this->m_FontArr = pArray;
this->Invalidate();
}
}
break;
}
return CDialog::OnCommand(wParam, lParam);
}
//================================================================================
//= 生成位图阵列
//================================================================================
BYTE *CdlgCharEdit::MakeBmpArray(CString FileName, CDC * pdc, COLORREF ForeColor,BOOL ByteAdjust)
{
// 图片显示
CImage oImg;
CWnd *pWnd = this->GetDlgItem(IDC_PIC_DRAW);
BYTE *pReturn= NULL;
RECT rClient;
RECT rParent;
UINT W,H;
UINT i,j,iSize;
ULONG Co;
// 装入图片
oImg.Load(FileName);
// 装入失败
if(oImg.IsNull())
return NULL;
// 无效设备环境
if(!pdc)
return NULL;
// 区域
pWnd->GetClientRect(&rClient);
pWnd->ClientToScreen(&rClient);
this->GetClientRect(&rParent);
this->ClientToScreen(&rParent);
rClient.left -= rParent.left;
rClient.top -= rParent.top ;
rClient.right -= rParent.left;
rClient.bottom -= rParent.top ;
// 图像比例
if(oImg.GetWidth() > (rClient.right - rClient.left) ||
oImg.GetHeight() > (rClient.bottom - rClient.top))
{
W = rClient.right - rClient.left;
H = rClient.bottom - rClient.top;
if(((float)oImg.GetHeight() / (float)oImg.GetWidth()) > ((float)H / (float)W))
{
H = rClient.bottom - rClient.top;
W = (UINT)(H * (float)oImg.GetWidth() / (float)oImg.GetHeight());
}
else
{
W = rClient.right - rClient.left;
H = (UINT)(W * (float)oImg.GetHeight() / (float)oImg.GetWidth());
}
}
else
{
W = oImg.GetWidth();
H = oImg.GetHeight();
}
// 绘制图像
oImg.Draw(pdc->GetSafeHdc(),rClient.left ,rClient.top ,W,H);
// 自动对齐
if(ByteAdjust)
{
W = (W + 7) / 8 * 8;
}
iSize = (W * H + 7) / 8 + 3;
if(iSize > (MAX_MALLOC_SIZE) || iSize < 3)
{
return NULL;
}
pReturn = (BYTE *)malloc(iSize);
if(!pReturn)
{
return NULL;
}
// 前景色
Co = ForeColor;
// 字体阵列头处理
pReturn[0] = W;
pReturn[1] = H;
pReturn[2] = W >> 3;
// 数据判断后修改
for(i = 0;i < H; i++)
{
for(j = 0; j< W; j++)
{
ForeColor = pdc->GetPixel(j + rClient.left,i + rClient.top);
if( (ForeColor & 0x0000ff) - (Co & 0x0000ff) < 0x000022 ||
(ForeColor & 0x00ff00) - (Co & 0x00ff00) < 0x002200 ||
(ForeColor & 0xff0000) - (Co & 0xff0000) < 0x220000 )
{
// fill bits for array
pReturn[3 + (i * W + j) / 8] |= 0x80 >> ((i * W + j) % 8);
}
else
{
// cleran bits for arry
pReturn[3 + (i * W + j) / 8] &= ~(0x80 >> ((i * W + j) % 8));
}
}
}
return pReturn;
}
//================================================================================
//= 回调函数
//================================================================================
BOOL CALLBACK tsEnumFontsProc(
CONST LOGFONT *lplf, // 枚举的逻辑字体
CONST TEXTMETRIC *lptm, // 物理字体信息
DWORD dwType, // 字体类型
LPARAM lpData // 传递给回调函数的数据指针
)
{
CdlgCharEdit *pThis = (CdlgCharEdit *)lpData;
// 添加,并且过滤掉'@'开头的字体.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -