Control design:
Set the document property of the above button and the remaining controls of the form control (PRINTDIALOG1, etc.) to the corresponding PrintDocument.
Code:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Threading.Tasks;9 usingSystem.Windows.Forms;Ten One namespaceYundayin A { - Public Partial classForm1:form - { the PublicForm1 () - { - InitializeComponent (); - + This. Printdocument1.originatmargins =true;//Enable page margins - This. Pagesetupdialog1.enablemetric =true;//in millimeters + } A at /// <summary> - ///Print - /// </summary> - Private voidButton1_Click (Objectsender, EventArgs e) - { - if( This. Printdialog1.showdialog () = =DialogResult.OK) in { - This. Printdocument1.print (); to } + } - the /// <summary> * ///Print Settings $ /// </summary>Panax Notoginseng Private voidButton2_Click (Objectsender, EventArgs e) - { the This. Pagesetupdialog1.showdialog (); + } A the /// <summary> + ///Print Preview - /// </summary> $ Private voidButton3_Click (Objectsender, EventArgs e) $ { - This. Printpreviewdialog1.showdialog (); - } the - Private voidPrintdocument1_printpage (Objectsender, System.Drawing.Printing.PrintPageEventArgs e)Wuyi { the ////----------------The entire form is printed---------------- - //Image myformimage; Wu //myformimage = new Bitmap (this. Width, this. Height); - //Graphics g = graphics.fromimage (myformimage); About //G.copyfromscreen (this. Location.x, this. LOCATION.Y, 0, 0, this. Size); $ //e.graphics.drawimage (myformimage, 0, 0); - - ////----------------Print content as a local this.groupbox1---- - //Bitmap _newbitmap = new Bitmap (groupbox1.width, groupbox1.height); A //Groupbox1.drawtobitmap (_newbitmap, New Rectangle (0, 0, _newbitmap.width, _newbitmap.height)); + //e.graphics.drawimage (_newbitmap, 0, 0, _newbitmap.width, _newbitmap.height); the - //------------------Print content as custom text content------------ $Font font =NewFont ("Song Body", A); theBrush Bru =Brushes.blue; the //for (int i = 1; I <= 5; i++) the //{ the - //} inE.graphics.drawstring ("Hello World", Font, BRU,Ten,Ten); the } the } About}
Complete.
c#-winform-Print Controls