using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form
{
public static void Main()
{
Application.Run(new Form1());
}
private System.Drawing.Printing.PrintDocument printdoc;
public Form1()
{
Button button1=new Button();
Button button2=new Button();
button1.Location = new System.Drawing.Point(0, 0);
button2.Location = new System.Drawing.Point(0, 100);
this.Controls.Add(button1);
this.Controls.Add(button2);
button1.Name="button1";
button1.Text = "button1";
button1.UseVisualStyleBackColor = true;
button1.Size = new System.Drawing.Size(200, 50);
button2.Name="button2";
button2.Text = "button2";
button2.UseVisualStyleBackColor = true;
button2.Size = new System.Drawing.Size(200, 50);
button1.Click+=new EventHandler(this.button1_Click);
button2.Click+=new EventHandler(this.button2_Click);
}
private void button2_Click(object sender, EventArgs e)
{
this.printdoc = new System.Drawing.Printing.PrintDocument();
//PrintPage事件处理器追加
printdoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printdoc_PrintPage);
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
printPreviewDialog1.Document = printdoc;
printPreviewDialog1.FormBorderStyle = FormBorderStyle.Fixed3D;
printPreviewDialog1.PrintPreviewControl.AutoZoom = false;
printPreviewDialog1.PrintPreviewControl.Zoom = 1;
printPreviewDialog1.ShowIcon = false;
printPreviewDialog1.ShowDialog();
}
private void printdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
StringFormat mStringFormat = new StringFormat();
mStringFormat.Alignment = StringAlignment.Near;
mStringFormat.LineAlignment = StringAlignment.Center;
Font printFont = new Font("宋体", 11, FontStyle.Bold);//加上粗体可以正常显示
SolidBrush printFontColor = new SolidBrush(System.Drawing.Color.Blue);//字体颜色
//e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;//加这句也是不行的....
e.Graphics.DrawString("测试测试", printFont, printFontColor, new RectangleF(20, 0, 200, 30), mStringFormat);
e.Graphics.DrawString("测试测试", printFont, printFontColor, new RectangleF(20, 40, 200, 30), mStringFormat);
e.Graphics.DrawString("测试测试", printFont, printFontColor, new RectangleF(20, 80, 200, 30), mStringFormat);
e.Graphics.DrawString("测试测试", printFont, printFontColor, new RectangleF(20, 120, 200, 30), mStringFormat);
e.Graphics.DrawString("测试测试", printFont, printFontColor, new RectangleF(20, 140, 200, 30), mStringFormat);
e.Graphics.DrawString("测试测试", printFont, printFontColor, new RectangleF(120, 140, 200, 30), mStringFormat);
e.Graphics.DrawString("测试测试", printFont, printFontColor, new RectangleF(120, 140, 200, 30), mStringFormat);
e.Graphics.DrawString("测试测试", printFont, printFontColor, new RectangleF(120, 140, 200, 30), mStringFormat);
}
private void button1_Click(object sender, EventArgs e)
{
this.printdoc = new System.Drawing.Printing.PrintDocument();
//PrintPage事件处理器追加
printdoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printdoc_PrintPage);
printdoc.Print();
}
}