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

329

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

// Cho phép người dùng chọn một máy in

// và chỉ định các thiết lập khác.

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.

ParagraphDocument doc = (ParagraphDocument)sender;

// Định nghĩa font và text.

Font font = new Font("Arial", 15);

e.Graphics.DrawString(doc.Text, font, Brushes.Black,

e.MarginBounds, StringFormat.GenericDefault);

}

}

public class ParagraphDocument : PrintDocument {

private string text;

public string Text {

get {return text;}

set {text = value;}

}

public ParagraphDocument(string text) {

this.Text = text;

}

}