PrivateFontCollection::AddFontFile 方法 (gdiplusheaders.h)

PrivateFontCollection::AddFontFile方法會將字型檔案新增至這個私人字型集合。

語法

Status AddFontFile(
  [in] const WCHAR *filename
);

參數

[in] filename

類型: const WCHAR*

指定字型檔案名的寬字元字串指標。

傳回值

類型: 狀態

如果方法成功,它會傳回 Ok,這是 Status 列舉的元素。

如果方法失敗,它會傳回 Status 列舉的其中一個其他元素。

備註

當您使用 GDI+ API 時,絕對不能允許應用程式從不受信任的來源下載任意字型。 作業系統需要提高的許可權,以確保所有已安裝的字型都受到信任。

範例

下列範例會建立 PrivateFontCollection 物件,並將三個字型檔案新增至集合。 然後,程式碼會取得集合中的字型系列,而且針對集合中的每個系列,建立用來繪製文字的字型。

VOID Example_AddFontFile(HDC hdc)
{
   Graphics              graphics(hdc);
   SolidBrush            solidBrush(Color(255, 0, 0, 0));
   INT                   found = 0;
   INT                   count = 0;
   WCHAR                 familyName[50];
   FontFamily*           pFontFamily;
   PrivateFontCollection privateFontCollection;

   // Add three font files to the private collection.
   privateFontCollection.AddFontFile(L"C:\\WINNT\\Fonts\\Arial.ttf");
   privateFontCollection.AddFontFile(L"C:\\WINNT\\Fonts\\Cour.ttf");
   privateFontCollection.AddFontFile(L"C:\\WINNT\\Fonts\\Times.ttf");

   // How many font families are in the private collection?
   count = privateFontCollection.GetFamilyCount();

   // Allocate a buffer to hold the array of FontFamily objects returned by
   // the GetFamilies method.
   pFontFamily = (FontFamily*)malloc(count * sizeof(FontFamily));

   // Get the array of FontFamily objects.
   privateFontCollection.GetFamilies(count, pFontFamily, &found);

   for(INT j = 0; j < found; ++j)
   {
      // Get the font family name.
      pFontFamily[j].GetFamilyName(familyName);
   
      // Pass the family name and the address of the private collection to a
      // Font constructor.
      Font* pFont = new Font(familyName, 16, FontStyleRegular,
                             UnitPixel, &privateFontCollection);

      // Use the font to draw a string.
      graphics.DrawString(
                          L"Hello", 
                          5,          // string length 
                          pFont, 
                          PointF(10.0f, (REAL)j*25), 
                          &solidBrush);

      delete(pFont);
   }

   free(pFontFamily);
}

需求

   
最低支援的用戶端 Windows XP、Windows 2000 Professional [僅限傳統型應用程式]
最低支援的伺服器 Windows 2000 Server [僅限傳統型應用程式]
目標平台 Windows
標頭 gdiplusheaders.h (包含 Gdiplus.h)
程式庫 Gdiplus.lib
Dll Gdiplus.dll

另請參閱

建立私人字型集合

InstalledFontCollection

PrivateFontCollection

使用文字和字型