Let’s talk about the last post “Publish a PrintDocument-derived object that supports large amounts of text printing and make the source code public”
In the article, I published an object that inherits from System.Drawing.Printing.PrintDocument. This object supports generating print job documents from file streams and text strings. I did not test it carefully at the time and found that there was a big logic bug in it. .
The symptom of the bug is that if you use string assignment to generate an object instance, you will be surprised to find that the document content is printed twice during the print preview. And I only discovered this problem later.
After searching, I found the reason, but the reason is strange.
The principle is that in order to read one line of text at a time, I use the System.IO.StringReader class as the reader. Its constructor is the string that is passed in to save the text content, and then uses the StringReader.ReadLine() method to read the text and generate printed content.
But I have to implement a mechanism to check whether the reader has read the last content, and the StringReader.Peek() method is to detect whether there is content behind it, and if there is no content, it returns a value of -1. After my testing, no matter how it is detected, -1 is not returned. Instead, -1 is returned after inputting the content twice, which causes the same content to be displayed twice.
This bug has been corrected. I modified the detection method through other methods. This time there is no need to type the content twice. Anyone who downloaded it, please download it again.
Now let's talk about the PrintPreveiwControl control.
The first thing to note is that it is a custom windows control, not a new print preview window. It is the part of the print preview window that displays the print preview. If you want to use it, you must create a windows form and then use it.
To be honest, Microsoft's own print preview window in .net is very bad. It doesn't look good and is not easy to use, so Microsoft provides a System.Windows.Forms.PrintPreviewControl control, and this control is not very good either. The main reason is that it does not support the mouse wheel, and the most important thing is that it does not support printing page counting. The number of available pages during print preview is a necessary attribute for buttons such as "Previous Page" and "Next Page" in the print preview window.
So I used Reflector to decompile Microsoft's PrintPreviewControl control, used its original code and added the "OnMouseWheel" event handler, and OnMouseWheel is specifically designed to handle the operation of the mouse wheel when it is scrolled. Also publish the source code.
I added a lot of comments in the OnMouseWheel method. Friends who want to deal with the mouse wheel can use the principles of this code.
Download: Click here to download
http://www.cnblogs.com/submaie/archive/2006/08/11/474568.html