MFC printing operations

Source: Internet
Author: User
Although more and more people are turning to C # For interface development, MFC is still difficult for people who are used to the semi-object-oriented language C. Recently, I am doing some database-related things. Because it is a database, it is naturally inseparable from printing reports and other things. So we also studied how to use MFC to print related things.

Although more and more people are turning to C # For interface development, MFC is still difficult for people who are used to the semi-object-oriented language C. Recently, I am doing some database-related things. Because it is a database, it is naturally inseparable from printing reports and other things. So we also studied how to use MFC to print related things.

Although more and more people are turning to C # For interface development, for a person who is used to the semi-object-oriented language C ++, the existence of MFC is still difficult. Recently, I am doing some database-related things. Because it is a database, it is naturally inseparable from printing reports and other things. So I also studied how to use MFC to print related operations.

This article mainly references this article and the sample code of MSDN. In addition, this article also provides a lot of useful references. If you want to read the original article, you can check it out.

Directly Using the CPrintDialog class provided by Microsoft for printing is very simple. I encapsulate the code into two functions, in addition to defining the part that actually draws things on a page in the second function, other print-related operations are implemented on the first function. The details are as follows:

Print related operation main function:

Void CPrinttToPrinterDlg: OnBnClickedButtonPrint () {CPrintDialog dlg (FALSE, PD_ALLPAGES, NULL); // print the dialog box object definition. The parameter 1 pop-up dialog box is the Print dialog box, parameter 2 sets the print range to all pages. Parameter 3 specifies the printer. By default, you can set the initial value of the/* Print dialog box. You can set it as needed in actual applications, for example, obtain the page range to be printed by pre-calculation */dlg. m_pd.nMinPage = 1; // specify the minimum value of the page number range of the Start/end page number editing control. If nMinPage = nMaxPage, the "page number range" radio button and the start/end page number editing control are dimmed by dlg. m_pd.nMaxPage = 2; // specify the maximum page number range of the Start/end page number editing control dlg. m_pd.nCopies = 1; // specify the initial value of the copy count Edit Control dlg. m_pd.nFromPage = 1; // Specify the initial value of the page number editing control dlg. m_pd.nToPage = 2; // specify the initial value of the end page number editing control if (dlg. doModal () = IDOK) {HDC hdcPrinter = dlg. getPrinterDC (); if (hdcPrinter = NULL) // check the handle of the device environment. if no device environment is found, the system prompts and exits {MessageBox (_ T ("Buy a printer! ");} Else {/* Create a graphical device environment and associate it with the printer device. Drawing on the above is equivalent to drawing on the paper to be printed on the printer */CDC dcPrinter; dcPrinter. attach (hdcPrinter);/** some items we want users to select during the print setting process, so the dialog box is used, however, we want some things, such as the paper size and printing direction, to be fixed, so we do not need to make the settings difficult. * therefore, we use the existing printer graphics equipment environment, you can modify it to implement the fixed setting function */LPDEVMODE pDevMode; pDevMode = (LPDEVMODE) GlobalLock (dlg. m_pd.hDevMode); pDevMode-> dmOrientation = DMORIENT_LANDSCAPE; // set the printing direction to horizontal pDevMode-> dmPaperSize = DMPAPER_A4; // set the paper size to A4dcPrinter. resetDC (pDevMode); // notifies the printer driver to receive printed documents and start printing DOCINFO docinfo; memset (& docinfo, 0, sizeof (docinfo); docinfo. cbSize = sizeof (docinfo); docinfo. lpszDocName = _ T ("CDC: StartDoc () Code Fragment"); // if the initialization fails, the system prompts and exits if (dcPrinter. startDoc (& docinfo) <0) {MessageBox (_ T ("Printer wouldn't initalize");} We need to know the paper size printed by else, in addition, we also need to convert the pixel points of the device and the actual length unit, that is, DPI */int xDPI = GetDeviceCaps (hdcPrinter, LOGPIXELSX); // return the number of device points per inch in the X direction, that is, DPIfloat mmdpix = xDPI/25.4; // The number of device points per millimeter int yDPI = GetDeviceCaps (hdcPrinter, LOGPIXELSY); // The number of device points per inch in the Y direction are returned, that is, DPIfloat mmdpiy = yDPI/25.4; // The number of device points per millimeter float printerscreenx and printerscreeny; // the width and height of the printer screen are displayed, that is, the size of the paper, it will be about 6 smaller than the actual number of times, so here we will process the selected items */int pagebegin, pageend; // print the range if (dlg. printAll () // check whether you have selected print all. If yes, print all {pagebegin = dlg. m_pd.nMinPage; pageend = dlg. m_pd.nMaxPage;} else if (dlg. printRange () // The selected page number range item {pagebegin = dlg. m_pd.nFromPage; pageend = dlg. m_pd.nToPage;} else // The selected range is printed, because the selected range function is not provided here, therefore, once you select this option as Operation error handling, You can {MessageBox (_ T ("cocould not choise this one"); dcPrinter. abortDoc (); // error exit is different from EndDoc} int ncopy = dlg. m_pd.nCopies; // The user selects to print the score/* print the work here */while (ncopy --) // print each copy {for (int page = pagebegin; page <= pageend; page ++) // print {if (dcPrinter. startPage () <0) {MessageBox (_ T ("cocould not start page"); dcPrinter. abortDoc (); // error exit is different from EndDoc} else // If you enter here, print the content to be printed and print a page. {doThePrint (dcPrinter, page, mmdpix, mmdpiy, printerscreenx, printerscreeny); dcPrinter. endPage () ;}}} dcPrinter. endDoc (); // print out dcPrinter. detach (); // release DC }}}

The content actually printed on a page is defined in this function. The following function draws a green rectangle slightly smaller than the page size on the printed paper:
Void CPrinttToPrinterDlg: doThePrint (CDC & dc, int page, float mdpix, float mdpiy, float mpagex, float mpagey) // print the actual plotting function, draw {CPen pen, * pOldPen; // define the pen object and pointer above the CDC passed in to parameter 1 // create a green solid pen in 10 units of width. createPen (PS_SOLID, 100, RGB (0,255, 0); pOldPen = dc. selectObject (& pen); // select the green pen dc. re [from the Internet (http://www.68idc.cn)] ctangle (0 * mdpix, 0 * mdpiy, mpagex * mdpix, mpagey * mdpiy); // draw a rectangular dc. selectObject (pOldPen); // select the green pen. deleteObject (); // Delete the green pen}

This article has been written in. You can call the main function to print the animation on the printer. As for what to print and how to print it, it is about the GDI drawing. In addition, we have not implemented the print preview function, so we can use a virtual printer to see the effect. Of course, if we have money, we can also use a real printer to see it. Haha, I am using finepring, in short, there are two words to describe, an artifact!

Print preview and how to print data tables. I will post this function or send a link later if I have researched and implemented the function. This article will come here first.

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.