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

📄 formatbar.cpp

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

#include "stdafx.h"
#include "Two.h"
#include "FormatBar.h"

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

struct FONTDATA
	{
	CString 		strFont;
	CComboBoxEx*	FontBox;
	CFormatBar		*Papa;
	};

/////////////////////////////////////////////////////////////////////////////
// CFormatBar

CFormatBar::CFormatBar()
{
}

CFormatBar::~CFormatBar()
{
}


BEGIN_MESSAGE_MAP(CFormatBar, CToolBar)
	//{{AFX_MSG_MAP(CFormatBar)
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
	ON_NOTIFY_EX(CBEN_GETDISPINFO, IDC_FONTLIST, OnGetdispinfoFontBox)
	ON_NOTIFY_REFLECT(CBEN_GETDISPINFO, OnGetdispinfoFontBox)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFormatBar message handlers

int CFormatBar::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CToolBar::OnCreate(lpCreateStruct) == -1)
		return -1;

	if (!LoadToolBar(IDR_FORMATBAR))
		return (-1);
	
	// TODO: Add your specialized creation code here
	CWindowDC dc(this);
	m_Font.CreatePointFont (80, _T("MS Sans Serif"));
	CFont *pOldFont = dc.SelectObject (&m_Font);
	TEXTMETRIC tm;
	dc.GetTextMetrics (&tm);
	int cxChar = tm.tmAveCharWidth;
	int cyChar = tm.tmHeight + tm.tmExternalLeading;
	dc.SelectObject (pOldFont);

	CRect rc;

	SetButtonInfo (0, IDC_FONTLIST, TBBS_SEPARATOR, 24 * cxChar);
	GetItemRect (0, &rc);
//
//	Add 25 percent of the character height to the
//	overall depth of the list box. The baseline is above
//	the descenders, so that 25 per cent of the type
//	is outside the rectangle.
	rc.bottom = rc.top + (cyChar * 16) + cyChar / 4;

	m_FontList.Create (WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWNLIST,
						rc, this, IDC_FONTLIST);

	m_Images.Create (16, 16, ILC_MASK, 2, 2);
	HICON hIcon = AfxGetApp()->LoadIcon (IDI_TRUETYPEFONTS);
	m_Images.Add (hIcon);
	DeleteObject (hIcon);
	hIcon = AfxGetApp()->LoadIcon (IDI_PRINTERFONTS);
	m_Images.Add (hIcon);
	DeleteObject (hIcon);
	m_FontList.SetImageList (&m_Images);

	FONTDATA fd;
	CString strFont = _T("Times New Roman");
	fd.FontBox = &m_FontList;
	fd.strFont = strFont;
	fd.Papa = this;
	::EnumFontFamilies ((HDC) dc, NULL, (FONTENUMPROC) AddFontName, (LPARAM) &fd);

	int nCount = m_FontData.GetCount ();
	POSITION pos = m_FontData.GetHeadPosition ();
	CFontData font = m_FontData.GetHead ();
	for (int n = 0; pos != NULL; ++n)
	{
		font = m_FontData.GetNext (pos);
		COMBOBOXEXITEM cbi;
		memset (&cbi, '\0', sizeof (COMBOBOXEXITEM));
		cbi.iItem = n;
		cbi.mask = CBEIF_IMAGE | CBEIF_TEXT | CBEIF_SELECTEDIMAGE | CBS_SORT;
//		cbi.iImage = I_IMAGECALLBACK;
//		cbi.iSelectedImage = I_IMAGECALLBACK;
//		cbi.pszText = LPSTR_TEXTCALLBACK;
		cbi.iImage = font.m_nIcon;
		cbi.iSelectedImage = font.m_nIcon;
		cbi.pszText = (char *) (LPCSTR) font.m_strName;
		m_FontList.InsertItem (&cbi);
		if (!font.m_strName.CompareNoCase (strFont))
			m_FontList.SetCurSel (n);
	}
//	m_FontData.RemoveAll ();
	SetButtonInfo (1, IDC_POINTSIZE, TBBS_SEPARATOR, 5 * cxChar);
	GetItemRect (1, &rc);
//
//	Don't have to be so accurate with a plain combo box.
//	The list box will size itself on a line boundary.
	rc.bottom = rc.top + (cyChar * 10);
	rc.left += 2;
	rc.right = rc.left + 8 * cxChar;
	m_PointSize.Create (WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWNLIST,
						rc, this, IDC_POINTSIZE);
	m_PointSize.SetFont (&m_Font);
	int nPoints[] = {8, 10, 11, 12, 14, 15, 18, 24, 30, 36, 42, 48, 54, 60, 72};
	for (int i = 0; i < (sizeof (nPoints) / sizeof (int)); ++i)
	{
		CString str;
		str.Format ("%d", nPoints[i]);
		m_PointSize.AddString ((LPCSTR) str);
	}
	m_PointSize.SelectString (-1, "12");

	return 0;
}

