Click here to Skip to main content
15,897,891 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionPrinting to a printer onto a form Pin
Henri7-Feb-08 3:07
Henri7-Feb-08 3:07 
GeneralRe: Printing to a printer onto a form Pin
Matthew Faithfull7-Feb-08 3:26
Matthew Faithfull7-Feb-08 3:26 
GeneralRe: Printing to a printer onto a form Pin
Mark Salsbery7-Feb-08 5:54
Mark Salsbery7-Feb-08 5:54 
QuestionRe: Printing to a printer onto a form Pin
Henri7-Feb-08 8:17
Henri7-Feb-08 8:17 
GeneralRe: Printing to a printer onto a form Pin
Mark Salsbery7-Feb-08 8:49
Mark Salsbery7-Feb-08 8:49 
QuestionRe: Printing to a printer onto a form Pin
Henri10-Feb-08 3:20
Henri10-Feb-08 3:20 
GeneralRe: Printing to a printer onto a form Pin
Mark Salsbery10-Feb-08 9:10
Mark Salsbery10-Feb-08 9:10 
GeneralRe: Printing to a printer onto a form Pin
bob169727-Feb-08 7:05
bob169727-Feb-08 7:05 
Here's a quick sample to plop into a new CScrollView doc/view app to see if the results are consistent using print preview. It's more of a WYSIWYG approach and simplifies printing because it uses the same drawing code to print that is used for the screen. Try this out in a new project then adapt what you need to your application.

NOTE: This was previous post of mine describing the print paging mechanism to someone but it demonstrates general printing. I hope it helps.

//CDocument members...

// Header file .h
protected:
CSize m_DocSize;

// Implementation file .cpp
CSize CYourDoc::GetDocSize() const
{
return m_DocSize;
}

CYourDoc::CYourDoc()
{
// TODO: add one-time construction code here
m_DocSize=CSize(2000,2800);
}

//CScrollView members...

// Header file .h
private:
int m_nPage;

// Implementation file .cpp
CYourView::CYourView()
{
// TODO: add construction code here
SetScrollSizes(MM_TEXT,CSize(0,0)); // Set arbitrary values

m_nPage=1;
}

/***************************************
NOTE: The pInfo parameter is uncommented
****************************************/
void CYourView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* pInfo)
{
// TODO: add extra initialization before printing
pInfo->SetMaxPage(3);
}

void CYourView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: Add your specialized code here and/or call the base class
m_nPage=pInfo->m_nCurPage;

CScrollView::OnPrint(pDC, pInfo);
}

void CYourView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
CScrollView::OnPrepareDC(pDC);

// TODO: Add your specialized code here and/or call the base class

// Set up the DC for the current scale factor
int nExtentX;
int nExtentY;
CSize sizeDoc;
CRect rectClient;

pDC->SetMapMode(MM_ISOTROPIC);

// Get pertinent rectangle data
GetClientRect(&rectClient);
sizeDoc=GetDocument()->GetDocSize();

sizeDoc.cy=(-sizeDoc.cy); // Y goes down as it increments
pDC->SetWindowExt(sizeDoc); // Window extent is size of document

// Calculate viewport extent
nExtentX=rectClient.Width();
nExtentY=(int)((nExtentX*sizeDoc.cy)/(sizeDoc.cx));

// What kind of device context do we have?
if (pDC->IsPrinting()==TRUE) {
pDC->SetViewportExt(pDC->GetDeviceCaps(HORZRES),-pDC->GetDeviceCaps(VERTRES));
} else {
// Context is for screen
pDC->SetViewportExt(nExtentX,nExtentY);
}
}

void CYourView::ResetScrollBars()
{
CSize sizeDoc;
CClientDC dc(this);

this->OnPrepareDC(&dc); // Update the device context

sizeDoc=GetDocument()->GetDocSize();
dc.LPtoDP(&sizeDoc); // Logical to device

this->SetScrollSizes(MM_TEXT,sizeDoc); // Update scrollbars
}

void CYourView::OnSize(UINT nType, int cx, int cy)
{
CScrollView::OnSize(nType, cx, cy);

// TODO: Add your message handler code here
ResetScrollBars();
}

void CYourView::OnDraw(CDC* pDC)
{
CYourDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here

CSize sizeDoc=pDoc->GetDocSize();
CRect rectOutline(0,0,sizeDoc.cx,sizeDoc.cy);
rectOutline.DeflateRect(10,10); // Ensure we can see it

LOGFONT logFont={0};
memcpy(logFont.lfFaceName,"Arial",6);
logFont.lfHeight=300;

CFont font;
font.CreateFontIndirect(&logFont);

CFont* pOldFont=pDC->SelectObject(&font);
CBrush* pOldBrush=(CBrush*)pDC->SelectStockObject(NULL_BRUSH);
CString sMessage;
sMessage.Format("You can add code to center the printout later\nPage %d",m_nPage);
pDC->DrawText(sMessage,&rectOutline,DT_CENTER|DT_WORDBREAK);

pDC->Rectangle(&rectOutline);

pDC->SelectObject(pOldFont);
pDC->SelectObject(pOldBrush);
}
GeneralSizing my dialog Pin
Sabry19057-Feb-08 1:02
Sabry19057-Feb-08 1:02 
QuestionRe: Sizing my dialog Pin
CPallini7-Feb-08 1:40
mveCPallini7-Feb-08 1:40 
GeneralRe: Sizing my dialog Pin
Iain Clarke, Warrior Programmer7-Feb-08 2:17
Iain Clarke, Warrior Programmer7-Feb-08 2:17 
GeneralRe: Sizing my dialog Pin
CPallini7-Feb-08 8:07
mveCPallini7-Feb-08 8:07 
GeneralRe: Sizing my dialog Pin
Schehaider_Aymen11-Apr-08 23:19
Schehaider_Aymen11-Apr-08 23:19 
QuestionRe: Sizing my dialog Pin
David Crow7-Feb-08 2:24
David Crow7-Feb-08 2:24 
GeneralRe: Sizing my dialog Pin
Hamid_RT10-Feb-08 21:45
Hamid_RT10-Feb-08 21:45 
GeneralPlaying audio file within a windows service Pin
MarcoNedwig7-Feb-08 0:23
MarcoNedwig7-Feb-08 0:23 
GeneralRe: Playing audio file within a windows service Pin
Iain Clarke, Warrior Programmer7-Feb-08 0:36
Iain Clarke, Warrior Programmer7-Feb-08 0:36 
GeneralRe: Playing audio file within a windows service Pin
David Crow7-Feb-08 2:25
David Crow7-Feb-08 2:25 
GeneralRe: Playing audio file within a windows service Pin
MarcoNedwig7-Feb-08 3:06
MarcoNedwig7-Feb-08 3:06 
Questionwhat is the standard way to give file name in FtpOpenFile() [modified] Pin
vptech197-Feb-08 0:18
vptech197-Feb-08 0:18 
QuestionRe: what is the standard way to give file name in FtpOpenFile() Pin
David Crow7-Feb-08 2:28
David Crow7-Feb-08 2:28 
General[Message Deleted] Pin
vptech197-Feb-08 3:25
vptech197-Feb-08 3:25 
QuestionRe: what is the standard way to give file name in FtpOpenFile() Pin
David Crow7-Feb-08 3:33
David Crow7-Feb-08 3:33 
GeneralRe: what is the standard way to give file name in FtpOpenFile() Pin
vptech197-Feb-08 4:25
vptech197-Feb-08 4:25 
QuestionRe: what is the standard way to give file name in FtpOpenFile() Pin
David Crow7-Feb-08 4:32
David Crow7-Feb-08 4:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.