|
I am using an older public domain MFC version of Notepad that does not allow printing a selection of text. Does anyone know how to print just the selected portion of text?
|
|
|
|
|
I am trying to print the contents of a rich edit control over multiple pages using your code, but the last line of the page is getting clipped. Is there any way to avoid this? Can I retrieve individual lines from the contents of the rich edit control?
Edit: Nevermind, should have read beer's comment below first.
The world is not enough.
modified 20-May-12 5:22am.
|
|
|
|
|
I need to renter some RTF text (from clipboard) on a new image (bitmap.)
Any idea?
public Image GetRtfImage(string rtfText)
{
Bitmap image = new Bitmap(<small>size to hold the rtf</small>);
<small>???</small>
return image;
}
Horia Tudosie
|
|
|
|
|
I was struggling to print out embedded metafile drawings. In particular, if I used Page Breaks, the pages after the page break would only fill say a third of the page before starting a new page. It seems in this instance FormatRange() (used with RichEdit4.1) modifies the fmtRange.rc that is passed in. In other words, fmtRange.rc.bottom was continually getting smaller. To fix this, I have to call fmtRange.rc = page; inside the page processing loops, i.e.
while (last < m_Control.GetTextLength())
{
fmtRange.chrg.cpMin = last;
fmtRange.chrg.cpMax = -1;
fmtRange.rc = page;
last = m_Control.FormatRange(&fmtRange, FALSE);
fmtRange.chrg.cpMax = last;
pInfo->SetMaxPage(pInfo->GetMaxPage() + 1);
}
Cheers,
Phil.
|
|
|
|
|
Use the following code to create the device context in OnPreparePrinting
PRINTDLG pd;<br />
if ( !AfxGetApp()->GetPrinterDeviceDefaults(&pd) )<br />
return FALSE;<br />
<br />
HDC hdc = AfxCreateDC(pd.hDevNames, pd.hDevMode);<br />
if ( !hdc )<br />
return FALSE;<br />
<br />
dc.Attach(hdc);<br />
instead
AfxGetApp()->CreatePrinterDC(dc);
Dont forget to add
#include <afxpriv.h>
in stdafx.h
Herbert
|
|
|
|
|
If you add the code
if (::GetDeviceCaps(pDC->m_hDC, TECHNOLOGY) != DT_METAFILE && pDC->m_hAttribDC != NULL)<br />
{<br />
::ScaleWindowExtEx(pDC->m_hDC,<br />
::GetDeviceCaps(pDC->m_hDC, LOGPIXELSX),<br />
::GetDeviceCaps(pDC->m_hAttribDC, LOGPIXELSX),<br />
::GetDeviceCaps(pDC->m_hDC, LOGPIXELSY),<br />
::GetDeviceCaps(pDC->m_hAttribDC, LOGPIXELSY), NULL);<br />
}
after
pDC->SaveDC();
the printpreview is correct.
See in VIEWRICH.CPP CRichEditView::PrintPage of MFC
Herbert
|
|
|
|
|
Hi. I am still a beginner in Visual C++. I use Visual C++ 2005 Express Edition. My application consists of listView and treeView, then it should be able to print the items in the listView, which display employee data, like company name, employee name, address, etc.
How to print all the data in the listView on a A4 paper? How to set the paper to A4?
I've read the help about using printDocument and printPreview component but I still don't understand.
Do you have a simple example in code?
Thank you very much.
|
|
|
|
|
I have discovered that the print method in this article doesn't work with Rich Edit Controls 2.0. The reason being that GetTextLength() doesn't return the number of characters as it should, instead it returns the number of bytes that the control contains! This causes FormatRange to return an incorrect value for the final iteration of the for-loop that determines the number of pages in the final print out.
The solution is to use GetTextLengthEx() instead. Using the flag GetTextLengthEx(GTL_PRECISE) worked for me.
|
|
|
|
|
i have scroll rich edit with data i need to print it in labelprinter and it is dialog base application
chetan
|
|
|
|
|
I had try to check whether the client rect can print whole text in the rich edit box or not. I use the return value of FormatRange(&fr, FALSE). It is the last index of printable text.
First time I got right value of last printable index. It is less than the string length. So I try to decrease every font size value of the rtf code by program.
I decrease the font size in rtf code and send it back to the rich edit box by the stream function. Then I try to check the last printable text index again. At this time, the last printable text index was not changed.
Is there any other things I forgot to do before call FormatRange(&fr, FALSE) again?
Please give me your opinion. Thank you very much.
|
|
|
|
|
How can I send an escape sequence to the printer?
I'm working with the C++ Builder and the VCL is not supporting escape sequences.
So I tried these snippet
char Help[1024];
sprintf(Help, "\x00c"); // escape sequnce for form feed
Escape(printer->Canvas->Handle,
PASSTHROUGH,
sizeof(Help),
Help,
NULL);
But the printer is not doing anything.
Thanks for your help!
|
|
|
|
|
hi all,
I am trying to get the output of the RTF control using the Device context using the Print method of the CWnd class .It is working fine with Screen Dc and showing the required output but when i am trying to get the output on the Printer by writing on the Device Context of the printer the output is not turning up on the Paper...
The code is just working fine it i write directly to the Printer Device using the FormatRange Structure But there is makes the Screen and Printer output differ a lot.
i hope someone can provide me with a solution for getting the Output of the RTF control on the screen and the printer in a similar fashion..
I am wrting the code that i am using to Write at the Passed DC as parameter.
void CRenderedRTF::DrawControl(CDC* pDC)
{
CRect& refRect = m_ptrControlInfo->GetRect();
// save the device context
pDC->SaveDC();
CClientDC dc(this);
pDC->SetMapMode(MM_ANISOTROPIC);
CSize sz(dc.GetDeviceCaps(LOGPIXELSX),
dc.GetDeviceCaps(LOGPIXELSY) );
pDC->SetWindowExt( sz );
sz = CSize( pDC->GetDeviceCaps(LOGPIXELSX),
pDC->GetDeviceCaps(LOGPIXELSY) );
pDC->SetViewportExt( sz );
CPoint oldPoint = pDC->SetWindowOrg(-refRect.left,-refRect.top);
Print(pDC,PRF_CLIENT | PRF_CHILDREN | PRF_OWNED);
pDC->SetWindowOrg(oldPoint.x,oldPoint.y);
// restore the device context
pDC->RestoreDC(-1);
}
The code tends to write at the Screen but fails to show the output on paper.
any help or comment is most welcome..
Sajal
|
|
|
|
|
CDC dc;
CRect page;
FORMATRANGE fmtRange;
long lLineWidth;
int last = 0;
CPrintDialog printDlg(FALSE);
//dlg.GetDefaults();//if we do not want we can use defult printer
if (printDlg.DoModal() == IDCANCEL) // Get printer settings from user
return;
dc.Attach(printDlg.GetPrinterDC()); // Get and attach a printer DC
dc.m_bPrinting = TRUE;
//
DOCINFO di; // Initialise print document details
::ZeroMemory (&di, sizeof (DOCINFO));
di.cbSize = sizeof (DOCINFO);
di.lpszDocName = "BUYHOUSE";
BOOL bPrintingOK = dc.StartDoc(&di); // Begin a new print job
// pInfo->SetMaxPage(0);
// get the page size in twips (1440 twips/inch)
page.left = 0;
page.top = 0;
page.right = ::MulDiv(dc.GetDeviceCaps(PHYSICALWIDTH),
1440, dc.GetDeviceCaps(LOGPIXELSX));
page.bottom = ::MulDiv(dc.GetDeviceCaps(PHYSICALHEIGHT),
1440, dc.GetDeviceCaps(LOGPIXELSY));
lLineWidth = ::MulDiv(dc.GetDeviceCaps(PHYSICALWIDTH),
1440, dc.GetDeviceCaps(LOGPIXELSX))
-dc.GetDeviceCaps(LOGPIXELSX);
CRect rect;//print rectange area
rect.top=dc.GetDeviceCaps(LOGPIXELSY)/2;
rect.bottom=page.bottom-dc.GetDeviceCaps(LOGPIXELSY);
rect.left=dc.GetDeviceCaps(LOGPIXELSX)/2;
rect.right=page.right-dc.GetDeviceCaps(LOGPIXELSX)/2;
// setup the format range attributes
fmtRange.hdc = dc.m_hDC;
fmtRange.hdcTarget = dc.m_hAttribDC;
fmtRange.rc = rect;
fmtRange.rcPage = page;
// determine the correct output for this page
// m_display_ctr.SetTargetDevice(dc, lLineWidth);
while (last < m_display_ctr.GetTextLength())
{
dc.StartPage();
fmtRange.chrg.cpMin = last;
fmtRange.chrg.cpMax = -1;
// measure the data that goes on this page if false
last = m_display_ctr.FormatRange(&fmtRange, TRUE);
fmtRange.chrg.cpMax = last;
m_display_ctr.DisplayBand(&rect);
dc.EndPage();
// pInfo->SetMaxPage(pInfo->GetMaxPage() + 1);//here is used for DOC view
}
m_display_ctr.FormatRange(NULL, FALSE);
if (bPrintingOK)
dc.EndDoc(); // end a print job
else
dc.AbortDoc();
// release the cached DC information stored in SetTargetDevice
dc.DeleteDC();
|
|
|
|
|
did not work for me, the while loop never quits sending 1000s of pages to printer
fixed by:
1) using m_display_ctr.GetTextLengthEx(GTL_PRECISE); in the while loop
2) m_display_ctr.FormatRange(NULL, FALSE); must be included in the while loop
now you have:
// determine the correct output for this page
while (last < m_display_ctr.GetTextLengthEx(GTL_PRECISE))
{
dc.StartPage();
fmtRange.chrg.cpMin = last;
fmtRange.chrg.cpMax = -1;
// measure the data that goes on this page if false
last = m_display_ctr.FormatRange(&fmtRange, TRUE);
fmtRange.chrg.cpMax = last;
m_display_ctr.DisplayBand(&rect);
dc.EndPage();
m_display_ctr.FormatRange(NULL, FALSE);
}
hope this helps.
|
|
|
|
|
how to print since a richText ?
|
|
|
|
|
How can I set all the text-colors to black when printing?
Looking for a good game?
Do it yourself.
GLBasic.com - you can do!
|
|
|
|
|
I have been working on this a while and to me it seems that none of this is need when the control is properly initialized using the two functions below (that are also copied from codeguru, RichEditGS sample) in OnInitialUpdate.
Anyone Agrees? All I did was call these two functions and leave ALL printing stuff unchanged from the defaults.
void CMyView::SetFontSize(int nPointSize)
{ CHARFORMAT cf;
cf.cbSize = sizeof(CHARFORMAT);
cf.dwMask = CFM_SIZE;
GetRichEditCtrl().GetSelectionCharFormat(cf);
nPointSize *= 20; // convert from points to twips
cf.yHeight = nPointSize;
cf.dwMask = CFM_SIZE;
GetRichEditCtrl().SetSelectionCharFormat(cf);
}
void CMyView::SetFontName(const CString strFontName)
{ CHARFORMAT cf;
cf.cbSize = sizeof(CHARFORMAT);
cf.dwMask = CFM_FACE;
GetRichEditCtrl().GetSelectionCharFormat(cf);
CString strName = strFontName.Left(31);
for (int i = 0; i <= strName.GetLength()-1; i++)
cf.szFaceName[i] = (char)(strName[i]);
cf.szFaceName[i]='\0';
cf.dwMask = CFM_FACE;
GetRichEditCtrl().SetSelectionCharFormat(cf);
}
|
|
|
|
|
Hm, I don't know exactly what you want to say. What is not needed?
As I see, you are using the CRichEditView class (?). I think CRichEditView does very much of the printing internally (including the correct stuff for the preview). CRichEditView can be seen as a wrapper for the CRichEditCtrl.
But I think the main problem in this article is that we (and I ) want to use only the CRichEditCtrl, e.g. in a dialog or in a CFormView. And the printing support of CRichEditCtrl is very limited.
|
|
|
|
|
After reading this article I made some investigations regarding the ugly print preview. In the implementation of the CRichEditView from Microsoft I found the solution.
The "trick" seems to be to scale the window extends correctly, before you do the output.
<br />
if (::GetDeviceCaps(pDC->m_hDC, TECHNOLOGY) != DT_METAFILE && pDC->m_hAttribDC != NULL)<br />
{<br />
::ScaleWindowExtEx(pDC->m_hDC,<br />
::GetDeviceCaps(pDC->m_hDC, LOGPIXELSX),<br />
::GetDeviceCaps(pDC->m_hAttribDC, LOGPIXELSX),<br />
::GetDeviceCaps(pDC->m_hDC, LOGPIXELSY),<br />
::GetDeviceCaps(pDC->m_hAttribDC, LOGPIXELSY), NULL);<br />
}<br />
Add this code before you do the printing. I inserted it at the beginning of the OnPrint method. Maybe there is a better place in a CView method during the pre-print phase.
With this code the print preview looks correct in my application. I hope soemone can add comments how it works in other applications, but I think it should work.
If it works the author should add this to the article example code
|
|
|
|
|
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
|
|
|
|
|