Background
Break News
How to add local font to Tailwind Css and NextJS? - Tutorial Design Pattern? - Blockchain Technology, How to create own Bitcoin virtual currency - Zustand mordern management state - Design Pattern - Flyweight Pattern? - Docker Full training Topic

[Tutorial] Font Rendering Techniques with C++ on a PC

Tuesday, 15 September 2015
|
Read: Completed in minutes

[Tutorial] Font Rendering Techniques with C++ on a PC


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




Font Rendering Techniques with C++ on a PC


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


Font Rendering Techniques with C++ on a PC



Font Rendering Techniques with C++ on a PC






Font Rendering Techniques with C++ on a PC

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 ✋✋✋✋

🙇🏼🙇🏼 We Appreciate Your Comments and Suggestions - Webzone, all things Tech Tips web development
Popular Webzone Tech Tips topic maybe you will be like it - by Webzone Tech Tips - Zidane
As a student, I found Blogspot very useful when I joined in 2014. I have been a developer for years . To give back and share what I learned, I started Webzone, a blog with tech tips. You can also search for tech tips zidane on Google and find my helpful posts. Love you all,

I am glad you visited my blog. I hope you find it useful for learning tech tips and webzone tricks. If you have any technical issues, feel free to browse my posts and see if they can help you solve them. You can also leave a comment or contact me if you need more assistance. Here is my blog address: https://learn-tech-tips.blogspot.com.

My blog where I share my passion for web development, webzone design, and tech tips. You will find tutorials on how to build websites from scratch, using hot trends frameworks like nestjs, nextjs, cakephp, devops, docker, and more. You will also learn how to fix common bugs on development, like a mini stackoverflow. Plus, you will discover how to easily learn programming languages such as PHP (CAKEPHP, LARAVEL), C#, C++, Web(HTML, CSS, javascript), and other useful things like Office (Excel, Photoshop). I hope you enjoy my blog and find it helpful for your projects. :)

Thanks and Best Regards!
Follow me on Tiktok @learntechtips and send me a direct message. I will be happy to chat with you.
Webzone - Zidane (huuvi168@gmail.com)
I'm developer, I like code, I like to learn new technology and want to be friend with people for learn each other
I'm a developer who loves coding, learning new technologies, and making friends with people who share the same passion. I have been a full stack developer since 2015, with more than years of experience in web development.
Copyright @2022(November) Version 1.0.0 - By Webzone, all things Tech Tips for Web Development Zidane
https://learn-tech-tips.blogspot.com