방법: 설치된 글꼴 열거

InstalledFontCollection 클래스는 FontCollection 추상 기본 클래스에서 상속됩니다. InstalledFontCollection 개체를 사용하여 컴퓨터에 설치된 글꼴을 열거할 수 있습니다. InstalledFontCollection 개체의 Families 속성은 FontFamily 개체의 배열입니다.

예제

다음 예제에서는 컴퓨터에 설치된 모든 글꼴 패밀리의 이름을 나열합니다. 이 코드는 Families 속성에서 반환된 배열에서 각 FontFamily 개체의 Name 속성을 검색합니다. 패밀리 이름이 검색되면 쉼표로 구분된 목록을 형성하기 위해 연결됩니다. 그런 다음, Graphics 클래스의 DrawString 메서드는 사각형에 쉼표로 구분된 목록을 그립니다.

예제 코드를 실행하는 경우 출력은 다음 그림에 표시된 것과 유사합니다.

설치된 글꼴 패밀리를 보여 주는 스크린샷.

FontFamily fontFamily = new FontFamily("Arial");
Font font = new Font(
   fontFamily,
   8,
   FontStyle.Regular,
   GraphicsUnit.Point);
RectangleF rectF = new RectangleF(10, 10, 500, 500);
SolidBrush solidBrush = new SolidBrush(Color.Black);

string familyName;
string familyList = "";
FontFamily[] fontFamilies;

InstalledFontCollection installedFontCollection = new InstalledFontCollection();

// Get the array of FontFamily objects.
fontFamilies = installedFontCollection.Families;

// The loop below creates a large string that is a comma-separated
// list of all font family names.

int count = fontFamilies.Length;
for (int j = 0; j < count; ++j)
{
    familyName = fontFamilies[j].Name;
    familyList = familyList + familyName;
    familyList = familyList + ",  ";
}

// Draw the large string (list of all families) in a rectangle.
e.Graphics.DrawString(familyList, font, solidBrush, rectF);
Dim fontFamily As New FontFamily("Arial")
Dim font As New Font( _
   fontFamily, _
   8, _
   FontStyle.Regular, _
   GraphicsUnit.Point)
Dim rectF As New RectangleF(10, 10, 500, 500)
Dim solidBrush As New SolidBrush(Color.Black)

Dim familyName As String
Dim familyList As String = ""
Dim fontFamilies() As FontFamily

Dim installedFontCollection As New InstalledFontCollection()

' Get the array of FontFamily objects.
fontFamilies = installedFontCollection.Families

' The loop below creates a large string that is a comma-separated
' list of all font family names.
Dim count As Integer = fontFamilies.Length
Dim j As Integer

While j < count
    familyName = fontFamilies(j).Name
    familyList = familyList & familyName
    familyList = familyList & ",  "
    j += 1
End While

' Draw the large string (list of all families) in a rectangle.
e.Graphics.DrawString(familyList, font, solidBrush, rectF)

코드 컴파일

앞의 예제는 Windows forms에서 사용하도록 설계되었으며 PaintEventArgs의 매개 변수인 ePaintEventHandler가 필요합니다. 또한 System.Drawing.Text 네임스페이스를 가져와야 합니다.

참고 항목