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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Get the size of RTF text on a print DC

Get the size of RTF text on a print DC

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
4 Posts 2 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.
  • C Offline
    C Offline
    CodeBrain
    wrote on last edited by
    #1

    I want to print the content of a CRichEditCtrl. So far no problem for me, I know how to do it. My problem is that I have to know the size of RTF text on the paper before I start the printing. So I have to measure the size of the text depending on the resolution of a DC. If I have to print normal ASCII text I use the CDC::DrawText method with the option DT_CALCRECT which gives me the size of the text section for the given DC. Is there a simmilar method for RTF text? As a workaround I think of using the CRichEditCtrl::FormatRange method and calculate the size iterative by giving a guessed value for the text rectangle to FormatRange and see, if all charcters fit into the rectangle. Now I size the rectangle until all characters fit into the rectangle. Has anyone an idea how to solve the problem or is this the only solution? Thanks in advance.

    E 1 Reply Last reply
    0
    • C CodeBrain

      I want to print the content of a CRichEditCtrl. So far no problem for me, I know how to do it. My problem is that I have to know the size of RTF text on the paper before I start the printing. So I have to measure the size of the text depending on the resolution of a DC. If I have to print normal ASCII text I use the CDC::DrawText method with the option DT_CALCRECT which gives me the size of the text section for the given DC. Is there a simmilar method for RTF text? As a workaround I think of using the CRichEditCtrl::FormatRange method and calculate the size iterative by giving a guessed value for the text rectangle to FormatRange and see, if all charcters fit into the rectangle. Now I size the rectangle until all characters fit into the rectangle. Has anyone an idea how to solve the problem or is this the only solution? Thanks in advance.

      E Offline
      E Offline
      eco
      wrote on last edited by
      #2

      There is another solution: using windowless richedit but you'll need to know COM 1. loadlibrary richedit.dll 2. obtain the CreateTextServices proc : GetProcAddress( hRichDll, "CreateTextServices" ) 2. obtain the IID_ITextService : (GUID*)(GetProcAddress( hRichDll, "IID_ITextHost" )) 3. create an instance of ITextHost (a dummy one) .. read the doc 3. call query interfce for the ITextService IUnknown* unk = NULL; (*fnCreateServices)( NULL, &m_TextHost, &unk ); unk->QueryInterface( g_RichService, (void**)&m_Service ); unk->Release( );4. send the rtf to the service m_Service->TxSendMessage( EM_STREAMIN, SF_RTF, ... (doc)... ) 5. ask the natural size long forHeight = 1; SIZEL sze; sze.cx = forWidth; sze.cy = forHeight; m_Service->TxGetNaturalSize( DVASPECT_DOCPRINT, m_hDC, 0, NULL, TXTNS_FITTOCONTENT, (SIZEL*) &sze, &forWidth, &forHeight ); i cannot give you the whole source of the implementation of ITextHost because it should be too long, but read the ITextHost doc and implement the strict minimum. i use that way to create dynamic rtf, calculate rtf display size, print rtf ... notice that under XP, you should use 'msftedit.dll' instead of 'richedit.dll' msftedit is really really faster. good luke ;)

      E C 2 Replies Last reply
      0
      • E eco

        There is another solution: using windowless richedit but you'll need to know COM 1. loadlibrary richedit.dll 2. obtain the CreateTextServices proc : GetProcAddress( hRichDll, "CreateTextServices" ) 2. obtain the IID_ITextService : (GUID*)(GetProcAddress( hRichDll, "IID_ITextHost" )) 3. create an instance of ITextHost (a dummy one) .. read the doc 3. call query interfce for the ITextService IUnknown* unk = NULL; (*fnCreateServices)( NULL, &m_TextHost, &unk ); unk->QueryInterface( g_RichService, (void**)&m_Service ); unk->Release( );4. send the rtf to the service m_Service->TxSendMessage( EM_STREAMIN, SF_RTF, ... (doc)... ) 5. ask the natural size long forHeight = 1; SIZEL sze; sze.cx = forWidth; sze.cy = forHeight; m_Service->TxGetNaturalSize( DVASPECT_DOCPRINT, m_hDC, 0, NULL, TXTNS_FITTOCONTENT, (SIZEL*) &sze, &forWidth, &forHeight ); i cannot give you the whole source of the implementation of ITextHost because it should be too long, but read the ITextHost doc and implement the strict minimum. i use that way to create dynamic rtf, calculate rtf display size, print rtf ... notice that under XP, you should use 'msftedit.dll' instead of 'richedit.dll' msftedit is really really faster. good luke ;)

        E Offline
        E Offline
        eco
        wrote on last edited by
        #3

        arg mistake, read that 2. obtain the IID_ITextServices : (GUID*)(GetProcAddress( hRichDll, "IID_ITextServices" ))

        1 Reply Last reply
        0
        • E eco

          There is another solution: using windowless richedit but you'll need to know COM 1. loadlibrary richedit.dll 2. obtain the CreateTextServices proc : GetProcAddress( hRichDll, "CreateTextServices" ) 2. obtain the IID_ITextService : (GUID*)(GetProcAddress( hRichDll, "IID_ITextHost" )) 3. create an instance of ITextHost (a dummy one) .. read the doc 3. call query interfce for the ITextService IUnknown* unk = NULL; (*fnCreateServices)( NULL, &m_TextHost, &unk ); unk->QueryInterface( g_RichService, (void**)&m_Service ); unk->Release( );4. send the rtf to the service m_Service->TxSendMessage( EM_STREAMIN, SF_RTF, ... (doc)... ) 5. ask the natural size long forHeight = 1; SIZEL sze; sze.cx = forWidth; sze.cy = forHeight; m_Service->TxGetNaturalSize( DVASPECT_DOCPRINT, m_hDC, 0, NULL, TXTNS_FITTOCONTENT, (SIZEL*) &sze, &forWidth, &forHeight ); i cannot give you the whole source of the implementation of ITextHost because it should be too long, but read the ITextHost doc and implement the strict minimum. i use that way to create dynamic rtf, calculate rtf display size, print rtf ... notice that under XP, you should use 'msftedit.dll' instead of 'richedit.dll' msftedit is really really faster. good luke ;)

          C Offline
          C Offline
          CodeBrain
          wrote on last edited by
          #4

          Thank you for your detailed answer! :) I hoped I could work with RTF without using COM, but I think I can not escape the Microsoft doctrine.:rolleyes:

          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