|
I started to take a look at the possibilities on how to print the contents of a CRichtEditCtrl some days ago. I quickly recognized that the MS implementation of the CRichtEditCtrl printing support sucks.
Only some basic, and in my opinion mostly useless features if you develope a complex software. Not mentioned the laughable print preview.
After those shocking experiences I began to take a look at the MSDN Wordpad example and the implementation of the CRichEditView class.
Now I think that the source of many questions about printing an RTF control is in MS' implementation of the CRichEditView class. The source code is in the file VIEWRICH.CPP in the Visual Studio installation directory.
The printing implementation looks very interesting, but I am still at the beginning so I can not post any results yet.
Roger, thank you for your great work and this example
|
|
|
|
|
Hello,
I'm having trouble while printing more than one page. The first page prints correctly.
However, the second page prints its output plus the output of the first page on top.
The only changes i have from your origional code is that i use OnBeginPrinting instead of OnPreparePrinting because OnBeginPrinting already provides me with the CDC.
Thank You in Advance and Thank you for the article...
|
|
|
|
|
Nevermid,
I should have read the post "Beer" made it solves the problem nicely.
Thank you all.
|
|
|
|
|
hey,
if you paste a list of sequential numbers up to say 50
1 on each line
like so
1
2
3
4
5...
You will quicly see that when this project prints out page 2, page 1's numbers are still on the bottom. Is there any way to clear the DC? So that page 2's remaining space is white?
hey
|
|
|
|
|
here's the fix for that
find this in the OnPrint Function
for (UINT i = 0 ; i < pInfo->m_nCurPage ; i++)
{
fmtRange.chrg.cpMin = last;
fmtRange.chrg.cpMax = -1;
// measure the data that goes on this page
last = m_Control.FormatRange(&fmtRange, TRUE); // just measuring, not displaying
fmtRange.chrg.cpMax = last;
// when we drop out of the loop, we should print the correct part of the output for the current page
}
and replace it with this
for (UINT i = 0 ; i < pInfo->m_nCurPage ; i++)
{
fmtRange.chrg.cpMin = last;
fmtRange.chrg.cpMax = -1;
// measure the data that goes on this page
if (i < pInfo->m_nCurPage-1) {
last = m_Control.FormatRange(&fmtRange, FALSE); m_Control.FormatRange(NULL, FALSE); }
else { last = m_Control.FormatRange(&fmtRange, TRUE); }
fmtRange.chrg.cpMax = last;
// when we drop out of the loop, we should print the correct part of the output for the current page
}
That will make sure that multiple pages will not overlap on any whitespace on the last page
hey
|
|
|
|
|
is it Posible to Print richtext with in a given rectangle area
don't worry with the size of file..
is a simple thing i expliend in lot..
Trying: Edit,RichEdit control printing,
with in a rectangle as per they are drawn
in scroll view..
----
//****************************************************************************
// I want to draw the RichTextControl content in a specified rectangle on my printing page
// (as i done with editbox control) using FormatRange function with the different
// Font styles as in RichEditControl - HelpMe please!.
// DrawRichText(CDC *pDC) -> could u solve the problem that i have in this function ?..
//***********************************************************************
//RTCtrlPrint1View.h
public:
CRTCtrlPrint1Doc* GetDocument();
//My*************************************
void RTCtrlCreate();
void DrawRichText(CDC *pDC);
void DrawEditText(CDC *pDC);
CRichEditCtrl m_redit;
CRect m_redRect;
CEdit m_edit;
CRect m_edRect;
UINT r;
CFont font;
//***************************************
//RTCtrlPrint1View.cpp
//=======================
void CRTCtrlPrint1View::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
//edith box displaytext font
font.CreateFont(14,0,0,0,FW_NORMAL,FALSE,FALSE,
0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,
DEFAULT_PITCH|FF_MODERN,"Arial");
r=14;
m_edRect = CRect(50*r,-100*r,240*r,-700*r);
m_redRect = CRect(250*r,-100*r,750*r,-700*r);
CSize sizeTotal;
sizeTotal.cx = 850*r;
sizeTotal.cy = 1100*r;
SetScrollSizes(MM_TWIPS, sizeTotal); //MM_TWIPS
RTCtrlCreate();
}
void CRTCtrlPrint1View::OnDraw(CDC* pDC)
{
CRTCtrlPrint1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDC->Rectangle(1*r,1*r,800*r,-1100*r);//Print Area Rect
if(pDC->IsPrinting())
{
DrawEditText(pDC);//Simple Edit box text
DrawRichText(pDC);//My RichEdit box text
}
}
#define IDC_REDIT 4001 //defining a resource ID
#define IDC_EDIT 4002 //defining a resource ID
void CRTCtrlPrint1View::RTCtrlCreate() //creates both Rich edit & Edit Controls
{
CRect m_temp;
CClientDC dc(this);
OnPrepareDC(&dc);
//creating edit box in scroll view
m_temp = m_edRect;
dc.LPtoDP(m_temp);
m_edit.Create(WS_CHILD|WS_VISIBLE|WS_BORDER|ES_MULTILINE,m_temp,this,IDC_EDIT);
m_edit.SetFont(&font,FALSE);
m_edit.SetWindowText("Help Me! Thank u");
//creating rich edit box in scroll view
m_temp = m_redRect;
dc.LPtoDP(m_temp);
m_redit.Create(WS_CHILD|WS_VISIBLE|WS_BORDER|ES_MULTILINE,m_temp,this,IDC_REDIT);
//writing some text in richtext control
CString str;
str="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\froman\\fprq2\\fcharset0 Times New Roman;}{\\f1\\fswiss\\fprq2\\fcharset0 System;}}{\\colortbl ;\\red255\\green0\\blue0;\\red51\\green153\\blue102;\\red0\\green0\\blue255;}\\viewkind4\\uc1\\pard\\cf1\\i\\f0\\fs24 ThankYou\
\\cf0\\i0 \\cf2\\b for\
\\cf0\\b0 \\cf3 Trying\
\\cf0 !\\b\\f1\\fs20 \\par }";
EDITSTREAM es;
es.dwCookie = (DWORD)&str;
es.pfnCallback = RichEditStreamInCallback;
m_redit.StreamIn(SF_RTF,es);
}
void CRTCtrlPrint1View::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
CScrollView::OnPrepareDC(pDC, pInfo);
// pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetMapMode(MM_TWIPS);
}
void CRTCtrlPrint1View::DrawEditText(CDC *pDC)
{
CRect m_temp;
m_temp = m_edRect;
CString str;
m_edit.GetWindowText(str);
pDC->SetBkMode(TRANSPARENT);
pDC->Rectangle(m_temp);
//Draws with Fonts as we wish
pDC->DrawText(str,m_temp,DT_WORDBREAK|DT_EXPANDTABS|DT_NOPREFIX);
}
//****************************************************************************// I want to draw the RichTextControl content in a specified rectangle on my
// printing page (as i done with editbox control) using FormatRange function
// with the different Font styles as in RichEditControl - HelpMe please!.
// DrawRichText(CDC *pDC) -> could u solve the problem that i have in this
// function ?..
//****************************************************************************void CRTCtrlPrint1View::DrawRichText(CDC *pDC)
{
CRect m_temp;
FORMATRANGE fmtRange;
long lLineWidth;
m_temp = m_redRect;
// pDC->LPtoDP(m_temp);
pDC->Rectangle(m_temp);
lLineWidth = ::MulDiv(pDC->GetDeviceCaps(PHYSICALWIDTH), 1440,
pDC->GetDeviceCaps(LOGPIXELSX));
fmtRange.hdc = pDC->m_hDC;
fmtRange.hdcTarget = pDC->m_hDC;
fmtRange.rc = m_temp;
fmtRange.rcPage = m_temp;
fmtRange.chrg.cpMin = 0;
fmtRange.chrg.cpMax = -1;
m_redit.SetTargetDevice(pDC->m_hDC, lLineWidth);
m_redit.FormatRange(&fmtRange, TRUE);
}
Balaji
|
|
|
|
|
is it Posible to Print richtext with in a given rectangle area
don't worry with the size of file..
is a simple thing i expliend in lot..
Trying: Edit,RichEdit control printing,
with in a rectangle as per they are drawn
in scroll view..
----
//***************************************************************************************
// I want to draw the RichTextControl content in a specified rectangle on my printing page
// (as i done with editbox control) using FormatRange function with the different
// Font styles as in RichEditControl - HelpMe please!.
// DrawRichText(CDC *pDC) -> could u solve the problem that i have in this function ?..
//***************************************************************************************
//The partial code
//RTCtrlPrint1View.h
public:
CRTCtrlPrint1Doc* GetDocument();
//My*************************************
void RTCtrlCreate();
void DrawRichText(CDC *pDC);
void DrawEditText(CDC *pDC);
CRichEditCtrl m_redit;
CRect m_redRect;
CEdit m_edit;
CRect m_edRect;
UINT r;
CFont font;
//***************************************
//RTCtrlPrint1View.cpp
//=======================
void CRTCtrlPrint1View::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
//edith box displaytext font
font.CreateFont(14,0,0,0,FW_NORMAL,FALSE,FALSE,
0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,
DEFAULT_PITCH|FF_MODERN,"Arial");
r=14;
m_edRect = CRect(50*r,-100*r,240*r,-700*r);
m_redRect = CRect(250*r,-100*r,750*r,-700*r);
CSize sizeTotal;
sizeTotal.cx = 850*r;
sizeTotal.cy = 1100*r;
SetScrollSizes(MM_TWIPS, sizeTotal); //MM_TWIPS
RTCtrlCreate();
}
void CRTCtrlPrint1View::OnDraw(CDC* pDC)
{
CRTCtrlPrint1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDC->Rectangle(1*r,1*r,800*r,-1100*r);//Print Area Rect
if(pDC->IsPrinting())
{
DrawEditText(pDC);//Simple Edit box text
DrawRichText(pDC);//My RichEdit box text
}
}
#define IDC_REDIT 4001 //defining a resource ID
#define IDC_EDIT 4002 //defining a resource ID
void CRTCtrlPrint1View::RTCtrlCreate() //creates both Rich edit & Edit Controls
{
CRect m_temp;
CClientDC dc(this);
OnPrepareDC(&dc);
//creating edit box in scroll view
m_temp = m_edRect;
dc.LPtoDP(m_temp);
m_edit.Create(WS_CHILD|WS_VISIBLE|WS_BORDER|ES_MULTILINE,m_temp,this,IDC_EDIT);
m_edit.SetFont(&font,FALSE);
m_edit.SetWindowText("Help Me! Thank u");
//creating rich edit box in scroll view
m_temp = m_redRect;
dc.LPtoDP(m_temp);
m_redit.Create(WS_CHILD|WS_VISIBLE|WS_BORDER|ES_MULTILINE,m_temp,this,IDC_REDIT);
//writing some text in richtext control
CString str;
str="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\froman\\fprq2\\fcharset0 Times New Roman;}{\\f1\\fswiss\\fprq2\\fcharset0 System;}}{\\colortbl ;\\red255\\green0\\blue0;\\red51\\green153\\blue102;\\red0\\green0\\blue255;}\\viewkind4\\uc1\\pard\\cf1\\i\\f0\\fs24 ThankYou\
\\cf0\\i0 \\cf2\\b for\
\\cf0\\b0 \\cf3 Trying\
\\cf0 !\\b\\f1\\fs20 \\par }";
EDITSTREAM es;
es.dwCookie = (DWORD)&str;
es.pfnCallback = RichEditStreamInCallback;
m_redit.StreamIn(SF_RTF,es);
}
void CRTCtrlPrint1View::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
CScrollView::OnPrepareDC(pDC, pInfo);
// pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetMapMode(MM_TWIPS);
}
void CRTCtrlPrint1View::DrawEditText(CDC *pDC)
{
CRect m_temp;
m_temp = m_edRect;
CString str;
m_edit.GetWindowText(str);
pDC->SetBkMode(TRANSPARENT);
pDC->Rectangle(m_temp);
//Draws with Fonts as we wish
pDC->DrawText(str,m_temp,DT_WORDBREAK|DT_EXPANDTABS|DT_NOPREFIX);
}
//****************************************************************************// I want to draw the RichTextControl content in a specified rectangle on my
// printing page (as i done with editbox control) using FormatRange function
// with the different Font styles as in RichEditControl - HelpMe please!.
// DrawRichText(CDC *pDC) -> could u solve the problem that i have in this
// function ?..
//****************************************************************************void CRTCtrlPrint1View::DrawRichText(CDC *pDC)
{
CRect m_temp;
FORMATRANGE fmtRange;
long lLineWidth;
m_temp = m_redRect;
// pDC->LPtoDP(m_temp);
pDC->Rectangle(m_temp);
lLineWidth = ::MulDiv(pDC->GetDeviceCaps(PHYSICALWIDTH), 1440,
pDC->GetDeviceCaps(LOGPIXELSX));
fmtRange.hdc = pDC->m_hDC;
fmtRange.hdcTarget = pDC->m_hDC;
fmtRange.rc = m_temp;
fmtRange.rcPage = m_temp;
fmtRange.chrg.cpMin = 0;
fmtRange.chrg.cpMax = -1;
m_redit.SetTargetDevice(pDC->m_hDC, lLineWidth);
m_redit.FormatRange(&fmtRange, TRUE);
}
Balaji
|
|
|
|
|
I am trying to print the background color of the CRichEditControl. It is not working on my
W2K platform so I am filling the area to be printed with a bitmap of the appropriate color. This introduces a new problem on
W2k because now it will not print the text over the bitmap in a TRANSPARENT background mode.
I.E. Calling SetBkMode(TRANSPARENT); has no effect, the text has a white background over the bitmap!
GDI functions like TextOut() work as expected. It is the CRichEditControl's
FormatRange() that seems to be the problem.
|
|
|
|
|
I've the same problem ...
Has someone found a workaround ?
Thx
|
|
|
|
|
I made a copy some text from another application's richedit
control to control of the demo project. When I want to print
,it returns with "Failed to get printer DC" (trace message).
What is missing that I should add to demo project?
Thanks.
|
|
|
|
|
I have seen this problem in the past when either the application or the PC has no default printer setup for it. Having recently switched to XP, it has been much more common. Try selecting the "Print Setup" option first to see if that fixes the problem. If its not a problem with the default printer, you will have to post some code to let me better understand the problem.
Roger Allen
Sonork 100.10016
This is a multiple choice question, choose wisely
Why did the hedgehog cross the road?
A: To show he had guts?
B: To see his flat mate?
|
|
|
|
|
I already have default printer installed. Using w2000.
Nothing changed. Same trace message still.
Thanks.
|
|
|
|
|
i get the same error when using your project.
when i use 'print setup' first and then try to print or print preview, i get a debug assertation error in afxcmn.inl, line 687.
i'm actually trying to modify your code to print docs from a CRichEditView.
|
|
|
|
|
I had the same problem. Unfortunately, the printer I wish to print to may not be the default printer, so as far as I can tell, any computation made on the default printer would be irrelevant (e.g., my default printer is B&W 2400dpi, but the printer I want to print on is color, 1200dpi; and suppose my default printer uses standard paper but I want to use 11x17 in my color printer? Overall, I don't see how this can possibly work by assuming a default printer for the first computation).
Also, I don't get background color, and I can't figure out how to get page headings and footings, and I seem to get multiple copies of the same page instead of multiple pages.
|
|
|
|
|
The problem goes away when you use OnBeginPrinting instead of OnPreparePrinting, since there you'll get the DC passed in. Just replace "dc." by "pDC->" and remove the printer DC creation.
|
|
|
|
|
...im having the same problems! Actually, i've managed to have a good print preview of it; using a normal CDC::DrawText with font size 12, had the size as the RTF. The problem was on printing! I had to resize the entire output to match the printer (MM_ISOTROPIC) and everything works well except the RTF, which increased several times! I think it may have something to do with output DPI (dots per inch), although im setting the device target.
I came to a point where i dont know if im doing the right preview with the wrong printing or exactly the opposite!
Ill be happy to work on some sollution.![Smile | :)](https://codeproject.global.ssl.fastly.net/script/Forums/Images/smiley_smile.gif)
|
|
|
|
|