You want to view all font in your PC, you want to do one program can easy to view it. You want to code it by yourself. You want everything but you don't know how to do it, This Tutorial will be for you
Explore My Other Channel for More Cool and Valuable Insights
👉 Youtube Learn Tech Tips👉 Tiktok
👉 Facebook:First you must build your GUI same here:
1. One Text Box contains some text which you input it
2. One List Box which you want to add all list font
3. One Text Box with properties read only = true for show your font
4. Two Check Box for support user choose "Use default text" or choose font style by italic
5. One Label with show font with you change
Download full projects here
GUI
Source Code
Using CreateFont and EnumFontFamiliesEx function to create font
// Enumerate the font families
::EnumFontFamiliesEx( (HDC)dc, &lf, (FONTENUMPROC)EnumFontFamProc,
(LPARAM)this, 0 );
// Create the font to be used
m_fSampFont.CreateFont( (iHeight - 5), 0, 0,0, FW_NORMAL,
bItalic,0,0, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, m_strFontName);
/***********************************************************************
Developer: Zidane (huuvi168@gmail.com)
Last Modified: 2015-09-02
**********************************************************************/
// Days7_TextAndFontDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Days7_TextAndFont.h"
#include "Days7_TextAndFontDlg.h"
#include <tchar.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CDays7_TextAndFontDlg dialog
CDays7_TextAndFontDlg::CDays7_TextAndFontDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDays7_TextAndFontDlg::IDD, pParent)
, m_strSampleText(_T(""))
, m_strFontName(_T(""))
, m_strDisplayText(_T(""))
, m_iCount(0)
, m_bUserEnteredText(FALSE)
, m_bItalic(FALSE)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDays7_TextAndFontDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT_TEXT, m_strSampleText);
DDX_Control(pDX, IDC_LIST_FONT, m_cFontList);
DDX_LBString(pDX, IDC_LIST_FONT, m_strFontName);
DDX_Text(pDX, IDC_STATIC_DISPLAYTEXT, m_strDisplayText);
DDX_Control(pDX, IDC_STATIC_DISPLAYTEXT, m_cDisplayText);
DDX_Text(pDX, IDC_EDIT_COUNT, m_iCount);
DDX_Check(pDX, IDC_CHECK_USETEXT, m_bUserEnteredText);
DDX_Check(pDX, IDC_CHECK_ITALIC, m_bItalic);
}
BEGIN_MESSAGE_MAP(CDays7_TextAndFontDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_EN_CHANGE(IDC_EDIT_TEXT, &CDays7_TextAndFontDlg::OnEnChangeEditText)
ON_LBN_SELCHANGE(IDC_LIST_FONT, &CDays7_TextAndFontDlg::OnLbnSelchangeListFont)
ON_BN_CLICKED(IDC_CHECK_USETEXT, &CDays7_TextAndFontDlg::OnBnClickedCheckUsetext)
ON_BN_CLICKED(IDC_CHECK_ITALIC, &CDays7_TextAndFontDlg::OnBnClickedCheckItalic)
END_MESSAGE_MAP()
// CDays7_TextAndFontDlg message handlers
BOOL CDays7_TextAndFontDlg::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
FillFontList();
// show the number of font
m_iCount = m_cFontList.GetCount();
UpdateData(FALSE);
// initialize the text to be entered
m_strSampleText = _T("https://learn-tech-tips.blogspot.com");
// copy the text to the font sample area
m_strDisplayText = m_strSampleText;
// Update the dialog
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
// Font build by ViLH (huuvi168@gmail.com) Zidane
int CALLBACK EnumFontFamProc(LPENUMLOGFONT lpelf, LPNEWTEXTMETRIC lpntm,
DWORD nFontType, long lParam)
{
// Create a pointer to th dialog window
CDays7_TextAndFontDlg* pWnd = (CDays7_TextAndFontDlg*) lParam;
// limit the list to TrueType fonts
if ( (nFontType & TRUETYPE_FONTTYPE) == TRUETYPE_FONTTYPE )
{
// Addd the font name to the listbox
pWnd->m_cFontList.AddString(lpelf->elfLogFont.lfFaceName);
}
// Return 1 to continue font enumeration
return 1;
}
void CDays7_TextAndFontDlg::FillFontList(void)
{
int iCount; // the number of font
int iCurCount; // the current font
CString strCurFont; // the current font name
CString strPrevFont = _T(""); // the previous font name
LOGFONT lf;
// Initialize the LOGFONT structure
lf.lfCharSet = DEFAULT_CHARSET;
// ******************************
// strcpy = wcscpy;
// Not set(mode): strcpy;
// Unicode(mode): wcscpy;
// ******************************
wcscpy(lf.lfFaceName, _T("") );
// Clear the ListBox
m_cFontList.ResetContent();
// Create a device context variable
CClientDC dc(this);
// Enumerate the font families
::EnumFontFamiliesEx( (HDC)dc, &lf, (FONTENUMPROC)EnumFontFamProc,
(LPARAM)this, 0 );
// get the number of fonts in the list box
iCount = m_cFontList.GetCount();
// loop from the last deleting the duplicate entries
for (iCurCount = iCount; iCurCount > 0; iCurCount--)
{
// get the current font name
m_cFontList.GetText( (iCurCount - 1), strCurFont );
// is it the same as the previous font name?
if (strCurFont == strPrevFont)
//if yes, then delete it;
m_cFontList.DeleteString(iCurCount - 1);
// set the previous font name to current font name
strPrevFont = strCurFont;
}
}
void CDays7_TextAndFontDlg::OnEnChangeEditText()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
UpdateData(TRUE);
// copy the current text to the font sample;
m_strDisplayText = m_strSampleText;
// update the dialog with the variable
UpdateData(FALSE);
}
void CDays7_TextAndFontDlg::SetMyFont(void)
{
CRect rRect; // the rectangle of the display area
int iHeight; // the height of the display area
// has a font been selected?
if ( m_strFontName != _T("") )
{
// get the dimensions of the font sample display area
m_cDisplayText.GetWindowRect(&rRect);
// Calculate the area height
iHeight = rRect.top - rRect.bottom;
// make sure the height is positive
if ( iHeight < 0 )
iHeight = 0 - iHeight;
// release the current font
CFont m_fSampFont;
m_fSampFont.Detach();
bool bItalic = 0;
if (m_bItalic == TRUE)
bItalic = 1;
else
bItalic = 0;
// Create the font to be used
m_fSampFont.CreateFont( (iHeight - 5), 0, 0,0, FW_NORMAL,
bItalic,0,0, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS,
CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH |
FF_DONTCARE, m_strFontName);
// Set the font for sample display area
m_cDisplayText.SetFont (&m_fSampFont);
}
}
void CDays7_TextAndFontDlg::OnLbnSelchangeListFont()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if (m_bUserEnteredText) // true
m_strDisplayText = m_strSampleText;
else
m_strDisplayText = m_strFontName;
UpdateData(FALSE);
SetMyFont();
}
void CDays7_TextAndFontDlg::OnBnClickedCheckUsetext()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if ( m_bUserEnteredText == TRUE )
m_strDisplayText = m_strSampleText;
else
m_strDisplayText = m_strFontName;
UpdateData(FALSE);
}
void CDays7_TextAndFontDlg::OnBnClickedCheckItalic()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
SetMyFont();
}
Download full projects here
Thank you for reading this post. I hope you found it helpful and easy to follow. If you have any feedback or questions about
Font Rendering Techniques with C++ on a PC
,
please share them in the comments below. I would love to hear from you and discuss this topic further
✋✋✋✋
Webzone Tech Tips, all things Tech Tips for web development
- I am Zidane, See you next time soon ✋✋✋✋