For some reason, people do get confused about what is in the print rich text control. I'm not an expert in printing, but I've figured it out after researching the materials, so I'm going to record it here.
The key to solving the problem is this message: em formatrange. Each time the message is sent, the content of the widget prints somewhat, and then the message returns an index of the first character of the text that has not yet been printed, and then it can continue to pass it to the next piece of text based on this index. All that's left is simple setup and resource destruction.
We start by getting the dimensions of the page and use the em setTargetDevice message to tell the rich text control what we intend to render. Next, we need to fill in the FormatRange struct, and to do this, we specify the HDC we want to render to and the paper size. But what about the range of characters?
We're lazy and let the rich text control handle it for us: we select all the text, and then let the rich text control tell us what we just selected, and it returns in the form of a charrange, which is exactly what we need.
Next is the print loop. While there is still text to print (and we didn't encounter an error), we start a new page and ask the rich text control to render the page, remembering that the next page should start with ** and then end the current page.
Notice that a check is performed to ensure that the rich text control moves forward. If not, then we end up in an infinite loop, printing out a blank page!(I don't know if this is theoretically possible, but I'd likewise prevent it.) )
When the print loop is complete, we let the rich text control know that we're done by sending a final em formatrange message, and we can discard the information it cached.
We can use all the information we have learned in the last few days to make a simple "print RTF" program.
We create a rich text control and populate it with the files passed on the command line. We then ask the printdlg function to give us the DC of the user's default printer. We give the document a title, start the document, print the rich text into the document, and then end the document (or abort the document if there is a problem during the printing process). Clean up a little and the program is out of operation. A small program that can print any RTF file without any particularly difficult to understand.
Summary. I sometimes really want to put the print feature on the last line on the list of requirements, but in the real world, this feature is probably the most desirable for a certain type of user. So, try your best to embrace what you're most afraid of.
At last. Raymond Chen's "The Old New Thing" is one of my favorite blogs, and it has a lot of little knowledge about Windows, which is really helpful for Windows developers. **how do i print the contents of a rich text control?》