CÁC GIẢI PHÁP LẬP TRÌNH C# - Trang 290

290
Chương 8: Đồ họa, đa phương tiện, và in ấn

// (Bỏ qua phần mã designer.)

private void ListFonts_Load(object sender, System.EventArgs e) {

// Tạo tập hợp font.

InstalledFontCollection fontFamilies =

new InstalledFontCollection();

// Duyệt qua tất cả các font.

int offset = 10;

foreach (FontFamily family in fontFamilies.Families) {

try {

// Tạo một Label để hiển thị text (viết ở font này).

Label fontLabel = new Label();

fontLabel.Text = family.Name;

fontLabel.Font = new Font(family, 14);

fontLabel.Left = 10;

fontLabel.Width = pnlFonts.Width;

fontLabel.Top = offset;

// Thêm Label vào Panel cuộn được.

pnlFonts.Controls.Add(fontLabel);

offset += 30;

}catch {

// Lỗi sẽ xảy ra nếu font được chọn không

// hỗ trợ normal style (mặc định được sử dụng khi

// tạo đối tượng Font). Vấn đề này có thể

// được bỏ qua mà không sao cả.

}

}

}

}