⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fontwindow.cpp

📁 这些源代码
💻 CPP
字号:
// FontWindow.cpp : implementation file
//

#include "stdafx.h"
#include "FontView.h"
#include "FontWindow.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFontWindow

CFontWindow::CFontWindow()
{
	memset ((char *) m_abc, '\0', 256 * sizeof (ABC));
	memset ((char *) m_abcf, '\0', 256 * sizeof (ABCFLOAT));
	memset ((char *) m_nWidths, '\0', 256 * sizeof (int));
	memset ((char *) m_fWidths, '\0', 256 * sizeof (float));
	memset ((char *) &m_TM, '\0', sizeof (TEXTMETRIC));
	memset ((char *) &m_rcSelect, '\0', sizeof (RECT));

	m_uiSelection = 0;
	m_uiMouseSelection = 0;
}

CFontWindow::~CFontWindow()
{
}


BEGIN_MESSAGE_MAP(CFontWindow, CStaticEx)
	//{{AFX_MSG_MAP(CFontWindow)
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFontWindow message handlers

void CFontWindow::OnPaint() 
{
int nWidth, nCellWidth;
int nHeight, nCellHeight;
int 	i, j;

	CPaintDC dc(this); // device context for painting
	dc.SelectObject (&m_Font);

	if (m_rcPosition.right == 0)
	{
		nWidth = m_rcClient.right - m_rcClient.left - 20;
		nWidth -= nWidth % 32;
		m_rcPosition.left = (m_rcClient.right - nWidth) / 2;
		m_rcPosition.right = m_rcPosition.left + nWidth;
		m_rcPosition.top = m_rcOriginal.top;
		m_rcPosition.bottom = m_rcClient.bottom - 10;
		m_rcPosition.bottom -= (m_rcPosition.bottom - 14) % 8;
		MoveWindow (&m_rcPosition, FALSE);
	}
	nWidth = m_rcPosition.right - m_rcPosition.left;
	nCellWidth = nWidth / 32 - 2;
	nHeight = m_rcPosition.bottom - m_rcPosition.top;
	nCellHeight = nHeight / 8 - 2;

	CBrush blackBrush ((COLORREF) 0x00000000);
	CBrush whiteBrush ((COLORREF) 0x00ffffff);
	CPen blackPen;
	blackPen.CreatePen (PS_SOLID, 2, RGB(0x00, 0x00, 0x00));
	dc.SelectObject (blackPen);
	dc.Rectangle (0, 0, nWidth, nHeight);
	for (j = 0; j < 8; ++j)
	{
		for (i = 0; i < 32; ++i)
		{
			if (!(i + j))
				continue;
			RECT rcChar;
			char ch;
			ch = i + j * 32;
			rcChar.top = 2 + j * (nCellHeight + 2);
			rcChar.bottom = rcChar.top + nCellHeight;
			rcChar.left = i * (nCellWidth + 2);
			rcChar.right = rcChar.left + nCellWidth;
			dc.DrawText (&ch, 1, &rcChar, DT_VCENTER | DT_CENTER | DT_NOPREFIX);
		}
	}
	RECT rc = {0, 0, nWidth, nHeight};
	dc.DrawEdge (&rc, EDGE_SUNKEN, BF_TOPLEFT);
	dc.DrawEdge (&rc, EDGE_ETCHED, BF_BOTTOMRIGHT);
	blackPen.DeleteObject ();
	blackPen.CreatePen (PS_SOLID, 1, RGB(0x00, 0x00, 0x00));
	dc.SelectObject (blackPen);
	for (i = 1; i < 32; ++i)
	{
		POINT ptTop = {(2 + nCellWidth) * i - 2, 1};
		POINT ptBottom = {ptTop.x, nHeight - 1};
		dc.MoveTo (ptTop);
		dc.LineTo (ptBottom);
	}
	for (i = 1; i < 8; ++i)
	{
		POINT ptLeft = {1, (2 + nCellHeight) * i - 1};
		POINT ptRight = {nWidth - 1, ptLeft.y};
		dc.MoveTo (ptLeft);
		dc.LineTo (ptRight);
	}
	HighlightSelection ((CWindowDC *) &dc);
}

