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

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

private System.Windows.Forms.Button cmdPrint;

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

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

// Tạo văn bản gồm 100 dòng.

string[] printText = new string[100];

for (int i=0; i < 100; i++) {

printText[i] = i.ToString();

printText[i] +=

": Thập Diện Mai Phục (House of Flying Daggers)";

}

PrintDocument doc = new TextDocument(printText);

doc.PrintPage += new PrintPageEventHandler(this.Doc_PrintPage);

PrintDialog dlgSettings = new PrintDialog();

dlgSettings.Document = doc;

// Nếu người dùng nhắp OK thì in văn bản.

if (dlgSettings.ShowDialog() == DialogResult.OK) {

doc.Print();

}

}

private void Doc_PrintPage(object sender, PrintPageEventArgs e) {

// Thu lấy văn bản đã gửi sự kiện này.

TextDocument doc = (TextDocument)sender;

// Định nghĩa font và xác định chiều cao.

Font font = new Font("Tahoma", 10);

float lineHeight = font.GetHeight(e.Graphics);