#include "stdafx.h"
#include "CeFontDlg.h"
#include "CeLibRes.h"
CeFontDlg::CeFontDlg(DWORD nSize, LOGFONT* plogfont)
{
HWND hWnd = ::GetActiveWindow();
HDC hDC = ::GetWindowDC(hWnd);
m_cyPixelsPerInch = ::GetDeviceCaps(hDC, LOGPIXELSY);
m_nPointsPerInch = 72;
::ReleaseDC(hWnd, hDC);
if (NULL == plogfont)
{
// Get the attributes of the old font
m_hFont = (HFONT) ::GetStockObject(SYSTEM_FONT);
}
else
{
m_hFont = ::CreateFontIndirect(plogfont);
}
LOGFONT logfont;
GetObject(m_hFont, sizeof logfont, &logfont);
m_bUnder = (logfont.lfUnderline != FALSE);
m_bItal = (logfont.lfItalic != FALSE);
m_bBold = (logfont.lfWeight >= 700);
m_strFont = logfont.lfFaceName;
m_nPointSize = nSize;
m_strSize.SetLong(m_nPointSize);
// nSize = abs( (lfHeight * m_cyPixelsPerInch) / m_cyPixelsPerInch );
// lfHeight = -abs( m_cyPixelsPerInch * nSize / m_nPointsPerInch) );
}
CeFontDlg::~CeFontDlg()
{
if (m_hFont)
::DeleteObject(m_hFont);
}
inline BOOL CeFontDlg::OnInitDialog()
{
BOOL bRet = CeDialog::OnInitDialog();
// populate the combobox with the fonts
HDC hDC = GetWindowDC();
::EnumFontFamilies( hDC, NULL, (FONTENUMPROC) EnumFontFamProc, (LPARAM) m_hWnd);
// fill Size combobox with "common" sizes
TCHAR* Defaults[] =
{
_T("8"), _T("9"), _T("10"), _T("11"), _T("12"), _T("14"),
_T("16"), _T("18"), _T("20"), _T("22"), _T("24"), _T("26")
};
HWND hCombo = GetDlgItem(IDC_FONTSIZE);
for (int ii = 0; ii < (sizeof(Defaults)/sizeof(Defaults[0])); ii++)
ComboBox_AddString(hCombo, Defaults[ii]);
ComboBox_SelectString(hCombo, -1, m_strSize);
// Set the dialog control values
ComboBox_SelectString(GetDlgItem(IDC_FONT), -1, (LPCTSTR) m_strFont);
Button_SetCheck(GetDlgItem(IDC_CHECK_BOLD), m_bBold);
Button_SetCheck(GetDlgItem(IDC_CHECK_ITAL), m_bItal);
Button_SetCheck(GetDlgItem(IDC_CHECK_UNDER), m_bUnder);
::SetWindowText(GetDlgItem(IDC_STATIC_SAMPLE), _T("AaBbCcYyZz"));
return bRet;
}
BOOL CeFontDlg::OnCommand(WPARAM wParam, LPARAM lParam, bool& bHandled)
{
WORD wNotify = HIWORD(wParam); // notification code
WORD wId = LOWORD(wParam); // item, control, or accelerator identifier
HWND hwndCtl = (HWND) lParam; // handle of control
bHandled = false;
// control commands
switch (wId)
{
case IDC_FONT:
case IDC_FONTSIZE:
if (wNotify == CBN_KILLFOCUS || wNotify == CBN_SELCHANGE)
OnSelChange();
bHandled = true;
break;
case IDC_CHECK_BOLD:
case IDC_CHECK_ITAL:
case IDC_CHECK_UNDER:
if (wNotify == BN_CLICKED)
OnSelChange();
bHandled = true;
break;
}
return 0;
}
int CALLBACK CeFontDlg::EnumFontFamProc(
ENUMLOGFONT FAR *lpelf,
NEWTEXTMETRIC FAR *lpntm,
int FontType,
LPARAM lParam
)
{
HWND hCombo = ::GetDlgItem((HWND) lParam, IDC_FONT);
ComboBox_AddString(hCombo, (LPCTSTR)lpelf->elfFullName);
return 1;
}
void CeFontDlg::OnSelChange()
{
// The selection hasn't changed yet, so change it
// if ( IsChild(::GetFocus()) && GetFocus()->GetParent()->IsKindOf(RUNTIME_CLASS(CComboBox)))
// {
// CComboBox *cb = (CComboBox *)GetFocus()->GetParent();
// CString sText;
// if( cb->GetCurSel() != CB_ERR )
// {
// cb->GetLBText( cb->GetCurSel(), sText );
// cb->SetWindowText( sText );
// }
// }
// Get the attributes of the old font
LOGFONT logfont;
GetObject(m_hFont, sizeof logfont, &logfont);
// update all the variables from the dialog controls
m_bBold = (BST_CHECKED == Button_GetCheck(GetDlgItem(IDC_CHECK_BOLD)));
m_bItal = (BST_CHECKED == Button_GetCheck(GetDlgItem(IDC_CHECK_ITAL)));
m_bUnder = (BST_CHECKED == Button_GetCheck(GetDlgItem(IDC_CHECK_UNDER)));
m_strFont.GetWindowText(GetDlgItem(IDC_FONT));
m_strSize.GetWindowText(GetDlgItem(IDC_FONTSIZE));
m_nPointSize = m_strSize;
logfont.lfHeight = -abs( (m_cyPixelsPerInch * m_nPointSize) / m_nPointsPerInch );
logfont.lfUnderline = m_bUnder;
logfont.lfItalic = m_bItal;
if (m_bBold)
logfont.lfWeight = 700;
else
logfont.lfWeight = 400;
// not supported (at least not by me)
logfont.lfStrikeOut = FALSE;
logfont.lfUnderline = FALSE;
memcpy(logfont.lfFaceName, m_strFont, sizeof TCHAR * LF_FACESIZE);
::DeleteObject(m_hFont);
m_hFont = ::CreateFontIndirect(&logfont);
// set the font of the static control
HWND hWnd = GetDlgItem(IDC_STATIC_SAMPLE);
::SendMessage(hWnd, WM_SETFONT, (WPARAM) m_hFont, MAKELPARAM(TRUE, 0));
}