当我点击button1时,它会打开一个打印对话框但是当我关闭它时会打开另一个对话框。任何想法(根据我的代码)为什么?我确实检查了我的代码,但我没有看到任何冗余。谢谢另外,伙计们,在InitializeComponent
之后+ =表示什么public partial class Print2 : Form
{
public Print2()
{
InitializeComponent();
this.button1.Click += button1_Click;
this.printDocument1.PrintPage += printDocument1_PrintPage;
}
private void Print2_Load(object sender, EventArgs e)
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
using (SqlConnection connection = new SqlConnection("Data Source=XXXX;Initial Catalog=XXXXX;Integrated Security=True"))
{
SqlCommand command =
new SqlCommand("select [CustomerName],[Inserted] from baf where id=(select max(id) from baf)", connection);
connection.Open();
SqlDataReader read = command.ExecuteReader();
while (read.Read())
{
textBox1.Text = (read["CustomerName"].ToString());
textBox2.Text = (read["Inserted"].ToString());
}
read.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
this.printPreviewDialog1.Document = this.printDocument1;
this.printPreviewDialog1.Document.DocumentName = "PictureTextDesignerFile1";
this.printPreviewDialog1.Size = new Size(1200, 800);
this.printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
float neededWidth = 840;
float neededHeight = 1320;
float availableWidth = 840;
float availableHeight = 1320;
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
Bitmap bmpPrint = null;
try
{
double multiplier = neededWidth / neededHeight;
double dblRef = availableWidth / availableHeight;
float zoom = 1;
if (multiplier >= dblRef)
{
zoom = availableWidth / neededWidth;
}
else
{
zoom = availableHeight / neededHeight;
}
bmpPrint = new Bitmap(1200, 900);
bmpPrint.SetResolution(300, 300);
this.DrawToBitmap(bmpPrint, new Rectangle(0, 0, 2000, 2800));
//this.DrawToBitmap(bmpPrint, new Rectangle(0, 0, bmpPrint.Width, bmpPrint.Height));
e.Graphics.Clip = new Region(e.MarginBounds);
e.Graphics.DrawImage(bmpPrint, e.MarginBounds.Left, e.MarginBounds.Top, Math.Min(neededWidth * zoom, availableWidth), Math.Min(neededHeight * zoom, availableHeight));
e.Graphics.Clip.Dispose();
}
catch
{
}
finally
{
if ((bmpPrint != null))
{
bmpPrint.Dispose();
bmpPrint = null;
}
}
}