Visual C + + 6.0 programming to implement printing capabilities

Source: Internet
Author: User
Tags ini

Visual c++6.0 is a powerful tool for developing Windows applications, but it is always a difficult task for beginners to realize the printing function of a program, often friends ask how to implement printing in VC, they often feel that it is very complicated to implement this problem within the framework provided by MFC, Don't know how to do it. This example aims at this problem, introduces a simple method to realize the printing function of the text string, the reader friend can make a little change on this basis, can realize the printing function of file and image.

One, the realization method

Under Windows operating systems, displays, printers, and plotters are considered output devices, and normally the system's default output device is the monitor. To use a printer, you first need to create a device environment handle that points to the printer, and then call the relevant drawing function through the handle to output the text and graphics you want to the printer. When the print is finished, remove the device environment handle.

When a printer is installed in a Windows system, the system always automatically sets a printer as the default printer for the system, and the default printer with the keyword device is listed in the [Window] section in the Windows Startup profile Win.ini. The following is the contents of the [Windows] field in Win.ini in a machine:

[windows]
load=
run=
NullPort=None
device=HP LaserJet 4050 (computer000),HPBFDB1,LPT1

In the string after the above keyword device, contains three important properties of the default printer in the system, which is the device name HP LaserJet 4050 (computer000) of the printer, the driver name is HPBFDB1 and the output port is LPT1.

In order to manipulate the system default printer, realize the program printing function, in the program can invoke API function getprofilestring () obtains device this device string from the Win.ini file, the function's prototype is: DWORD getprofilestring ( LPCTSTR Lpappname, LPCTSTR lpkeyname, LPCTSTR lpdefault, LPTSTR lpreturnedstring, DWORD nsize). The Lpappname parameter in the function is the field name in the Win.ini file to retrieve; Lpkeyname is the keyword name in the field; Lpdefault is the default string; Lpreturnedstring is the retrieved string, if the function does not Has the corresponding string retrieved from the Lpkeyname keyword, the kpretrunedstring returns the default string lpdefault;nsize the length of the return string.

After getting the above string, use the Strtok () function to decompose the string to obtain three properties related to the printer, as an API function CreateDC () to create parameters for the printer device environment handle, CREATEDC () function if the call succeeds, A device environment handle is created for the default printer, otherwise a null value (NULL) is returned. The prototype of the function is: HDC CreateDC (LPCTSTR lpszdriver,lpctstr lpszdevice,lpctstr lpszoutput,const). The first three parameters of the function correspond exactly to the three properties of the printer, and the last parameter is the data that initializes the printer driver, which is normally set to null.

During the specific printing process, the int startdoc (HDC HDC, CONST docinfo *lpdi) function is invoked to start a print task, where the parameter lpdi is a pointer to the DOCINFO structure as follows:

typedef struct {
 int cbSize; //结构的尺寸大小;
 LPCTSTR lpszDocName; //文档的名字;
 LPCTSTR lpszOutput; //输出文档名, 一般情况下为NULL;
 LPCTSTR lpszDatatype;//用来记录打印过程的数据类型,一般 情况下为NULL;
 DWORD fwType; //用来支持打印工作的额外信息,一般情况下为 NULL;
} DOCINFO, *LPDOCINFO;

Start a print task, then call the StartPage (hdcprint) function to let the printer walk paper, notify the printer that the document will be printed, the next job is to output data, this part of the work for developers is like to the computer screen output text, image as easy, but is The computer automatically outputs the data to the printer according to the current device environment handle. After the data is printed, some remedial work needs to be done, using the RESTOREDC (hdcprint,-1) function to restore the printer device handle, the EndPage (hdcprint) function to let the printer stop printing, and finally call EndDoc (Hdcprint) function to end the print job above.

Related Article

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.