Fonts

Font is a collection of small bitpatters which are used to render graphical output of ASCII characters in output device like monitors or printers. The entire set of bitpatterns are combined in one single file with an extension as TTF or FON. Font files are located in Fonts directory under Windows. Windows GDI subsystem loads these fonts as font related Win32 calls are made. There are two types of fonts - 1) normal and 2) true type. Normal font files comes with the extension FON while true type font files have the extension TTF.

CreateFont syntax

The CreateFont function creates a logical font with the specified characteristics. Characteristics or attributes are taken as the input parameters to this API and the function returns a handle to the font object.

HFONT CreateFont(
int     nHeight,
int     nWidth,
int     nEscapement,
int     nOrientation,
int     fnWeight,
DWORD   fdwItalic,
DWORD   fdwUnderline,
DWORD   fdwStrikeOut,
DWORD   fdwCharSet,
DWORD   fdwOutputPrecision,
DWORD   fdwClipPrecision,
DWORD   fdwQuality,
DWORD   fdwPitchAndFamily,
LPCTSTR lpszFace
);

Win32 Fonts APIs

A font is represented by a name and verious attributes and style paramerts. CreateFont() is the Win32 API function to create an FONT handle to the in Windows graphical Application. The handle to the font or HFONT can be passed to device context to using SelectObject() call. Drawing text and string output APIs uses these fonts to draw the bitmaps of the text figures. DeleteObject() call can be used once the use of this font is finished. This function deallocates the font object from memory and closes the handle.

  • CreateFontIndirect - Creates a logical font handle from LOGFONT structure
  • CreateFontIndirectEx - Creates a logical font handle from LOGFONT structure
  • EnumFontFamilies - Enumarate font families
  • EnumFontFamiliesEx - Enumarate font families with additional params
  • EnumFonts - Enumarate font families for old legacy windows
  • SelectObject - Select and associate font or other GDI object to device context
  • DeleteObject - Generic function to free font object of any windows object handle

Font Demo

We are creating an overlapped window and creating different text string display in WM_PAINT event.


// Global Variables:
HINSTANCE hInst;// current instance

// Foward declarations of functions included in this code module:
ATOM  MyRegisterClass(HINSTANCE hInstance);
BOOL  InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);


int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
   // TODO: Place code here.
  MSG msg;

  MyRegisterClass(hInstance);

  // Perform application initialization:
  if (!InitInstance (hInstance, nCmdShow)) 
  {
    return FALSE;
  }

  // Main message loop:
  while (GetMessage(&msg, NULL, 0, 0)) 
  {
      DispatchMessage(&msg);
  }

  return msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
  WNDCLASS wc; 
  memset(&wc, 0, sizeof(WNDCLASS));
  wc.style      = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc  = (WNDPROC)WndProc;
  wc.cbClsExtra    = 0;
  wc.cbWndExtra    = 0;
  wc.hInstance    = hInstance;
  wc.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
  wc.lpszMenuName  = NULL;
  wc.lpszClassName  = "WndFontDemo";
  return RegisterClass(&wc);
}


BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow("WndFontDemo", "Windows Font Demo", WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  int wmId, wmEvent;
  PAINTSTRUCT ps;
  HDC hdc;

  switch (message) 
  {
    case WM_COMMAND:
      wmId    = LOWORD(wParam); 
      wmEvent = HIWORD(wParam); 
      // Parse the menu selections:
      switch (wmId)
      {

        case IDM_EXIT:
           DestroyWindow(hWnd);
           break;
        default:
           return DefWindowProc(hWnd, message, wParam, lParam);
      }
      break;
    case WM_PAINT:
      RECT rect;
      HBRUSH hBrush;
      HFONT hFont;
      hdc = BeginPaint(hWnd, &ps);
      hFont = CreateFont(48,
                         0,
                         0,
                         0,
                         FW_DONTCARE,
                         FALSE,
                         TRUE,
                         FALSE,
                         DEFAULT_CHARSET,
                         OUT_OUTLINE_PRECIS,
                         CLIP_DEFAULT_PRECIS,
                         ANTIALIASED_QUALITY,
                         VARIABLE_PITCH,TEXT("Impact"));
      SelectObject(hdc, hFont);
      SetRect(&rect, 10,10,700,200);
      SetTextColor(hdc, RGB(255,0,0));
      DrawText(hdc, TEXT("Drawing Text with Impact"), -1,&rect, DT_NOCLIP);
            
      hFont = CreateFont(30,
                         10,
                         -300,
                         0,
                         FW_DONTCARE,
                         FALSE,
                         TRUE,
                         FALSE,
                         DEFAULT_CHARSET,
                         OUT_OUTLINE_PRECIS,
                         CLIP_DEFAULT_PRECIS,
                         ANTIALIASED_QUALITY,
                         VARIABLE_PITCH,TEXT("Times New Roman"));
      SelectObject(hdc,hFont);
      SetRect(&rect, 20, 100, 900, 800);
      SetTextColor(hdc, RGB(0,128,0));
      DrawText(hdc, TEXT("Drawing Text with Times New Roman"), -1,&rect, DT_NOCLIP);
            
                
      hFont = CreateFont(36,
                         10,
                         250,
                         0,
                         FW_DONTCARE,
                         FALSE,
                         TRUE,
                         FALSE,
                         DEFAULT_CHARSET,
                         OUT_OUTLINE_PRECIS,
                         CLIP_DEFAULT_PRECIS,
                         ANTIALIASED_QUALITY,
                         VARIABLE_PITCH,TEXT("Arial"));
      SelectObject(hdc,hFont);
      SetRect(&rect, 50, 410, 1400, 600);
      SetTextColor(hdc, RGB(0,0,255));
      DrawText(hdc, TEXT("Drawing Text with Arial"), -1,&rect, DT_NOCLIP);
      DeleteObject(hFont);    
        
      EndPaint(hWnd, &ps);
      break;
    case WM_DESTROY:
      PostQuitMessage(0);
      break;
    default:
      return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

Font Demo Output

CreateFont() demo application

About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...

#