📄 fontdemodlg.cpp
字号:
// FontDemoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "FontDemo.h"
#include "FontDemoDlg.h"
#include <math.h>
#define DegToRnd(x) (x/180.*3.14159)
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFontDemoDlg dialog
CFontDemoDlg::CFontDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFontDemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFontDemoDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CFontDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFontDemoDlg)
DDX_Control(pDX, IDC_LIST1, m_lstFont);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFontDemoDlg, CDialog)
//{{AFX_MSG_MAP(CFontDemoDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFontDemoDlg message handlers
// 枚举字体时的回调函数,该函数将系统的所有可用字体及其字符集添加到列表框中,
// 由应用程序提供的参数 lParam 提供了指向该列表框的 CListBox 对象的指针
int WINAPI EnumFontFamProc(const LOGFONTA * lplf, const TEXTMETRICA *lptm,
unsigned long FontType, LPARAM lParam)
{
CListBox *pList=(CListBox*)lParam;
CString str;
// 将当前字体的字体名 (FaceName) 和字符集 (CharSet) 添加到列表框中
str.Format("FACENAME: %s \tCHARSET: %d", lplf->lfFaceName, lplf->lfCharSet);
pList->AddString(str);
return TRUE; // 返回 TRUE 以继续字体枚举的过程,返回 FALSE 将终止字体枚举的过程
}
BOOL CFontDemoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
// 更改列表框所用的字体为 9 磅 (12 象素) 大小的宋体
static CFont font;
font.CreatePointFont(90, "宋体");
m_lstFont.SetFont(&font);
// 设置列表框的制表符位置为 200 个对话框单位
m_lstFont.SetTabStops(200);
// 枚举当前系统的所有可用字体,将指向列表框的 CListBox 对象的指针作为应用程序提供的
// 参数传递给枚举字体回调函数 EnumFontFamProc
EnumFontFamilies(::GetDC(GetSafeHwnd()), NULL, EnumFontFamProc, (LPARAM)(&m_lstFont));
return TRUE; // return TRUE unless you set the focus to a control
}
void CFontDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CFontDemoDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
// 获得窗口的客户区设备上下文句柄
CPaintDC dc(this);
// 设置字体输出背景为透明
dc.SetBkMode(TRANSPARENT);
// 获得设备上下文所使用的当前字体
LOGFONT lf;
dc.GetCurrentFont()->GetLogFont(&lf);
CFont font1, font2;
CFont *pOldFont; // 保存设备上下文最初使用的字体对象
// 创建 font1 为 12 象素宋体
lf.lfCharSet=134;
lf.lfHeight=-12;
lf.lfWidth=0;
strcpy(lf.lfFaceName, "宋体");
font1.CreateFontIndirect(&lf);
// 更改当前字体为 20 象素的 Times New Roman,并且向上倾斜 40 度
lf.lfCharSet=0;
strcpy(lf.lfFaceName, "Times New Roman");
lf.lfEscapement=400;
lf.lfHeight=-20;
font2.CreateFontIndirect(&lf);
pOldFont=dc.SelectObject(&font2);
// 获得字符串 "lfEscapement= 400" 在输出时的宽度和高度
CSize sz=dc.GetTextExtent("lfEscapement= 400");
// 计算字符串合适的输出位置
dc.TextOut(10, 10+int(sz.cx*sin(DegToRnd(40))), "lfEscapement= 400");
// 将字体输出方向更改为向下倾斜 40 度
lf.lfEscapement=-400;
dc.SelectObject(pOldFont);
font2.DeleteObject();
font2.CreateFontIndirect(&lf);
dc.SelectObject(&font2);
// 计算字符串合适的输出位置
dc.TextOut(290-int(sz.cx*cos(DegToRnd(40))), 10, "lfEscapement=-400");
// 将字体输出方向更改为水平方向
lf.lfEscapement=0;
dc.SelectObject(pOldFont);
font2.DeleteObject();
font2.CreateFontIndirect(&lf);
dc.SelectObject(&font2);
// 对称于直线 x=150 输出字符串 "lfEscapement=0"
sz=dc.GetTextExtent("lfEscapement=0");
dc.TextOut(150-sz.cx/2, 110, "lfEscapement=0");
// 更改当前字体为最细的 50 象素大小的宋体
lf.lfCharSet=134;
strcpy(lf.lfFaceName, "宋体");
lf.lfEscapement=0;
lf.lfWeight=0;
lf.lfHeight=-50;
dc.SelectObject(pOldFont);
font2.DeleteObject();
font2.CreateFontIndirect(&lf);
dc.SelectObject(&font2);
// 输出一个 "细" 字
dc.TextOut(330, 10, "细");
// 在旁边使用 12 象素大小的宋体字给出当前字体的重量
dc.SelectObject(&font1);
dc.TextOut(395, 29, "lfWeight=0");
// 更改当前字体为最粗的 50 象素大小的字体
lf.lfWeight=1000;
dc.SelectObject(pOldFont);
font2.DeleteObject();
font2.CreateFontIndirect(&lf);
dc.SelectObject(&font2);
// 输出一个 "粗" 字
dc.TextOut(330, 80, "粗");
dc.SelectObject(&font1);
// 在旁边使用 12 象素大小的宋体字给出当前字体和重量
dc.TextOut(395, 99, "lfWeight=1000");
// 恢复设备上下文原有的 GDI 绘图对象
dc.SelectObject(pOldFont);
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CFontDemoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -