CFont用法 CFont 使用详解

  CFont class encapsulates the functionalities needed to manipulate the Fonts in Windows programming. A font can be created in 4 ways with a CFont class using CreateFont, CreateFontIndirect, CreatePointFont, or CreatePointFontIndirect functions. This CFont Samples page tries to give a piece of sample code for all of the above functions.

CFont Sample for using CFont :: CreateFont
:
   The following CFont sample illustrates how to create a font using CreateFont function of the CFont class.
 

   CClientDC dc(this);
   CFont l_font;
   l_font.CreateFont(14, 0, 0, 0, FW_NORMAL,
   FALSE, FALSE, FALSE, 0, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,   DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN, "Times New Roman");

   CFont* l_old_font = dc.SelectObject(&l_font); dc.TextOut(50, 50, "Hello World");    
   dc.SelectObject(l_old_font);
   // Delete the font object. 
   l_font.DeleteObject(); 

 


   In the above CFont Sample, the CreateFont function uses all default parameters (either 0 or Default constants), except for the height parameter. If CreateFont is called as above, the MFC framework will select the best fit parameters by itself and create a font accordingly. 

CFont Sample for using CFont :: CreateFontIndirect: 
   This part of CFont sample illustrates the usage of CreateFontIndirect. 


   CClientDC dc(this);
   CFont l_font;
   LOGFONT lf;
   lf.lfHeight = 12;
   strcpy(lf.lfFaceName, "Arial"); // Need a face name "Arial". 
   l_font.CreateFontIndirect(&lf); 
   CFont* l_old_font = dc.SelectObject(&l_font); 
   dc.TextOut(50, 50, "Hello World"); 
   dc.SelectObject(l_old_font); 
   // Delete the font object.
   l_font.DeleteObject(); 


The LOGFONT is a structure with all members required for the Font object. 

CFont Sample for using CFont :: CreatePointFont:
 This part of the sample illustrates the use of CreatePointFont for creating a font.


   CClientDC dc(this); 
   CFont l_font; 
   l_font.CreatePointFont(140,"Times New Roman"); 
   CFont* l_old_font = dc.SelectObject(&l_font);
   dc.TextOut(50, 50, "Hello World"); 
   dc.SelectObject(l_old_font); 
   // Delete the font object. 
   l_font.DeleteObject(); 


The first parameter of the CreatePointFont function is the height of the Font in tenths of a point. The return value of this function is non-zero value if successful. 

CFont Sample for using CFont :: CreatePointFontIndirect:
 


   CClientDC dc(this); 
   CFont l_font; 
   LOGFONT lf;
   lf.lfHeight = 120; 
   strcpy(lf.lfFaceName, "Arial"); // Need a face name "Arial". 
   l_font.CreatePointFontIndirect(&lf); 
   CFont* l_old_font = dc.SelectObject(&l_font); 
   dc.TextOut(50, 60, "Hello World"); 
   dc.SelectObject(l_old_font); 
   //  Delete the font object. 
   l_font.DeleteObject(); 


   Usually it is better to use either CreatePointFont or CreatePointFontIndirect functions as they reduce the head-ache of thinking about the width, weight and such parameters.
   Also, in the above CFont sample the l_font is deleted in the end. It is a good programming practice as advised by Windows Programming manuals to delete the Objects after removing them from the current device context. 

 本文转自:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值