VC ++ (II)-Implementation of the printing and video functions

Source: Internet
Author: User

I haven't written any technical articles for a long time. This time, I got a job from the school. I have no way to force me to continue learning VC ++. Now I have to learn it with my head, although I know this is very difficult, I believe that I will win again, and I still have so many friends to help, I believe I will learn back.

This time, the instructor asked us to go back and change the previous Enrollment System to the following:
1. Print and print the pre-cable Function
2. Digital Photography

For the first function, it is much easier to use some of the ready-made tools in MFC, and check some information, and then read it by yourself. I think it is something to set parameters, but I need to be more careful.
I saw the following content yesterday. The process is like onfileprint ()-> cview: onprepareprinting ()-> cview: onbeginprinting ()-> CDC: onstartdoc () -> CDC: onstartpage ()-> cview: onprint ()-> onstoppage ()-> CDC: onstopdoc-> cview: onendprint (),
In onstoppage, if the article to be printed is multi-page, it is automatically returned to onstartpage for printing the new page, until all the articles to be printed are printed, the printing function can be completed. However, in the message, the printing function is enabled through on_file_print in the drop-down menu. You can also add your own purposes and functions to implement custom functions.
The print preview function is implemented by setting a parameter. I remember it was not very accurate to change the parameter, and it seemed very simple.
Hey, it may have been confusing to say so much. Let's look at others!

Source: Source Code sky
Http://www.codesky.net/article/list.asp? Id = 3232

If you have tried encoding in a C-based SDK program to generate print output, you will like the support for MFC printing.
Although it is not a thorough solution, it does greatly exceed the SDK encoding. Since the printing support is
If cview is derived, it is necessary to pay attention to complicated procedures in the document/view structure.

1. device independence

The device requires a description table to be used as the logical canvas for painting. Just as a display device description table is displayed.
The printer must also have a printer description table. When graphical functions are used, the same code is used in
The device description table and the printer device description table are painted. This dual nature is achieved through cview: ondraw ()
Method. The pointer to the device description table passed in cview: ondraw () can be from two different settings.
This arrangement provides multiple methods for the device-independent printing and print preview of MFC.

Print loop cview: onfileprint ()

MFC uses eight main methods as its "Print lead". each step of the printing process includes a cview
For details about how to call a method, see the following table.

Cview printing method

Method description
Onfileprint () Main print method for running a print loop

Onprepareprinting () calls doprepareprinting () to display the Print dialog box
Doprepareprinting () display Print dialog box
Onbeginprinting () allocates the location of the GDI resource used for painting on the printer DC
Onpreparedc () is called by MFC before onprint. Like the image method, this method should be located at the setting of DC Properties
Onprint () uses the printer DC to call ondraw ()
Ondraw () reproduce document data in printed or previewed printer DC
Onendprinting () is called by MFC after the print ends. This method is used to release the GDI resources of any specially allocated printer.

In order for MFC to respond to the print command, a message image item must be defined to make the pre-defined MFC Value
Id_file_print is associated with the cview: onfileprint () method that controls the printing process. Call this method
Other cview help methods to automate a large number of printing processes. Can be called directly using code
Cview: onfileprint (). Or, MFC calls it to respond to any command with the on_file_print Value
Message (such as the print command on the menu ).

Because onfileprint () is a member of the protection class, this item must reside in the message image of the View class, as shown below:

Begin_message_map (cmyview, cview)

...

// Required for Printing

On_command (id_file_print, cview: onfileprint)

End_message_map ()

 

Prepare to print

To support the printing and preview of the MFC application, you must redesign the following method:
Cview: onprepareprintinf (), which is called by MFC before printing or previewing a document. Its original
Type:

Virtual bool onprepareprinting (cprintinfo * pinfo );

The pinfo parameter is a pointer to a cprintinfo object that contains information about the current print job.

Note: If the print job is canceled in the result printing dialog box
The cview: onprepareprinting () method calls the cview: doprepareprinting () method and returns a zero value.

Cprintinfo Class Structure

------------------------------------------

Strut cprintinfo // printing information structure

{

Cprintinfo ();

~ Cprintinfo ();

Cprintdialog * m_ppd; // pointer To Print dialog

Bool m_bpreview; // true if in preview mode

Bool m_bdirect; // true if bypassing Print dialog

Bool m_bcontinueprinting; // set to false to end printing

Uint m_ncurpage; // current pages

Uint m_nnumpreviewpages; // desired number of preview pages

Cstring m_strpagedesc; // format string for page number display

Lpvoid m_lpuserdata; // pointer to user created struct

Crect m_rectdraw; // rectangle defining usable page Area

Void setminpage (uint nminpage );

Void setmaxpage (uint nmaxpage );

Uint getminpage () const;

Uint getmaxpage () const;

Uint getfrompage () const;

Uint gettoppage () const;

}

Cprintinfo data member

