C # Set horizontal printing when printing and previewing in winform

Source: Internet
Author: User
For project purposes, when printing a screen, because the datagridview table in the screen is very long and needs to be horizontally arranged for complete printing, set printdocument. printersettings. defaultpagesettings. Landscape = true. However, it is still vertically arranged during preview. Headache...
It takes some time for other settings to be effective. Use reflector to view the printloop method of printcontroller. The order of the printing-related methods of printdocument is as follows:
Querypagesettings
Startpage
Printpage
Endpage
Currently, only startpage and printpage are processed.
Therefore, when the querypagesettings event is added, it is found that the querypagesettingseventargs. pagesettings. landscape is still false each time the print is executed, although defapagpagesettings. Landscape = true is set previously.
Therefore, this event is set to true. When you preview it again, you can finally see the neatly arranged reports.

Private Void Button2_click ( Object Sender, eventargs E) { Printpreviewdialog Pd = New Printpreviewdialog (); PD. Document = New Printdocument (); PD. Document. printpage + = New Printpageeventhandler (document_printpage ); PD. Document. querypagesettings + = New Querypagesettingseventhandler (document_querypagesetler ); PD. Document. beginprint + = New Printeventhandler (document_beginprint ); If (PD. showdialog ( This ) = Dialogresult. OK) { } }   Void Document_querypagesettings ( Object Sender, querypagesettingseventargs E) { E. pagesettings. Landscape = True ; Int Index =-1; For ( Int I = 0; I <E. pagesettings. printersettings. papersizes. Count; I ++) { If (E. pagesettings. printersettings. papersizes [I]. papername = "A4" ) { Index = I; Break ; } } If (Index! =-1) { E. pagesettings. papersize = E. pagesettings. printersettings. papersizes [Index]; } }   Void Document_beginprint ( Object Sender, printeventargs E) { }   Void Document_printpage ( Object Sender, printpageeventargs E) { Using (Bitmap bit = New Bitmap ( This . ListBox. Width, This . ListBox. Height )) { This . ListBox. drawtobitmap (bit, This . ListBox. clientrectangle ); E. Graphics. drawimage (bit, New Point (0, 0 )); } }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.