Archived

This topic is now archived and is closed to further replies.

How can i print the contents of RichEditCtrls?

This topic is 5666 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I would like to know, how to print the contents of RichEdit controls. I have tried to do as MSDN libary said (with FormatRange and DisplayBand), but it draws the contents on wrong (not in those i want to) locations, i think i am doing something wrong. Can anyone supply me with working code or give any ideas, what is wrong. My code is something like the following: void CMYView::OnPrint(CDC* pDC, CPrintInfo* pInfo) { //... FORMATRANGE fr; CHARRANGE ch; CRect rc; ch.cpMin = 0; ch.cpMax = -1; rc = pInfo->m_rectDrawM fr.hdc = pDC->m_hDC; fr.hdcTarget = pDC->m_hDC; fr.rc = pInfo->m_rectDraw; fr.rcPage = pInfo->m_rectDraw; fr.chrg = ch; rc.bottom /= 3; rc.top = rc.bottom/8; //m_ctlRichEdit is that richedit control m_ctlRichEdit.FormatRange(&fr, TRUE); m_ctlRichEdit.DisplayBand(&rc); //... } Siim Karus (not Toomas in any case, but still a member of RS)

Share this post


Link to post
Share on other sites
This isn't exactly what you want as this code was intended to be called from within a dialog not a view. m_rich is the control.

You should be able to see how it's done though.


    
void PrintTheContents()
{
FORMATRANGE fr;
DOCINFO docInfo;
LONG lTextOut, lTextAmt;
PRINTDLG pd;

// Initialize the PRINTDLG structure.

pd.lStructSize = sizeof (PRINTDLG);
pd.hwndOwner = m_rich.m_hWnd;
pd.hDevMode = (HANDLE)NULL;
pd.hDevNames = (HANDLE)NULL;
pd.nFromPage = 0;
pd.nToPage = 0;
pd.nMinPage = 0;
pd.nMaxPage = 0;
pd.nCopies = 0;
pd.hInstance = (struct HINSTANCE__ *)(AfxGetApp()->m_hInstance);
pd.Flags = PD_RETURNDC | PD_NOPAGENUMS | PD_NOSELECTION | PD_PRINTSETUP;
pd.lpfnSetupHook = (LPSETUPHOOKPROC)(FARPROC)NULL;
pd.lpSetupTemplateName = (LPTSTR)NULL;
pd.lpfnPrintHook = (LPPRINTHOOKPROC)(FARPROC)NULL;
pd.lpPrintTemplateName = (LPTSTR)NULL;

// Get the printer DC.

if (PrintDlg (&pd) == TRUE)
{
// Fill out the FORMATRANGE structure for the RTF output.

fr.hdc = fr.hdcTarget = pd.hDC; // HDC

FORMATRANGE fr;
int nHorizRes = GetDeviceCaps(pd.hDC, HORZRES),
nVertRes = GetDeviceCaps(pd.hDC, VERTRES),
nLogPixelsX = GetDeviceCaps(pd.hDC, LOGPIXELSX),
nLogPixelsY = GetDeviceCaps(pd.hDC, LOGPIXELSY);

// Ensure the printer DC is in MM_TEXT mode.


// Rendering to the same DC we are measuring.

ZeroMemory(&fr, sizeof(fr));
fr.hdc = fr.hdcTarget = pd.hDC;

// Set up the page.

fr.rcPage.left = fr.rcPage.top = 0;
fr.rcPage.right = (nHorizRes/nLogPixelsX) * 1440;
fr.rcPage.bottom = (nVertRes/nLogPixelsY) * 1440;

// Set up 1" margins all around.

fr.rc.left = fr.rcPage.left + 1440; // 1440 TWIPS = 1 inch.

fr.rc.top = fr.rcPage.top + 1440;
fr.rc.right = fr.rcPage.right;// - 1440;

fr.rc.bottom = fr.rcPage.bottom;// - 1440;


// Default the range of text to print as the entire document.

fr.chrg.cpMin = 0;
fr.chrg.cpMax = -1;

// Fill out the DOCINFO structure.

docInfo.cbSize = sizeof (DOCINFO);
docInfo.lpszDocName = "Report";
docInfo.lpszOutput = NULL;

// Be sure that the printer DC is in text mode.

SetMapMode (pd.hDC, MM_TEXT);

StartDoc (pd.hDC, &docInfo);
StartPage (pd.hDC);


lTextOut = 0;
lTextAmt = m_rich.SendMessage(WM_GETTEXTLENGTH, 0, 0);

while (lTextOut < lTextAmt)
{
lTextOut = m_rich.SendMessage(EM_FORMATRANGE, TRUE,(LPARAM)&fr);

if (lTextOut < lTextAmt)
{
EndPage (pd.hDC);
StartPage (pd.hDC);
fr.chrg.cpMin = lTextOut;
fr.chrg.cpMax = -1;
}
}

// Reset the formatting of the rich edit control.

m_rich.SendMessage(EM_FORMATRANGE, TRUE, (LPARAM)NULL);

// Finish the document.

EndPage (pd.hDC);
EndDoc (pd.hDC);

// Delete the printer DC.

DeleteDC (pd.hDC);
}
}



Hope this is of some help

[edited by - Box2020 on June 5, 2002 11:50:42 AM]

Share this post


Link to post
Share on other sites
Do i have to call displayband also?
I could not understand, how to print other richedit controls data in a specified location, is that supplied by FORMATRANGE::rc?

Siim Karus

Share this post


Link to post
Share on other sites
well maybe i am not a big help but i''ll write down couple words regarding to using win32 sdk api functions. you can use SendMessage(hwnd,WM_GETTEXT,length,textbuffer) to retrieve any text from RichEditctrl(hwnd) i had tested up to around 30k letters and it worked fine(just little slow). damn i feel mad dumb i gotta learn MFC

Share this post


Link to post
Share on other sites
OOPS I''ve just spotted a mistake. I''ve scoped in a second instance of FORMATRANGE fr; the function should be

  
void PrintTheContents()
{
FORMATRANGE fr;
DOCINFO docInfo;
LONG lTextOut, lTextAmt;
PRINTDLG pd;

// Initialize the PRINTDLG structure.

pd.lStructSize = sizeof (PRINTDLG);
pd.hwndOwner = m_rich.m_hWnd;
pd.hDevMode = (HANDLE)NULL;
pd.hDevNames = (HANDLE)NULL;
pd.nFromPage = 0;
pd.nToPage = 0;
pd.nMinPage = 0;
pd.nMaxPage = 0;
pd.nCopies = 0;
pd.hInstance = (struct HINSTANCE__ *)(AfxGetApp()->m_hInstance);
pd.Flags = PD_RETURNDC | PD_NOPAGENUMS | PD_NOSELECTION | PD_PRINTSETUP;
pd.lpfnSetupHook = (LPSETUPHOOKPROC)(FARPROC)NULL;
pd.lpSetupTemplateName = (LPTSTR)NULL;
pd.lpfnPrintHook = (LPPRINTHOOKPROC)(FARPROC)NULL;
pd.lpPrintTemplateName = (LPTSTR)NULL;

// Get the printer DC.

if (PrintDlg (&pd) == TRUE)
{
// Fill out the FORMATRANGE structure for the RTF output.


int nHorizRes = GetDeviceCaps(pd.hDC, HORZRES),
nVertRes = GetDeviceCaps(pd.hDC, VERTRES),
nLogPixelsX = GetDeviceCaps(pd.hDC, LOGPIXELSX),
nLogPixelsY = GetDeviceCaps(pd.hDC, LOGPIXELSY);


// Rendering to the same DC we are measuring.

ZeroMemory(&fr, sizeof(fr));
fr.hdc = fr.hdcTarget = pd.hDC;

// Set up the page.

fr.rcPage.left = fr.rcPage.top = 0;
fr.rcPage.right = (nHorizRes/nLogPixelsX) * 1440;
fr.rcPage.bottom = (nVertRes/nLogPixelsY) * 1440;

// Set up 1" margins all around.

fr.rc.left = fr.rcPage.left + 1440; // 1440 TWIPS = 1 inch.

fr.rc.top = fr.rcPage.top + 1440;
fr.rc.right = fr.rcPage.right;// - 1440;

fr.rc.bottom = fr.rcPage.bottom;// - 1440;


// Default the range of text to print as the entire document.

fr.chrg.cpMin = 0;
fr.chrg.cpMax = -1;

// Fill out the DOCINFO structure.

docInfo.cbSize = sizeof (DOCINFO);
docInfo.lpszDocName = "Report";
docInfo.lpszOutput = NULL;

// Be sure that the printer DC is in text mode.

SetMapMode (pd.hDC, MM_TEXT);

StartDoc (pd.hDC, &docInfo);
StartPage (pd.hDC);

// this line shows the display area when printing


lTextOut = 0;
lTextAmt = m_rich.SendMessage(WM_GETTEXTLENGTH, 0, 0);

while (lTextOut < lTextAmt)
{
lTextOut = m_rich.SendMessage(EM_FORMATRANGE, TRUE,(LPARAM)&fr);

if (lTextOut < lTextAmt)
{
EndPage (pd.hDC);
StartPage (pd.hDC);
fr.chrg.cpMin = lTextOut;
fr.chrg.cpMax = -1;
}
}

// Reset the formatting of the rich edit control.

m_rich.SendMessage(EM_FORMATRANGE, TRUE, (LPARAM)NULL);

// Finish the document.

EndPage (pd.hDC);
EndDoc (pd.hDC);

// Delete the printer DC.

DeleteDC (pd.hDC);
}
}


as for CRichEditCtrl::DisplayBand I didn''t need to call it as this code worked for my purposes

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by yangdh15
well maybe i am not a big help but i''ll write down couple words regarding to using win32 sdk api functions. you can use SendMessage(hwnd,WM_GETTEXT,length,textbuffer) to retrieve any text from RichEditctrl(hwnd) i had tested up to around 30k letters and it worked fine(just little slow). damn i feel mad dumb i gotta learn MFC


The trick is that i want to print formatted text and (OLE)objects. Gettext returns the text, but it loses its formatting parameters. My problem was specifing the rectangle were to print and making sure it fits on the page. The code i posted draws output differently in using print peview and when printing. I will however try using the code supplied by Box2020.
Anyways, thanks!

Siim Karus

Share this post


Link to post
Share on other sites