4 Replies - 2872 Views - Last Post: 09 May 2012 - 02:16 AM Rate Topic: -----

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 761
  • Joined: 31-August 11

Using Print Preview With a Rich Text Box?

Posted 07 May 2012 - 10:37 PM

So I was going nuts trying to figure out something which I was thinking would be simple. I was trying to figure out how to set a print document to a printpreview dialog for my program using content in a Rich TextBox. It turns out it appears (I'm wondering if there are any alternatives), but it appears that you need to use something like so

See link if you want:
My link

basically you have to render it to a printdoc using such elaborate things like
// send the EM_FORMATRANGE message to the RichTextBox to render or measure
// a range of the document into a target specified by a FORMATRANGE structure.
int FormatRange(RichTextBox rtb, bool render, ref FORMATRANGE fmt)
{
    // render or measure part of the document
    int nextChar = SendMessageFormatRange(
        rtb.Handle,
        EM_FORMATRANGE,
        render ? 1 : 0,
        ref fmt);

    // reset after rendering/measuring
    SendMessage(rtb.Handle, EM_FORMATRANGE, 0, 0);

    // return next character to print
    return nextChar;
}
Note that


I feel cheesy using that code but should I? Is there any other way to do this? Why is this so complicated? YES THE CODE DOES WORK.

Is This A Good Question/Topic? 0
  • +

Replies To: Using Print Preview With a Rich Text Box?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,674
  • Joined: 02-June 10

Re: Using Print Preview With a Rich Text Box?

Posted 08 May 2012 - 03:30 PM

http://msdn.microsof...e/cc188767.aspx
Was This Post Helpful? 1
  • +
  • -

#3 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 761
  • Joined: 31-August 11

Re: Using Print Preview With a Rich Text Box?

Posted 08 May 2012 - 07:17 PM

View PosttlhIn`toq, on 08 May 2012 - 03:30 PM, said:



Thanks man I appreciate it just what I was looking for +1
Was This Post Helpful? 0
  • +
  • -

#4 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 761
  • Joined: 31-August 11

Re: Using Print Preview With a Rich Text Box?

Posted 08 May 2012 - 07:59 PM

View PosttlhIn`toq, on 08 May 2012 - 03:30 PM, said:



Also while I have nothing against those methods of printing or print previewing they appear i.e. the methods appear to mostly work by having a document like at text file saved first PLUS I'm using a rich text box and most the the examples are text files which is cool still helpful
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,742
  • Joined: 05-May 12

Re: Using Print Preview With a Rich Text Box?

Posted 09 May 2012 - 02:16 AM

The point of the article was to show the entire context of how printing works from the 20,000 ft level down where a print or print preview dialog is used down to the ground level where you see the code iterating over the lines of the text file and rendering them.

In the case of rendering the contents of a RichTextBox, instead of iterating over the lines of a text file, you are iterating over the "lines" within the RichTextBox. The lines are defined to be parameters in the FORMATRANGE struct passed to the RichEdit control wrapped by the RichTextBox. Instead of calling gdiPage.DrawString() you are calling FormatRange().

This post has been edited by Skydiver: 09 May 2012 - 02:17 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1