Share via


PrivateFontCollection::AddFontFile メソッド (gdiplusheaders.h)

PrivateFontCollection::AddFontFile メソッドは、このプライベート フォント コレクションにフォント ファイルを追加します。

構文

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

パラメーター

[in] filename

型: const WCHAR*

フォント ファイルの名前を指定するワイド文字列へのポインター。

戻り値

種類: 状態

メソッドが成功した場合は、Status 列挙の要素である Ok を返します

メソッドが失敗した場合は、 Status 列挙体の他の要素のいずれかを返します。

解説

GDI+ API を使用する場合、アプリケーションで信頼されていないソースから任意のフォントをダウンロードできないようにする必要があります。 オペレーティング システムでは、インストールされているすべてのフォントが信頼されるように、管理者特権が必要です。

次の例では、 PrivateFontCollection オブジェクトを作成し、コレクションに 3 つのフォント ファイルを追加します。 次に、コレクション内のフォント ファミリを取得し、コレクション内の各ファミリに対して、テキストの描画に使用されるフォントを作成します。

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 を含む)
Library Gdiplus.lib
[DLL] Gdiplus.dll

関連項目

プライベート フォント コレクションの作成

InstalledFontCollection

PrivateFontCollection

テキストとフォントの使用