问题
I am trying to understand what Hasmorepages PrintPageEventArgs property is, why would you use it and how does it work.
MSDN Library doesn't really have a good explanation. All they say is that if you set it to true, printpage event is called again. Is that mean the event loops on itself without leaving or leaves and calls itself again or relies on you to call the printpage event again?
I am just trying to understand PrintPageEventArgs.hasmorepages property. Any hints or help will be greatly appreciated.
Thank you,
回答1:
HasMorePages
is a boolean property of the PrintPageEventArgs
you receive as a parameter of the event. You set it to True
after printing the current page if there are more pages to be printed, or False
if the current page is the last one.
Events are always called for you by something in the framework, and are never supposed to be called directly by you. They are events, which mean they're dispatched to tell you that something has happened and give you a chance to respond or react.
If you set it to True
, the PrintPage
event is called again automatically; you do not call it yourself. (That's exactly what the MSDN documentation says: If you set it to true, the printpage event is called again. It doesn't say you'll need to call it again - it says is called again.)
ev.HasMorePages := DoYouHaveMorePagesToPrint;
For a VB.NET example of the event and how to use ev.HasMorePages
, see the MSDN documentation for PrintDocument. For info on PrintPageEventArgs
, see this MSDN page, which has a link to the members of PrintPageEventArgs (including HasMorePages
).
回答2:
It is not a property of a PrintDocument, it is a property of PrintPageEventArgs. An instance of which gets passed to your PrintPage event handler.
The way the PrintController and PrintDocument classes work is heavily affected by the way printing is implemented in Windows. A core implementation detail is that it is page-based. The printer driver deals with one page at a time, the underlying winapi function is StartPage(). Anything rendered to the print device context ends up on one page. Then the EndPage() winapi function completes the page and submits it to the spooler.
It might help to diagram the calls made while a 3 page document is printed:
StartDoc()
PrintDocument.BeginPrint event
StartPage()
PrintDocument.PrintPage event, e.HasMorePages = true
EndPage()
StartPage()
PrintDocument.PrintPage event, e.HasMorePages = true
EndPage()
StartPage()
PrintDocument.PrintPage event, e.HasMorePages = false
EndPage()
PrintDocument.EndPrint event
EndDoc()
It ought to be clear now, by assigning e.HasMorePage = true, you let the PrintController keep firing the PrintPage event. It is up to you to paginate your document and render the content of the correct page in your PrintPage event handler. You'll need the BeginPrint event to, say, set your internal page counter to 0.
来源:https://stackoverflow.com/questions/11544989/what-does-hasmorepages-printpageeventargs-property-do-exactly