Introduction
This is a program to view system fonts of different names, sizes and weights.
Background
This program will allow the user to see what different fonts and sizes will look like on different background colors.
To view large fonts, the user can select maximize button. Right click the center of the screen and select the background color for the "Static Control". Click Select Font and pick a font to display.
Using the Code
Some items that need to be preset in "OnInitDialog
".
BOOL CFontSelectDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
...
...
...
bContinue = FALSE;
m_hbrush = CreateSolidBrush(RGB(220, 220, 220));
rgbColor = RGB(0, 0, 0);
m_rgbColor = (RGB(220, 220, 220));
SetTimer(1, 500, NULL);
The code for the "Select Font Button".
"rgbCurrent
" is set to black and is passed when you call "ChooseFont
" MFC library common Dialog.
If the call to the Dialog is successful, it returns TRUE
and you can get the returned values.
void CFontSelectDlg::OnBnClickedSelectFont()
{
BOOL bRet;
CHOOSEFONT cf;
static LOGFONT lf;
static COLORREF rgbCurrent;
rgbCurrent = RGB(0, 0, 0);
rgbColor = rgbCurrent;
int nPointSize;
ZeroMemory(&cf, sizeof(cf));
cf.lStructSize = sizeof (cf);
cf.hwndOwner = NULL;
cf.lpLogFont = &lf;
cf.rgbColors = rgbCurrent;
cf.Flags = CF_SCREENFONTS | CF_EFFECTS;
bRet = ChooseFont(&cf);
if(bRet)
{
rgbColor = cf.rgbColors;
m_font.CreateFontIndirect(&lf);
m_static1.SetFont(&m_font, TRUE);
nPointSize = cf.iPointSize;
CString szDisplay;
CString szFont;
CString szPointSize;
CString szWeight;
szFont.Format(_T("\r\n%s\r\n"), lf.lfFaceName);
szPointSize.Format(_T("Point Size: %ld\r\n"), nPointSize);
szWeight.Format(_T("Weight: %ld\r\n"), lf.lfWeight);
szDisplay = szFont;
szDisplay += szPointSize;
szDisplay += szWeight;
m_static1.SetWindowTextA(szDisplay);
}
}
The code for "OnCtrlColor
". This function sets the text and background color when the program displays the string
in the output window.
HBRUSH CFontSelectDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
HBRUSH hbrush;
hbrush = m_hbrush;
if(nCtlColor == CTLCOLOR_STATIC)
{
pDC->SetTextColor(rgbColor);
pDC->SetBkColor(m_rgbColor);
hbrush = m_hbrush;
}
return hbrush;
}
The code for "OnSize
". This code changes the size of the main window to display a larger font.
void CFontSelectDlg::OnSize(UINT nType, int cx, int cy)
{
CDialogEx::OnSize(nType, cx, cy);
theApp.ProcessIdleMsg();
CWnd *pWnd;
HWND hDlg;
RECT rc;
RECT rcItem;
int buttonWidth;
int buttonHeight;
int buttonWidth2;
int buttonHeight2;
GetClientRect(&rc);
pWnd = GetDlgItem(IDCANCEL);
hDlg = pWnd->GetSafeHwnd();
::GetWindowRect(hDlg, &rcItem);
buttonWidth = rcItem.right - rcItem.left;
buttonHeight = rcItem.bottom - rcItem.top;
::MoveWindow(hDlg, (rc.right - buttonWidth - 32),
(rc.bottom - buttonHeight - 8), buttonWidth, buttonHeight, TRUE);
pWnd = GetDlgItem(IDOK);
hDlg = pWnd->GetSafeHwnd();
::GetWindowRect(hDlg, &rcItem);
buttonWidth2 = rcItem.right - rcItem.left;
buttonHeight2 = rcItem.bottom - rcItem.top;
::MoveWindow(hDlg, ((rc.right - (buttonWidth + 32)) -
(buttonWidth2 + 8)), (rc.bottom - buttonHeight2 - 8), buttonWidth2, buttonHeight2, TRUE);
pWnd = GetDlgItem(IDC_SELECT_FONT);
hDlg = pWnd->GetSafeHwnd();
::GetWindowRect(hDlg, &rcItem);
buttonWidth = rcItem.right - rcItem.left;
buttonHeight = rcItem.bottom - rcItem.top;
::MoveWindow(hDlg, ((rc.right - buttonWidth - 32) / 2),
(rc.bottom - buttonHeight - 8), buttonWidth, buttonHeight, TRUE);
pWnd = GetDlgItem(IDC_STATIC_FONT);
hDlg = pWnd->GetSafeHwnd();
::MoveWindow(hDlg, (rc.left + 32), (rc.top + 32), (rc.right - rc.left) - 64,
(rc.bottom - rc.top) - 96, TRUE);
m_static1.SetFont(&m_font, TRUE);
Invalidate();
}
The code for "OnEraseBkgnd
". This code changes the portion of the main window that is not used to display the selected font.
BOOL CFontSelectDlg::OnEraseBkgnd(CDC* pDC)
{
CRect rc;
GetClientRect(&rc);
pDC->FillSolidRect(rc, GetSysColor(COLOR_ACTIVECAPTION));
return TRUE;
}
The code to pick the background color uses the "Right Mouse Button".
The right mouse button is used to select the background color of the selected font.
The right mouse is also used by the main program to display a context menu, therefore I check to see if the cursor is inside my display window frame.
If it is, set the bContinue
flag to TRUE
to allow selection of the background color.
void CFontSelectDlg::OnRButtonDown(UINT nFlags, CPoint point)
{
CRect rc;
CWnd *pWnd;
HWND hWnd;
CPoint pt;
GetCursorPos(&pt);
pWnd = GetDlgItem(IDC_STATIC_FONT);
hWnd = pWnd->GetSafeHwnd();
::GetWindowRect(hWnd, &rc);
if(((pt.x > rc.left) && (pt.x < rc.right)) &&
((pt.y > rc.top) && (pt.y < rc.bottom)))
bContinue = TRUE;
else
bContinue = FALSE;
CDialogEx::OnRButtonDown(nFlags, point);
}
void CFontSelectDlg::OnRButtonUp(UINT nFlags, CPoint point)
{
if(bContinue)
{
CHOOSECOLOR cc;
static COLORREF acrCustClr[16];
HWND hwnd = NULL;
HBRUSH hbrush;
static DWORD rgbCurrent;
ZeroMemory(&cc, sizeof(cc));
cc.lStructSize = sizeof(cc);
cc.hwndOwner = hwnd;
cc.lpCustColors = (LPDWORD) acrCustClr;
cc.rgbResult = rgbCurrent;
cc.Flags = CC_FULLOPEN | CC_RGBINIT;
if (ChooseColor(&cc)==TRUE)
{
hbrush = CreateSolidBrush(cc.rgbResult);
m_hbrush = hbrush;
rgbCurrent = cc.rgbResult;
m_rgbColor = cc.rgbResult;
m_static1.SetWindowTextA(_T("\r\nOk,\r\nSelect\r\nFont."));
}
}
CDialogEx::OnRButtonUp(nFlags, point);
}
And the last thing to do, display the string
using "OnTimer
".
void CFontSelectDlg::OnTimer(UINT_PTR nIDEvent)
{
KillTimer(1);
m_static1.SetWindowTextA(_T("\r\n\r\nRight click to set Background Color."));
CDialogEx::OnTimer(nIDEvent);
}
Points of Interest
To display a string
in a "Static Control", you can't have the code in "OnInitDialog
" because the window has not been created yet. I used a 500 millisecond timer to display the text.
History
- January 24th, 2019: V 1.0