Prompt PrintDialog : PrintDialog « GUI Windows Forms « C# / CSharp Tutorial






Prompt PrintDialog
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;

public class PrintDialogPrint : Form
{
    public PrintDialogPrint()
    {
        this.cmdPrint = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        this.cmdPrint.Location = new System.Drawing.Point(109, 122);
        this.cmdPrint.Size = new System.Drawing.Size(75, 23);
        this.cmdPrint.Text = "Print";
        this.cmdPrint.UseVisualStyleBackColor = true;
        this.cmdPrint.Click += new System.EventHandler(this.cmdPrint_Click);
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(272, 260);
        this.Controls.Add(this.cmdPrint);
        this.Text = "Simple Print";
        this.ResumeLayout(false);
    }

    private void cmdPrint_Click(object sender, EventArgs e)
    {
        PrintDocument doc = new PrintDocument();
        doc.PrintPage += this.Doc_PrintPage;

        PrintDialog dlgSettings = new PrintDialog();
        dlgSettings.Document = doc;

        if (dlgSettings.ShowDialog() == DialogResult.OK)
        {
            doc.Print();
        }
    }

    private void Doc_PrintPage(object sender, PrintPageEventArgs e)
    {
        Font font = new Font("Arial", 30);
        
        float x = e.MarginBounds.Left;
        float y = e.MarginBounds.Top;

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

        for (int i = 0; i < 5; i++)
        {
            e.Graphics.DrawString("This is line " + i.ToString(),font, Brushes.Black, x, y);
            y += lineHeight;
        }
        y += lineHeight;

        e.Graphics.DrawImage(Image.FromFile("c:\\YourFile.bmp"), x, y);
        
    }
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new PrintDialogPrint());
    }
    private System.Windows.Forms.Button cmdPrint;
    
}








23.49.PrintDialog
23.49.1.Display PrintDialogDisplay PrintDialog
23.49.2.Prompt PrintDialogPrompt PrintDialog