MFC Printing related Operations

Source: Internet
Author: User

Although more and more people have now turned to C # to do interface development, but for a person accustomed to the semi-object-oriented language of C + +, MFC is still a difficult existence. Recently in doing some database related things, because it is a database, naturally inseparable from printing reports and other things, so incidentally also to study how to use MFC to print related operations.

This article is more reference to this article and the MSDN sample code, in addition this article also provides a lot of useful reference , if you want to see the original words can go to see.

available directly from Microsoft The CPrintDialog class is very simple to print, and I encapsulate the code into two functions, except that the part that actually draws something on one page is defined in the second function, and other printing-related operations are implemented on the first function. Specific as follows :

Print related Operations main function:

void Cprintttoprinterdlg::onbnclickedbuttonprint () {CPrintDialog dlg (FALSE, Pd_allpages, NULL);//Print dialog box object definition, Parameter one set popup dialog box for the Print dialog, parameter two set the print range for all pages, parameter three can specify the printer, here the default can///print the initial value of the dialog box settings, in the actual application can be set according to the needs, such as to be printed by pre-calculation of the page range */dlg.m_ Pd.nminpage = 1;//Specifies the minimum value of the page range for the Start/End page number edit control, and if nminpage=nmaxpage, the page Range radio button and the Start/End page number edit controls are grayed dlg.m_pd.nMaxPage = 2;//Specify start/ End page Number The maximum value of the page range of the edit control Dlg.m_pd.nCopies = 1;//Specifies the copy number of copies of the edit control's initial value Dlg.m_pd.nFromPage = 1;//Specifies the start page number of the edit control's initial value Dlg.m_pd.nToPage = 2;// Specifies the end page number of the edit control's initial value if (dlg. DoModal () = = IDOK) {HDC hdcprinter = dlg. GETPRINTERDC (); if (hdcprinter = = NULL)//Check the handle of the device environment, retrieve the reminder and exit {MessageBox (_t ("Buy a printer!"));} else{/* Create a graphics device environment and associate it with a printer device, drawing on it is equivalent to drawing on the paper that the printer is going to */CDC Dcprinter;dcprinter.attach (hdcprinter);/* * There are some things we want the user to choose during the printing setup, so we use the form of the dialog, but some * things like paper size and direction of printing we want it to be fixed and not let the user arbitrarily set up the trouble * Therefore, we use the already obtained printer graphics device environment, By modifying it to implement the fixed setting function */lpdevmode Pdevmode;pdevmode = (lpdevmode) globallock (dlg.m_pd.hDevMode);pD evmode-> Dmorientation = dmorient_landscape;//Set print direction to landscape pdevmode->dmpapersize = dmpaper_a4;//set paper size to A4DCPRINTER.RESETDC (pDevMode);//Notifies the printer driver to receive the printed document and begins printing DocInfo docinfo;memset (&docinfo, 0, sizeof (docinfo));d ocinfo.cbsize = sizeof (docinfo);d ocinfo.lpszdocname = _t ("Cdc::startdoc () Code Fragment") and/or if initialization fails, alert and exit if (Dcprinter.startdoc ( &docinfo) < 0) {MessageBox (_t ("Printer wouldn ' T initalize"));} else{/* printed paper size We need to know, in addition we need the device pixel and the actual length of the unit conversion relationship is Dpi*/int xdpi = GetDeviceCaps (Hdcprinter, logpixelsx);// Returns the number of devices per inch in the x direction, i.e. dpifloat Mmdpix = xdpi/25.4;//The number of device points per millimeter int ydpi = GetDeviceCaps (Hdcprinter, logpixelsy);// Returns the number of devices per inch in the y direction, that is, dpifloat Mmdpiy = ydpi/25.4;//The number of devices per millimeter float Printerscreenx, printerscreeny;//here to get the width of the printer screen, That is, the size of the paper will be smaller than the actual 6mmprinterscreenx = GetDeviceCaps (Hdcprinter, horzsize);p Rinterscreeny = GetDeviceCaps (Hdcprinter, Vertsize);/* Because the page range and number of copies to be printed is the user's choice, the item selected by the user is processed */int pagebegin, pageend;//the print range if (dlg. Printall ())//Whether the user has chosen to print all of this item, then all print {Pagebegin = Dlg.m_pd.nminpage;pageend = Dlg.m_pd.nMaxPage;} else if (dlg. PrintRange ())//User selected page range item {pagebegin = Dlg.m_pd.nfrompage;pageend = Dlg.m_Pd.ntopage;} else//the user selects the selected range for printing, because we do not provide the function of the selected range here, so once the user chooses here as the operation error handling can {MessageBox (_t ("Could not choise this one")); Dcprinter.abortdoc ();//Error exit differs from Enddoc}int ncopy = dlg.m_pd.ncopies;//user chooses print score/* Print work here */while (ncopy--)//COLLATE {for (int page = pagebegin; page <= pageend; page++)//Start printing from the selected range {if (dcprinter.startpage () < 0) {MessageBox (_t ("Could not S Tart page "));d Cprinter.abortdoc ();//Error exit differs from enddoc}else//If you enter here, plot the print to print the contents and end the page (Dcprinter, page, Mmdpix, Mmdpiy, Printerscreenx, Printerscreeny);d cprinter.endpage ();}}} Dcprinter.enddoc ();//Print Complete exit Dcprinter.detach ();//Release DC}}}

<span style= "font-family:kaiti_gb2312;" >    the actual content printed on one page is defined in this function, and the following function content is implemented to draw a green rectangle that is slightly smaller than the page size on the printed paper:</span>
<span style= "font-family:kaiti_gb2312;" ></span><pre name= "code" class= "CPP" style= "font-family:kaiti_gb2312;" >void Cprintttoprinterdlg::d otheprint (CDC &dc,int page, float Mdpix, float mdpiy, float mpagex, float mpagey)//Print Real mapping function to the one passed to the CDC above the parameter
{CPen pen, *poldpen;//defines the Pen object and pointer  //creates a 10-wide green solid pen. CreatePen (Ps_solid, +, RGB (0, 255, 0));p oldpen = DC. SelectObject (&pen);//SELECT INTO a green pen  DC. Rectangle (0 * mdpix, 0 * mdpiy, Mpagex*mdpix, MPAGEY*MDPIY);//Draw a rectangular DC. SelectObject (Poldpen);//Select Green pen pen  . DeleteObject ();//delete Green pen}
<span style= "font-family:kaiti_gb2312;" >    to this article has been written to 7788, call the main function can be used to print to the printer to move things on the function. As for what to print, how to hit it is the GDI drawing thing. In addition, because we have not realized the function of print preview, so you can use the virtual printer to see the effect, of course, if the money, you can also directly use the real printer to see Haha, I use is finepring, in short, two words describe, artifact! </span>

<span style= "font-family:kaiti_gb2312;" >    Print Preview and how to print the data table These functions I will post if the research has been implemented, or send a link up, this article first come here. </span>

MFC Printing related Operations

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.