Data member description
M_ppd: pointer to the cprint dialog object used as the Print dialog box
M_bdirect indicates whether the document is directly printed. It bypasses the Print dialog box.
M_bpreview indicates whether the document is being previewed
M_bcontinueprinting indicates whether MFC should be kept within the print loop
M_ncurpage the number of pages being printed
M_nnumpreviewpages determine how many pages should be displayed in the print preview window: 1 or 2
M_lpuserdata pointer to the application-defined structure
M_rectdraw defines the rectangle of the current available page area on the printer DC
M_strpagedesc format string containing page number display information

Cprintinfo Class Method

Method description
Setminpage () specifies the first page number of the document (usually 1)
Setmaxpage () specifies the last page number of a document.
Getminpage (): Get the 1st page number of the document
Getmaxpage () obtains the last page number of a document.
Getfrompage () gets the 1st page number to be printed
Gettopage () gets the last page number to be printed

Print dialog box

Cview: The default implementation of the onprepareprinting () method is as follows:

Bool cdocview2view: onprepareprinting (cprintinfo * pinfo)

{

// Default preparation

Return doprepareprinting (pinfo );

}

By default, only the cview: doprepareprinting () method is called. The latter automatically handles the call without considering the Pring dialog box.

This dialog box obtains the pointer to the cprintinfo object (pinfo) from
Cview: The doprepareprinting () method is called. Cview: doprepareprinting () method
Has the following prototype:

Bool doprepareprinting (cprintinfo * pinfo );

MFC uses the value entered by the user in the print box to fill in various cprintinfo data members. Then,
The Print dialog box is a member of the cprintinfo object (pinfo-> m_ppd ).
For its HDC member, the method is as follows:

If (pinfo-> m_ppd-> m_pd.hdc = NULL)

{

// Call createprinterdc if DC was not created by abve

If (pinfo-> m_ppd-> createprinterdc () = NULL)

Return false;

}

Tip: Before calling cview: doprepareprinting (), set the cprintinfo class member to control
The value displayed in the Print dialog box. The value you specified appears in the corresponding dialog box control.

If the m_bpreview member is false, only the Print dialog box is displayed. During the print preview process, this dialog box is displayed.
Box does not appear. You can change the m_bpreview value to control the appearance of the dialog box.
Print dialog box of the print job. The print setting description table is created and stored in cview: doprepareprinting ()
After the cprintinfo data in is initialized, the call method cview: onprepareprinting () is
Exit. The next stop of the MFC print routine is the cview: onbeginprinting () method.

Start printing a job

Cview: the first task of the onbeginprinting () method is to make the legal print DC for your application is
Available. Re-design this method to provide initialization Based on printer DC features. Prototype representation of this method
As follows:

Virtual void onbeginprinting (CDC * PDC, cprintinfo * pinfo );

When a preview job is printed or printed, MFC calls this method, which is not used in the default cview implementation.
Job. By re-designing onbeginprint (), you may need
. If you allocate a GDI object in onbeginprint (), for example, select several brushes
Enter the print DC, which can be selected from the inside of the Priority cview: onprint () method.
Line (through cview: ondraw ).

After onbeginprint () is returned, the onfileprint () method starts the main print loop.
Every page of the file is initialized and printed in length.

Prepare to print the device description table

Call
The onpreparedc () method enables applications to adjust the DC features. The prototype of this method is described as follows:

Virtual void onpreparedc (CDC * PDC, cprintinfo * pinfo = NULL );

This method is usually re-designed to set the new image mode or some other features of DC.

Print job

After onpreparedc () is called, MFC calls the cview: onprint () method to print or preview each document page:

Virtual void onprint (CDC * PDC, cprintinfo * pinfo );

Onprint () uses the m_ncurpage member in the cprintinfo structure to determine the current page to be printed, and then
Onprint () calls ondraw () to draw pages on the printer DC.

Each page of the document calls the onprint () method once. If multiple pages of documents need to be re-designed
Current page printing logic. This requires that only the current page of ondraw () be drawn by operating the Windows origin to print
Each iteration of the loop view will move one page, which also allows you to print only the folding, such as the header and footer.

Here there is a minimal re-designed onprint () method, which finally calls a function with a print DC as a parameter
Ondraw () method:

Void csomeview: onprint (CDC * PDC, cprintinfo * pinfo)

{

Ondraw (PDC );

}

If the document contains more than one page, the next iteration of the print loop starts after onprint () is returned.

Clear

After all the pages of the document are printed, the print cycle ends (or the print preview ends), and the MFC call can be performed again.
The cview: onendprinting () method to release any
GDI resources. The prototype of this method is as follows:

Virtual void onendprinting (CDC * PDC, cprintinfo * pinfo );

Note: If the printer image is different from the display image, and the printer-specific GDI resources need to be appended
It is often used to allocate and release the GDI Resources in the onbeginprinting () method.

For the second question, I only have a preliminary influence and know the general steps, but I have not tried the details yet. I checked the following msdn, it is found that if you only want to implement a simple function, you only need to connect to the video device in two steps, and then create a capture window to operate on the content of the Video device, two functions are used: capcreatecapturewindow () and capdriverconnect (). Now I know about these functions, but I have not carefully read the relevant details, let me write it later!

Hey! I hope I can read this thing before I go back, because I still need to do something bigger!

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.