Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Draw rtf on user control

Draw rtf on user control

Scheduled Pinned Locked Moved C#
helptutorial
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z ZGelic

    I need to create control that accepts rtf and draws it. Problem is that drawstring takes only regular text and I cant find anywhere how to draw rtf. Any sugestions are welcome and appreciated...

    T Offline
    T Offline
    Tom Deketelaere
    wrote on last edited by
    #2

    draw rtf :confused: Have a look at 'RichTextBox' (that's a standard control)

    Z 1 Reply Last reply
    0
    • T Tom Deketelaere

      draw rtf :confused: Have a look at 'RichTextBox' (that's a standard control)

      Z Offline
      Z Offline
      ZGelic
      wrote on last edited by
      #3

      Yes I know about richtextbox.. and load and save rtf etc. Please read post again. I need to draw rtf on user control. But thanks anyway.

      T 1 Reply Last reply
      0
      • Z ZGelic

        Yes I know about richtextbox.. and load and save rtf etc. Please read post again. I need to draw rtf on user control. But thanks anyway.

        T Offline
        T Offline
        Tom Deketelaere
        wrote on last edited by
        #4

        You'll have explain a bit better what you mean with 'draw rtf'. I don't really see the point in actually painting the text onto a usercontrol when the richttextbox (that you can drop onto a usercontrol) would accomplish all this for you. (if you don't want users to alter the text just set it to disabled or read-only)

        1 Reply Last reply
        0
        • Z ZGelic

          I need to create control that accepts rtf and draws it. Problem is that drawstring takes only regular text and I cant find anywhere how to draw rtf. Any sugestions are welcome and appreciated...

          0 Offline
          0 Offline
          0x3c0
          wrote on last edited by
          #5

          Try using RichTextBox.DrawToBitmap, then rendering the bitmap. It's not perfect (it only draws the borders of embedded images), but suffices for a managed solution. I think you could use a method which draws a window (and by extension, control) image to a HDC, but I can't remember the name right now

          L 1 Reply Last reply
          0
          • 0 0x3c0

            Try using RichTextBox.DrawToBitmap, then rendering the bitmap. It's not perfect (it only draws the borders of embedded images), but suffices for a managed solution. I think you could use a method which draws a window (and by extension, control) image to a HDC, but I can't remember the name right now

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #6

            Hi, Conceptually OK, however a few days ago I learned RichTextBox.DrawToBitmap() does not really work. I solved that by creating my own method, based on FormatRange, see here[^]. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


            0 1 Reply Last reply
            0
            • L Luc Pattyn

              Hi, Conceptually OK, however a few days ago I learned RichTextBox.DrawToBitmap() does not really work. I solved that by creating my own method, based on FormatRange, see here[^]. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


              0 Offline
              0 Offline
              0x3c0
              wrote on last edited by
              #7

              That's very clever. Did you print to another HDC instead of the one given by PrintPageEventArgs, or use another method?

              L 1 Reply Last reply
              0
              • 0 0x3c0

                That's very clever. Did you print to another HDC instead of the one given by PrintPageEventArgs, or use another method?

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #8

                public void DrawToBitmap(Bitmap bitmap) {
                //Calculate the area to render and print
                RECT rectToPrint;
                rectToPrint.Top = 0;
                rectToPrint.Bottom = (int)(bitmap.Height*anInch);
                rectToPrint.Left = 0;
                rectToPrint.Right = (int)(bitmap.Width*anInch);

                //Calculate the size of the page
                RECT rectPage;
                rectPage.Top = 0;
                rectPage.Bottom = (int)(bitmap.Height\*anInch);
                rectPage.Left = 0;
                rectPage.Right = (int)(bitmap.Width\*anInch);
                
                Graphics g=Graphics.FromImage(bitmap);
                IntPtr hdc = g.GetHdc();
                
                FORMATRANGE fmtRange;
                // \[0,-1\]=everything
                fmtRange.chrg.cpMax = -1;				//Indicate character from to character to 
                fmtRange.chrg.cpMin = 0;
                fmtRange.hdc = hdc;                    //Use the same DC for measuring and rendering
                fmtRange.hdcTarget = hdc;              //Point at printer hDC
                fmtRange.rc = rectToPrint;             //Indicate the area on page to print
                fmtRange.rcPage = rectPage;            //Indicate size of page
                
                IntPtr res = IntPtr.Zero;
                
                IntPtr wparam = IntPtr.Zero;
                wparam = new IntPtr(1);
                
                //Get the pointer to the FORMATRANGE structure in memory
                IntPtr lparam= IntPtr.Zero;
                lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));
                Marshal.StructureToPtr(fmtRange, lparam, false);
                
                //Send the rendered data for printing 
                res = SendMessage(Handle, EM\_FORMATRANGE, wparam, lparam);
                
                //Free the block of memory allocated
                Marshal.FreeCoTaskMem(lparam);
                
                //Release the device context handle obtained by a previous call
                g.ReleaseHdc(hdc);
                

                }

                :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


                1 Reply Last reply
                0
                • Z ZGelic

                  I need to create control that accepts rtf and draws it. Problem is that drawstring takes only regular text and I cant find anywhere how to draw rtf. Any sugestions are welcome and appreciated...

                  Z Offline
                  Z Offline
                  ZGelic
                  wrote on last edited by
                  #9

                  First of all I want to thank you all for replying to this topic... But disscusion went in te wrong way. What I wanted is not any use of richtextbox, but method similiar to Graphics.Drawstring method that takes rtf instead of string and paints it on user control. Since there is no provided splution I guess I have to try to wirte some kind of parser that takes tekst and formating for it an use Graphics.Drawstring method...

                  J 1 Reply Last reply
                  0
                  • Z ZGelic

                    First of all I want to thank you all for replying to this topic... But disscusion went in te wrong way. What I wanted is not any use of richtextbox, but method similiar to Graphics.Drawstring method that takes rtf instead of string and paints it on user control. Since there is no provided splution I guess I have to try to wirte some kind of parser that takes tekst and formating for it an use Graphics.Drawstring method...

                    J Offline
                    J Offline
                    Jim Crafton
                    wrote on last edited by
                    #10

                    I think you may want some of the stuff in here: http://msdn.microsoft.com/en-us/library/cc656574(VS.85).aspx[^] and http://msdn.microsoft.com/en-us/library/cc656564(VS.85).aspx[^] and ITextDocument::Open[^] I have used this (the code is in C++ but it may give you some ideas) here: Win32TextPeer.cpp[^] You an create an ITextDocument, open your RTF, then use the TextServices to render the ITextDocument to an HDC.

                    ¡El diablo está en mis pantalones! ¡Mire, mire! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! Personal 3D projects Just Say No to Web 2 Point Oh

                    Z 1 Reply Last reply
                    0
                    • J Jim Crafton

                      I think you may want some of the stuff in here: http://msdn.microsoft.com/en-us/library/cc656574(VS.85).aspx[^] and http://msdn.microsoft.com/en-us/library/cc656564(VS.85).aspx[^] and ITextDocument::Open[^] I have used this (the code is in C++ but it may give you some ideas) here: Win32TextPeer.cpp[^] You an create an ITextDocument, open your RTF, then use the TextServices to render the ITextDocument to an HDC.

                      ¡El diablo está en mis pantalones! ¡Mire, mire! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! Personal 3D projects Just Say No to Web 2 Point Oh

                      Z Offline
                      Z Offline
                      ZGelic
                      wrote on last edited by
                      #11

                      Ok, thanks for the links. I'll take a closer look at ITextDocument.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups