当前位置: 首页>>代码示例>>C#>>正文


C# PrintDialog类代码示例

本文整理汇总了C#中System.Windows.Forms.PrintDialog的典型用法代码示例。如果您正苦于以下问题:C# PrintDialog类的具体用法?C# PrintDialog怎么用?C# PrintDialog使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


PrintDialog类属于System.Windows.Forms命名空间,在下文中一共展示了PrintDialog类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: new PrintDialog()

/*
Code revised from chapter 7


GDI+ Custom Controls with Visual C# 2005
By Iulian Serban, Dragos Brezoi, Tiberiu Radu, Adam Ward 

Language English
Paperback 272 pages [191mm x 235mm]
Release date July 2006
ISBN 1904811604

Sample chapter
http://www.packtpub.com/files/1604_CustomControls_SampleChapter.pdf



For More info on GDI+ Custom Control with Microsoft Visual C# book 
visit website www.packtpub.com 

*/ 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;

namespace PrintingCustomControlApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            printableRichTextBox1.Print(false);
        }

        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.printableRichTextBox1 = new PrintingCustomControlApp1.PrintableRichTextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // printableRichTextBox1
            // 
            this.printableRichTextBox1.Location = new System.Drawing.Point(77, 25);
            this.printableRichTextBox1.Name = "printableRichTextBox1";
            this.printableRichTextBox1.Size = new System.Drawing.Size(124, 117);
            this.printableRichTextBox1.TabIndex = 0;
            this.printableRichTextBox1.Text = "";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(63, 191);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(126, 30);
            this.button1.TabIndex = 1;
            this.button1.Text = "Print the Control text";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.printableRichTextBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private PrintableRichTextBox printableRichTextBox1;
        private System.Windows.Forms.Button button1;

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
    public partial class PrintableRichTextBox : RichTextBox
    {
        public PrintableRichTextBox()
        {
            InitializeComponent();
            _prtdoc = new PrintDocument();
            _prtdoc.PrintPage += new PrintPageEventHandler(_prtdoc_PrintPage);

        }
        string _text = null;
        int _pageNumber = 0;
        int _start = 0;
        PrintDocument _prtdoc = null;

        private bool DrawText(Graphics target, Graphics measurer, RectangleF r, Brush brsh)
        {
            if (r.Height < this.Font.Height)
                throw new ArgumentException("The rectangle is not tall enough to fit a single line of text inside.");
            int charsFit = 0;
            int linesFit = 0;
            int cut = 0;
            string temp = _text.Substring(_start);
            StringFormat format = new StringFormat(StringFormatFlags.FitBlackBox | StringFormatFlags.LineLimit);
            //measure how much of the string we can fit into the rectangle
            measurer.MeasureString(temp, this.Font, r.Size, format, out charsFit, out linesFit);
            cut = BreakText(temp, charsFit);
            if (cut != charsFit)
                temp = temp.Substring(0, cut);
            bool h = true;
            h &= true;
            target.DrawString(temp.Trim(' '), this.Font, brsh, r, format);
            _start += cut;
            if (_start == _text.Length)
            {
                _start = 0; //reset the location so we can repeat the document
                return true; //finished printing
            }
            else
                return false;
        }
        private static int BreakText(string text, int approx)
        {
            if (approx == 0)
                throw new ArgumentException();
            if (approx < text.Length)
            {
                //are we in the middle of a word?
                if (char.IsLetterOrDigit(text[approx]) && char.IsLetterOrDigit(text[approx - 1]))
                {
                    int temp = text.LastIndexOf(' ', approx, approx + 1);
                    if (temp >= 0)
                        return temp;
                }
            }
            return approx;
        }
        public void Print(bool hardcopy)
        {
            _text = this.Text;
            //create a PrintDialog based on the PrintDocument
            PrintDialog pdlg = new PrintDialog();
            pdlg.Document = _prtdoc;
            //show the PrintDialog
            if (pdlg.ShowDialog() == DialogResult.OK)
            {
                //create a PageSetupDialog based on the PrintDocument and PrintDialog
                PageSetupDialog psd = new PageSetupDialog();
                psd.EnableMetric = true; //Ensure all dialog measurements are in metric
                psd.Document = pdlg.Document;
                //show the PageSetupDialog
                if (psd.ShowDialog() == DialogResult.OK)
                {
                    //apply the settings of both dialogs
                    _prtdoc.DefaultPageSettings = psd.PageSettings;
                    //decide what action to take
                    if (hardcopy)
                    {
                        //actually print hardcopy
                        _prtdoc.Print();
                    }
                    else
                    {
                        //preview onscreen instead
                        PrintPreviewDialog prvw = new PrintPreviewDialog();
                        prvw.Document = _prtdoc;
                        prvw.ShowDialog();
                    }
                }
            }
        }
        private void _prtdoc_PrintPage(object sender, PrintPageEventArgs e)
        {
            e.Graphics.Clip = new Region(e.MarginBounds);

            //this method does all our printing work
            Single x = e.MarginBounds.Left;
            Single y = e.MarginBounds.Top;

            if (_pageNumber++ == 0)
                y += 30;

            RectangleF mainTextArea = RectangleF.FromLTRB(x, y, e.MarginBounds.Right, e.MarginBounds.Bottom);

            //draw the text
            if (DrawText(e.Graphics, e.PageSettings.PrinterSettings.CreateMeasurementGraphics(), mainTextArea, Brushes.Black))
            {
                e.HasMorePages = false; //the end has been reached
                _pageNumber = 0;
            }
            else
                e.HasMorePages = true;
        }
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            //this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        }

        #endregion
    }

    
}
开发者ID:C#程序员,项目名称:System.Windows.Forms,代码行数:258,代码来源:PrintDialog

示例2: Button1_Click

// Declare the PrintDocument object.
private System.Drawing.Printing.PrintDocument docToPrint = 
    new System.Drawing.Printing.PrintDocument();

// This method will set properties on the PrintDialog object and
// then display the dialog.
private void Button1_Click(System.Object sender, 
    System.EventArgs e)
{

    // Allow the user to choose the page range he or she would
    // like to print.
    PrintDialog1.AllowSomePages = true;

    // Show the help button.
    PrintDialog1.ShowHelp = true;

    // Set the Document property to the PrintDocument for 
    // which the PrintPage Event has been handled. To display the
    // dialog, either this property or the PrinterSettings property 
    // must be set 
    PrintDialog1.Document = docToPrint;

    DialogResult result = PrintDialog1.ShowDialog();

    // If the result is OK then print the document.
    if (result==DialogResult.OK)
    {
        docToPrint.Print();
    }
}

// The PrintDialog will print the document
// by handling the document's PrintPage event.
private void document_PrintPage(object sender, 
    System.Drawing.Printing.PrintPageEventArgs e)
{

    // Insert code to render the page here.
    // This code will be called when the control is drawn.

    // The following code will render a simple
    // message on the printed document.
    string text = "In document_PrintPage method.";
    System.Drawing.Font printFont = new System.Drawing.Font
        ("Arial", 35, System.Drawing.FontStyle.Regular);

    // Draw the content.
    e.Graphics.DrawString(text, printFont, 
        System.Drawing.Brushes.Black, 10, 10);
}
开发者ID:.NET开发者,项目名称:System.Windows.Forms,代码行数:51,代码来源:PrintDialog


注:本文中的System.Windows.Forms.PrintDialog类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。