Thread: Printing from a Rich Edit control

  1. #1
    Registered User JustMax's Avatar
    Join Date
    Jan 2009
    Posts
    59

    Printing from a Rich Edit control

    I have an application with a Rich Edit Control. My "print" function works but I get a small square at the end of each line on the printout.

    Any ideas as to how to get rid of them? I don't know if it the way I am printing or the way the Rich Edit control is set up.

    I also noticed that, if I select the text in the control, there is a blank space inserted after each line of text I type in.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Sounds like you're adding a \n or \r when you don't need to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User JustMax's Avatar
    Join Date
    Jan 2009
    Posts
    59
    I can open a blank form and type in a word, when I print, it has the square at the end of the word. I am not adding anything programmatically.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Is it printing the null character ('\0')?

  5. #5
    Registered User JustMax's Avatar
    Join Date
    Jan 2009
    Posts
    59
    I have no way of telling which character it is. I am assuming it is the '\0' or '\n' but I do not know why or how it is getting in there. If I open a text file in notepad, it isn't there but the same file in my control has them.

  6. #6
    Registered User JustMax's Avatar
    Join Date
    Jan 2009
    Posts
    59
    Could I be doing something wrong here:
    Code:
    TextOut (pd.hDC, 0, yChar * iLine, pstrBuffer,
                  (int) SendMessage (hwndEdit, EM_GETLINE,
                  (WPARAM) iLineNum, (LPARAM) pstrBuffer));
    Here is the print function:

    Code:
    BOOL PrintTheFile (HINSTANCE hInst, HWND hwnd, HWND hwndEdit, 
                           PTSTR szTitleName)
    {
         static DOCINFO  di = { sizeof (DOCINFO) } ;
         static PRINTDLG pd ;
         BOOL            bSuccess ;
         int             yChar, iCharsPerLine, iLinesPerPage, iTotalLines,
                         iTotalPages, iPage, iLine, iLineNum ;
         PTSTR           pstrBuffer ;
         TCHAR           szJobName [64 + MAX_PATH] ;
         TEXTMETRIC      tm ;
         WORD            iColCopy, iNoiColCopy ;
    
              // Invoke Print common dialog box
         
         pd.lStructSize         = sizeof (PRINTDLG) ;
         pd.hwndOwner           = hwnd ;
         pd.hDevMode            = NULL ;
         pd.hDevNames           = NULL ;
         pd.hDC                 = NULL ;
         pd.Flags               = PD_ALLPAGES | PD_COLLATE | 
                                  PD_RETURNDC | PD_NOSELECTION ;
         pd.nFromPage           = 0 ;
         pd.nToPage             = 0 ;
         pd.nMinPage            = 0 ;
         pd.nMaxPage            = 0 ;
         pd.nCopies             = 1 ;
         pd.hInstance           = NULL ;
         pd.lCustData           = 0L ;
         pd.lpfnPrintHook       = NULL ;
         pd.lpfnSetupHook       = NULL ;
         pd.lpPrintTemplateName = NULL ;
         pd.lpSetupTemplateName = NULL ;
         pd.hPrintTemplate      = NULL ;
         pd.hSetupTemplate      = NULL ;
         
         if (!PrintDlg (&pd))
              return TRUE ;
    
         if (0 == (iTotalLines = SendMessage (hwndEdit, EM_GETLINECOUNT, 0, 0)))
    	 {	
              return TRUE ;
    	 }
              // Calculate necessary metrics for file 
         
         GetTextMetrics (pd.hDC, &tm) ;
         yChar = tm.tmHeight + tm.tmExternalLeading ;
         
         iCharsPerLine = GetDeviceCaps (pd.hDC, HORZRES) / tm.tmAveCharWidth ;
         iLinesPerPage = GetDeviceCaps (pd.hDC, VERTRES) / yChar ;
         iTotalPages   = (iTotalLines + iLinesPerPage - 1) / iLinesPerPage ;
    
              // Allocate a buffer for each line of text
         
         pstrBuffer = (PSTR)malloc(sizeof (TCHAR) * (iCharsPerLine + 1)) ;
              // Display the printing dialog box
         
         EnableWindow (hwnd, FALSE) ;
         
         bSuccess   = TRUE ;
         bUserAbort = FALSE ;
         
         hDlgPrint = CreateDialog (hInst, TEXT ("PrintDlgBox"), 
                                   hwnd, PrintDlgProc) ;
    
         SetDlgItemText (hDlgPrint, IDC_RICHEDIT, szTitleName) ;
         SetAbortProc (pd.hDC, AbortProc) ;
    
              // Start the document
         GetWindowText (hwnd, szJobName, sizeof (szJobName)) ;
         di.lpszDocName = szJobName ;
         
         if (StartDoc (pd.hDC, &di) > 0)
         {
                   // Collation requires this loop and iNoiColCopy
    
              for (iColCopy = 0 ;
                   iColCopy < ((WORD) pd.Flags & PD_COLLATE ? pd.nCopies : 1) ;
                   iColCopy++)
              {
                   for (iPage = 0 ; iPage < iTotalPages ; iPage++)
                   {
                        for (iNoiColCopy = 0 ;
                             iNoiColCopy < (pd.Flags & PD_COLLATE ? 1 : pd.nCopies);
                             iNoiColCopy++)
                        {
                                  // Start the page
    
                             if (StartPage (pd.hDC) < 0)
                             {
                                  bSuccess = FALSE ;
                                  break ;
                             }
    
                                  // For each page, print the lines
                             
                             for (iLine = 0 ; iLine < iLinesPerPage ; iLine++)
                             {
                                  iLineNum = iLinesPerPage * iPage + iLine ;
                                  
                                  if (iLineNum > iTotalLines)
                                       break ;
                                  
                                  *(int *) pstrBuffer = iCharsPerLine ;
                                  
                                  TextOut (pd.hDC, 0, yChar * iLine, pstrBuffer,
                                           (int) SendMessage (hwndEdit, EM_GETLINE,
                                           (WPARAM) iLineNum, (LPARAM) pstrBuffer));
                             }
                             
                             if (EndPage (pd.hDC) < 0)
                             {
                                  bSuccess = FALSE ;
                                  break ;
                             }
                             
                             if (bUserAbort)
                                  break ;
                        }
                        SendMessage( hwnd, EM_SETSEL, 0, 0 );
                        if (!bSuccess || bUserAbort)
                             break ;
                   }
                   
                   if (!bSuccess || bUserAbort)
                        break ;
              }
         }
         else
              bSuccess = FALSE ;
         
         if (bSuccess)
              EndDoc (pd.hDC) ;
         
         if (!bUserAbort)
         {
              EnableWindow (hwnd, TRUE) ;
              DestroyWindow (hDlgPrint) ;
         }
         
         free (pstrBuffer) ;
    
         DeleteDC (pd.hDC) ;
         
         return bSuccess && !bUserAbort ;
    }
    Last edited by JustMax; 02-11-2009 at 05:03 PM.

  7. #7
    Registered User JustMax's Avatar
    Join Date
    Jan 2009
    Posts
    59
    Could it have to do with opening a text file in a rich edit control? There appears to be a blank space at the end of each line in the rich edit control. I also see it (by selecting all text) in WordPad. It does not appear in NotePad though.

  8. #8
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    I have seen this problem before (when I was viewing a txt file, a square came up where each newline should have been. that looked much worse than the prob you are having). It is a whitespace character, probably '\n', you could try omitting the last character of each line (might work).
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  9. #9
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Quote Originally Posted by JustMax View Post
    pstrBuffer = (PSTR)malloc(sizeof (TCHAR) * (iCharsPerLine + 1)) ;
    You forgot to check that this succeeds and also cast it to the wrong type, you wanted PTSTR instead.
    Last edited by adeyblue; 02-12-2009 at 03:31 PM. Reason: Me fail English, that's unpossible

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I think thei issue is that you are using the number of characters copied to the buffer, which includes the /n/r or /0
    Also you will print a NULL buffer if there is no text of the Send message fails.

    Try
    Code:
                                
    int iLen=SendMessage (hwndEdit, EM_GETLINE, (WPARAM) iLineNum, (LPARAM) pstrBuffer);
    if(iLen)//we got some text
    {
         TextOut (pd.hDC, 0, yChar * iLine, pstrBuffer, lstrlen(pstrBuffer));
    }
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  11. #11
    Registered User JustMax's Avatar
    Join Date
    Jan 2009
    Posts
    59
    Thank you all, Novacain has eased the pain. I now have a good looking printout.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  3. Dialog Edit Control and Enter Key
    By Quantrizi in forum Windows Programming
    Replies: 2
    Last Post: 04-14-2004, 07:59 AM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Rich edit control example Win API
    By Echidna in forum Windows Programming
    Replies: 1
    Last Post: 09-17-2001, 02:12 AM