void CFontWindow::HighlightSelection(CWindowDC* dc)
{
RECT	r, rc;
CPoint	Boundary[5];

	CWindowDC *pDC;
	if (dc == NULL)
		pDC = new CWindowDC (this);
	else
		pDC = dc;
	rc.left = 0;
	rc.right = m_rcPosition.right - m_rcPosition.left;
	rc.top = 0;
	rc.bottom = m_rcPosition.bottom - m_rcPosition.top;
	r = rc;

	int iPenWidth = 2;
	CPen blackPen;
	blackPen.CreatePen (PS_SOLID | PS_GEOMETRIC, iPenWidth, RGB(0x00, 0x00, 0xff));
	pDC->SelectObject (&blackPen);
	CBrush blackBrush ((COLORREF) 0x00ff0000);
	pDC->SelectObject (&blackBrush);
	int iInterval = (r.right) / 31;
	int jInterval = (r.bottom) / 8;
	int iObjectX = m_uiSelection % 32;
	int iObjectY = m_uiSelection / 32;
	int Left = iInterval * iObjectX - 1; // + 1;
	int Top = jInterval * iObjectY - 1; // + 1;

	m_rcSelect.left = Left;
	m_rcSelect.right = Left + iInterval;
	m_rcSelect.top = Top;
	m_rcSelect.bottom = Top + jInterval;

	Boundary[0].x = m_rcSelect.left + 1;
	Boundary[0].y = m_rcSelect.top + 1;
	Boundary[1].x = m_rcSelect.right;
	Boundary[1].y = m_rcSelect.top;
	Boundary[2].x = m_rcSelect.right;
	Boundary[2].y = m_rcSelect.bottom;
	Boundary[3].x = m_rcSelect.left;
	Boundary[3].y = m_rcSelect.bottom;
	Boundary[4] = Boundary[0];
	pDC->Polyline (Boundary, 5);

//	pDC->DrawEdge (&m_rcSelect, EDGE_SUNKEN, BF_TOPLEFT);
//	pDC->DrawEdge (&m_rcSelect, EDGE_BUMP, BF_BOTTOMRIGHT);
//	pDC->Draw3dRect (&m_rcSelect,
//					(COLORREF) 0x00ff00ff,
//					(COLORREF) 0x00ff00ff);
	if (dc == NULL)
		delete pDC;
}

void CFontWindow::SetFont(CString &FontName)
{
	m_Font.DeleteObject ();
	m_Font.CreatePointFont (85, FontName);
	Invalidate ();

	CWindowDC dc(this);
	dc.SelectObject (&m_Font);
	GetTextMetrics (dc.GetSafeHdc (), &m_TM);
	dc.GetCharWidth (0, 255, m_nWidths);
	dc.GetCharWidth (0, 255, m_fWidths);
	dc.GetCharABCWidths (0, 255, m_abc);
//	dc.GetCharABCWidths (0, 255, m_abcf);
	GetCharABCWidthsFloat (dc.GetSafeHdc (), 0, 255, m_abcf);
}

void CFontWindow::SetCharSelection(UINT sel)
{
	m_uiSelection = sel;
	Invalidate ();
}

void CFontWindow::OnLButtonDown(UINT nFlags, CPoint point) 
{
RECT	r;
int 	x, y, sel;

	r = m_rcPosition;
	int iInterval = (r.right - r.left) / 31;
	int yInterval = (r.bottom - r.top) / 8;
	x = point.x / iInterval;
	y = point.y / yInterval;
	sel = 0;
	for (int i = 0; i < y; ++ i)
		sel += 32;
	sel += x;
	if (!sel)
		return;
	SetCharSelection (sel);
	::SendMessage (GetParent()->m_hWnd, WM_USER+40, (WPARAM) (WM_USER+41), (LPARAM) sel);
	CStatic::OnLButtonDown(nFlags, point);
}

void CFontWindow::OnMouseMove(UINT nFlags, CPoint point) 
{
RECT	r;
int 	x, y;
UINT	sel;

	GetClientRect (&r);
	int iInterval = (r.right) / 31;
	int yInterval = (r.bottom) / 8;
	x = point.x / iInterval;
	y = point.y / yInterval;
	sel = 0;
	for (int i = 0; i < y; ++ i)
		sel += 32;
	sel += x;
	if (m_uiMouseSelection != sel)
	{
		m_uiMouseSelection = sel;
		::SendMessage (GetParent()->m_hWnd, WM_USER+40, (WPARAM) (WM_USER+40), (LPARAM) sel);
	}
	else
		CStaticEx::OnMouseMove(nFlags, point);
}

void CFontWindow::SetWindowRect(RECT &rcClient, RECT &rcFontView)
{
	m_rcClient = rcClient;
	m_rcOriginal = rcFontView;
	m_rcPosition.top = 0;
	m_rcPosition.left = 0;
	m_rcPosition.bottom = 0;
	m_rcPosition.right = 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -