EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

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

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sat 26 Apr/2014 12:41:40(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.