int CALLBACK CFormatBar::AddFontName(ENUMLOGFONT *lpelfe, NEWTEXTMETRIC *lpntme, int FontType, LPARAM lParam)
{
static int nItem = 0;

	FONTDATA *fd = (FONTDATA *) lParam;
	CFontData font;
	CFormatBar *bar = fd->Papa;
	font.m_strName = lpelfe->elfLogFont.lfFaceName;
	switch (FontType)
	{
		case RASTER_FONTTYPE:
		case DEVICE_FONTTYPE:
			font.m_nIcon = 1;
			break;
		case TRUETYPE_FONTTYPE:
			font.m_nIcon = 0;
			break;
	}
	POSITION pos;
	if (bar->m_FontData.GetCount () == 0)
	{
		bar->m_FontData.AddHead (font);
		return (TRUE);
	}
	pos = bar->m_FontData.GetHeadPosition ();
	CFontData data;
	int nCount = bar->m_FontData.GetCount ();
	data = bar->m_FontData.GetHead ();
	for (int n = 0; n < nCount; ++n)
	{
		int nResult = data.m_strName.CompareNoCase (font.m_strName);
		if (nResult > 0)
			break;
//		{
//			bar->m_FontData.InsertAfter (pos, font);
//			return (TRUE);
//		}
		data = bar->m_FontData.GetNext (pos);
	}
	if (pos == NULL)
	{
		if (font.m_strName.CompareNoCase (data.m_strName) < 0)
            {
				pos = bar->m_FontData.GetTailPosition ();
                bar->m_FontData.InsertBefore (pos, font);
            }
            else
                bar->m_FontData.AddTail (font);
			return (TRUE);
	}
	bar->m_FontData.GetPrev (pos);
	bar->m_FontData.InsertBefore (pos, font);
	return (TRUE);
/*	
	next = m_FontData.GetTail ();
	for (pos = m_FontData.GetHeadPosition ();
		 pos != NULL;
		 m_FontData.
next = m_States.GetHead();  next = m_States.GetNext (pos))
        {
			if (CompareStrings (next, States[i]) > 0)
				break;
        }

*/




	CComboBoxEx *FontBox = fd->FontBox;
	COMBOBOXEXITEM cbi;
	memset (&cbi, '\0', sizeof (COMBOBOXEXITEM));
	cbi.iItem = nItem++;
	cbi.mask = CBEIF_IMAGE | CBEIF_TEXT | CBEIF_SELECTEDIMAGE | CBS_SORT;
	switch (FontType)
	{
		case TRUETYPE_FONTTYPE:
			cbi.iImage = 0;
			cbi.iSelectedImage = 0;
			break;
		default:
			cbi.iImage = 1;
			cbi.iSelectedImage = 1;
			break;
	}
	cbi.pszText = lpelfe->elfLogFont.lfFaceName;
	FontBox->InsertItem (&cbi);
	if (fd->strFont == lpelfe->elfLogFont.lfFaceName)
		FontBox->SetCurSel (nItem - 1);
	return (TRUE);
}

void CFormatBar::GetFontSelection(CString &strFont)
{
	int nIndex = m_FontList.GetCurSel ();
	m_FontList.GetLBText (nIndex, strFont);
}

void CFormatBar::GetSizeSelection(int &nSize)
{
	CString strSel;
	int nIndex = m_PointSize.GetCurSel ();
	m_PointSize.GetLBText (nIndex, strSel);
	nSize = atoi ((LPCSTR) strSel);
}

void CFormatBar::SetFontSelection(CString &strFontName)
{
	COMBOBOXEXITEM boxitem;
	char szSelName[_MAX_PATH];
	memset (&boxitem, '\0', sizeof (COMBOBOXEXITEM));
	boxitem.iItem = m_FontList.GetCurSel ();
	boxitem.mask = CBEIF_TEXT;
	boxitem.pszText = szSelName;
	boxitem.cchTextMax = _MAX_PATH;
	m_FontList.GetItem (&boxitem);
	if (!strcmp (boxitem.pszText, (LPCSTR) strFontName))
		return;

	boxitem.mask = CBEIF_TEXT;
	boxitem.pszText = szSelName;
	boxitem.cchTextMax = _MAX_PATH;

	int nCount = m_FontList.GetComboBoxCtrl()->GetCount ();
	for (int nIndex = 0; nIndex < nCount; ++nIndex)
	{
		boxitem.iItem = nIndex;
		m_FontList.GetItem (&boxitem);
		if (!strcmp (szSelName, (LPCSTR) strFontName))
			break;
	}
	if (nCount != nIndex)
		m_FontList.SetCurSel (nIndex);
}

void CFormatBar::SetPointSelection(int nPoints)
{
CString strPoints;
	strPoints.Format ("%d", nPoints);
	int nIndex = m_PointSize.FindString (-1, (LPCSTR) strPoints);
	if (nIndex == CB_ERR)
	{
		nIndex = m_PointSize.AddString (strPoints);
		m_PointSize.SetCurSel (nIndex);
		return;
	}
	int nCurSel = m_PointSize.GetCurSel ();
	if (nCurSel == nIndex)
		return;
	m_PointSize.SetCurSel (nIndex);
}

void CFormatBar::OnGetdispinfoFontBox(UINT nID, NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	NMCOMBOBOXEX* pDispInfo = (NMCOMBOBOXEX *)pNMHDR;

	*pResult = 0;
}

⌨️ 快捷键说明

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