home *** CD-ROM | disk | FTP | other *** search
/ Delphi Anthology / aDELPHI.iso / ActiveX / 3DText_ActiveX_v3.1-FCN / data1.cab / Example_Files / 3DText / Vc / NFont.cpp < prev    next >
C/C++ Source or Header  |  1999-10-27  |  3KB  |  100 lines

  1. #include "stdafx.h"
  2.  
  3. #include <Afxctl.h>
  4. #include <Atlbase.h>
  5.  
  6. #include "NFont.h"
  7.  
  8. //////////////////////////////////////////////////////////////////////
  9. // class CNFont
  10. CNFont::CNFont(LPPROPERTYNOTIFYSINK pNotifySink /*= NULL*/)
  11. :CFontHolder(pNotifySink)
  12. {
  13.     // create a new default IFont
  14.     InitializeFont(NULL, NULL);
  15. }
  16.  
  17. //////////////////////////////////////////////////////////////////////
  18. CNFont::~CNFont()
  19. {
  20. }
  21.  
  22. //////////////////////////////////////////////////////////////////////
  23. BOOL CNFont::SetDispatch(LPDISPATCH pDispatch)
  24. {
  25.     InitializeFont(NULL, pDispatch);
  26.     return TRUE;
  27. }
  28.  
  29. //////////////////////////////////////////////////////////////////////
  30. BOOL CNFont::GetLogFont(LOGFONT& LogFont)
  31. {
  32.     CComPtr<IFontDisp>    pFontDisp;
  33.  
  34.     ZeroMemory(&LogFont, sizeof(LOGFONT));
  35.     
  36.     pFontDisp = GetFontDispatch();
  37.     if (pFontDisp == NULL)
  38.         return FALSE;
  39.  
  40.     CComQIPtr<IFont> pIFont(pFontDisp);
  41.     if (pIFont == NULL)
  42.         return FALSE;
  43.  
  44.     HFONT hFont;
  45.     if (FAILED(pIFont->get_hFont(&hFont)))
  46.         return FALSE;
  47.  
  48.     VERIFY(GetObject(hFont, sizeof(LogFont), (void*)&LogFont));
  49.     return TRUE;
  50. }
  51.  
  52. //////////////////////////////////////////////////////////////////////
  53. BOOL CNFont::SetLogFont(const LOGFONT& LogFont)
  54. {
  55.     FONTDESC FontDesc;
  56.     ZeroMemory(&FontDesc, sizeof(FONTDESC));
  57.  
  58.     FontDesc.cbSizeofstruct = sizeof(FONTDESC);
  59.     FontDesc.lpstrName        = (CString(LogFont.lfFaceName)).AllocSysString();
  60.     FontDesc.cySize.Lo        = (long)abs(int(LogFont.lfHeight * 3 / 4.0)) * 10000; 
  61.     FontDesc.cySize.Hi        = 0;
  62.     FontDesc.sWeight        = (short)LogFont.lfWeight;
  63.     FontDesc.sCharset        = LogFont.lfCharSet;
  64.     FontDesc.fItalic        = LogFont.lfItalic;
  65.     FontDesc.fUnderline        = LogFont.lfUnderline;
  66.     FontDesc.fStrikethrough    = LogFont.lfStrikeOut;
  67.  
  68.     InitializeFont(&FontDesc);
  69.  
  70.     return TRUE;
  71. }
  72.  
  73. //////////////////////////////////////////////////////////////////////
  74. void CNFont::GetTextMetrics(LPTEXTMETRIC pTextMetrics)
  75. {
  76.     CDC dc;
  77.     LOGFONT logFont;
  78.     CFont    font, *pOldFont;
  79.  
  80.     VERIFY(dc.CreateCompatibleDC(NULL));
  81.     VERIFY(GetLogFont(logFont));    // Forgot to initialize the object ?
  82.     VERIFY(font.CreateFontIndirect(&logFont));
  83.  
  84.     pOldFont = (CFont*)dc.SelectObject(&font);
  85.     dc.GetTextMetrics(pTextMetrics);
  86.     dc.SelectObject(pOldFont);
  87. }
  88.  
  89. //////////////////////////////////////////////////////////////////////
  90. BOOL CNFont::IsTrueType()
  91. {
  92.     TEXTMETRIC tm;
  93.  
  94.     GetTextMetrics(&tm);
  95.  
  96.     if (tm.tmPitchAndFamily & TMPF_TRUETYPE)
  97.         return TRUE;
  98.  
  99.     return FALSE;
  100. }