我从未使用C#打印过任何内容。我只是想知道标准的方法是什么。在我的表单中,我有一些列表框和一些文本框。我想打印它们的内容并在打印预览中显示它们,以表格的形式呈现良好的布局。然后我希望用户能够打印。
提前感谢!
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Font font = new Font("Arial", 10f);
Graphics g = e.Graphics;
Pen rectPen = new Pen(Color.Black, 2f);
Brush brush = Brushes.Black;
// find widest width of items
for (int i=0; i<listBox1.Items.Count; i++)
if(maxItemWidth < (int)g.MeasureString(listBox1.Items[i].ToString(), font).Width)
maxItemWidth = (int)g.MeasureString(listBox1.Items[i].ToString(), font).Width;
// starting positions:
int itemHeight = (int)g.MeasureString("TEST", font).Height + 5;
int maxItemWidth = 0;
int xpos = 200;
int ypos = 200;
// print
for (int i = 0; i < listBox1.Items.Count; i++)
{
g.DrawRectangle(rectPen, xpos, ypos, maxItemWidth, itemHeight );
g.DrawString(listBox1.Items[i].ToString(), font, brush, xpos, ypos);
ypos += itemHeight;
}
e.HasMorePages = false;
}
Pen rectPen
应该显式地被处理掉,还是由 GC 处理? - user153923