Visual c ++ 6.0 programming for Printing

Source: Internet
Author: User
Tags strtok
Visual c ++ 6.0 Programming to Implement the printing function-general Linux technology-Linux programming and kernel information. The following is a detailed description. Visual C ++ 6.0 is a powerful tool for developing Windows applications. However, it has always been a challenge for beginners to use it to print programs, some people often ask how to implement the printing function in VC. They often feel that it is complicated to implement this function within the framework provided by MFC and do not know how to start. In this example, a simple method is introduced to print text strings. Readers can modify the text strings to print files and images.

I. Implementation Method

In Windows, the display, printer, and plotter are regarded as output devices. Normally, the default output device is a display. To use a printer, you must first create a device environment handle pointing to the printer, and then use the handle to call the relevant drawing function to output the required text and graphics to the printer. After printing, delete the device environment handle.

After a printer is installed in Windows, the system automatically sets a printer as the default printer. the [window] section in ini lists the default printers with the device keyword. The content of the [Windows] field in Win. ini of a machine is as follows:

Load =
Run =
NullPort = None
Device = HP LaserJet 4050 (computer000), HPBFDB1, LPT1

The device string contains three important attributes of the default printer in the system, which are the printer's device name HP LaserJet 4050 (computer000) in sequence ), the driver name is HPBFDB1 and the output port is lpt1.

To manipulate the system's default printer and implement the program's printing function, you can call the API function GetProfileString () from Win. the device string is obtained in the INI file. The prototype of this function is DWORD GetProfileString (lpAppName, LPTSTR lpKeyName, LPTSTR lpDefault, LPTSTR lpReturnedString, DWORD nSize ). In the function, the lpAppName parameter is the Win. the field name in the INI file; lpKeyName is the keyword name in the field; lpDefault is the default string; lpReturnedString is the retrieved string. If the function does not retrieve the corresponding string from the lpKeyName keyword, then kpRetrunedString returns the default string lpDefault; nSize is the length of the returned string.

After obtaining the preceding string, use the strtok () function to break down the string and obtain the three attributes related to the printer. As the parameter of the API function CreateDC () to create the environment handle of the printer device, if the CreateDC () function is called successfully, a device environment handle is created for the default printer. Otherwise, a NULL value is returned ). The original form of this function is: HDC CreateDC (LPCTSTR lpszDriver, LPCTSTR lpszDevice, LPCTSTR lpszOutput, const devmode * lpinitData ). The first three parameters of this function correspond to three attributes of the printer. The last parameter is the data of the printer driver initialization. Generally, this parameter can be set to NULL.

During the specific printing process, call the int StartDoc (HDC hdc, const docinfo * lpdi) function to start a printing task. The lpdi parameter is a pointer to the DOCINFO structure, the structure is as follows:

Typedef struct {
Int cbSize; // the size of the structure;
LPCTSTR lpszDocName; // document name;
LPCTSTR lpszOutput; // output document name, which is generally NULL;
LPCTSTR lpszDatatype; // the data type used to record the printing process. Generally, it is NULL;
DWORD fwType; // used to support additional information for printing. Generally, it is NULL;

After starting a print task, call the StartPage (hdcprint) function to let the printer go through the paper and notify the printer that the document will be printed. The next task is to output the data, this part of work is just as easy for developers to output text and images to the computer screen, except that the computer automatically outputs data to the printer according to the current device environment handle. After printing the data, you need to do some aftercare. Use the RestoreDC (hdcprint,-1) function to restore the printer device handle and EndPage (hdcprint) function to stop printing, finally, call the EndDoc (hdcprint) function to end the preceding print job.

Ii. programming steps

1. Start Visual C ++ 6.0, create an application Test based on the dialog box, add a Button in the dialog box form of the program, and set the properties of the Button: ID = IDC_PRINT, CAPTION = "print ";

2. Use the Class Wizard to add a mouse to the button and click OnPrint ()

3. Modify the OnPrint () function in the TestDlg. cpp file;

4. Add code and compile and run the program.

3. program code

//////////////////////////////////////// ////
Void CTestDlg: OnPrint ()
Char szprinter [80];
Char * szDevice, * szDriver, * szOutput;
HDC hdcprint; // define a device environment handle
// Define a print job
Static DOCINFO di = {sizeof (DOCINFO), "printer", NULL };
// Get the device string and store it in the array szprinter
GetProfileString ("windows", "device", szprinter, 80 );
// Break down the device string
If (NULL! = (SzDevice = strtok (szprinter, ",") & NULL! = (SzDriver = strtok (NULL ,","))&&
NULL! = (SzOutput = strtok (NULL ,",")))
// Create a printer device handle
If (hdcprint = CreateDC (szDriver, szDevice, szOutput, NULL ))! = 0)
If (StartDoc (hdcprint, & di)> 0) // start executing a print job
StartPage (hdcprint); // The printer takes the paper and starts printing.
SaveDC (hdcprint); // Save the printer device handle
// Output a line of text
TextOut (hdcprint, "Congratulations! ", 16 );
RestoreDC (hdcprint,-1); // restore the printer device handle
EndPage (hdcprint); // stop printing
EndDoc (hdcprint); // end a print job
MessageBox ("printed! "," Prompt ", MB_ICONINFORMATION );
// Use the API function DeleteDC to destroy a printer device handle
DeleteDC (hdcprint );
MessageBox ("no default printer, or no printer is installed! ");

Iv. Summary

The above example is very simple. I mainly use it to explain how to implement the printing function, rather than how to implement the complex printing effect, because they are no longer part of the scope we will discuss here, I believe that readers can flexibly set various objects (such as font objects and image brushes) in the device environment after they have mastered the above printing function ), you can certainly print out a variety of satisfactory results.
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: 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.