Click here to Skip to main content
15,889,216 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionProblem with OnDrawClipboard Pin
dSolariuM4-Aug-07 1:12
dSolariuM4-Aug-07 1:12 
QuestionTapi reconnection Pin
mehrdadov4-Aug-07 0:06
mehrdadov4-Aug-07 0:06 
QuestionVS 2003 Tab Controls Pin
Code_Ray3-Aug-07 13:08
Code_Ray3-Aug-07 13:08 
AnswerRe: VS 2003 Tab Controls Pin
Mark Salsbery3-Aug-07 13:37
Mark Salsbery3-Aug-07 13:37 
AnswerRe: VS 2003 Tab Controls Pin
Michael Dunn3-Aug-07 14:48
sitebuilderMichael Dunn3-Aug-07 14:48 
AnswerRe: VS 2003 Tab Controls Pin
bob169723-Aug-07 16:15
bob169723-Aug-07 16:15 
QuestionPrinting in MFC [modified] Pin
hxhl953-Aug-07 12:30
hxhl953-Aug-07 12:30 
AnswerRe: Printing in MFC Pin
bob169723-Aug-07 16:02
bob169723-Aug-07 16:02 
You can use your OnDraw() functions to print with if you stand on your head just right. Start an MFC (Visual C++ 6.0) Doc/View app deriving from CScrollView. Add the necessary code sections below using the ClassWizard and make sure they resemble the code below. The demo shows how to use the OnDraw for both drawing in a view and to the printer. It uses an arbitrary world coordinate system with MM_ISOTROPIC. It demonstrates how to let the OnDraw know which page is being displayed or printed so OnDraw can adapt it's drawing accordingly. It will fit the drawing to the output device while preserving the documents aspect ratio. I left the centering of the drawing in the device as an exercise for you. (Hint, center/align the viewport and the window with respect to each other).
I also did not reset the m_nPage after printing, error checking etc...

Let me know if I forgot something.
Hope this 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); // Allow scaling with aspect ratio preserved

// 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)
{
CTestPrintDoc* 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);
}



QuestionRe: Printing in MFC Pin
hxhl953-Aug-07 19:32
hxhl953-Aug-07 19:32 
AnswerRe: Printing in MFC Pin
bob169724-Aug-07 3:41
bob169724-Aug-07 3:41 
GeneralRe: Printing in MFC Pin
hxhl954-Aug-07 6:36
hxhl954-Aug-07 6:36 
AnswerRe: Printing in MFC Pin
bob169723-Aug-07 16:25
bob169723-Aug-07 16:25 
AnswerRe: Printing in MFC Pin
hxhl954-Aug-07 6:42
hxhl954-Aug-07 6:42 
GeneralRe: Printing in MFC Pin
bob169724-Aug-07 8:38
bob169724-Aug-07 8:38 
GeneralRe: Printing in MFC Pin
Bram van Kampen5-Aug-07 3:11
Bram van Kampen5-Aug-07 3:11 
AnswerRe: Printing in MFC Pin
hxhl955-Aug-07 9:56
hxhl955-Aug-07 9:56 
QuestionCreating Formatting Bar Pin
.NET- India 3-Aug-07 9:23
.NET- India 3-Aug-07 9:23 
QuestionRe: Creating Formatting Bar Pin
David Crow3-Aug-07 10:35
David Crow3-Aug-07 10:35 
AnswerRe: Creating Formatting Bar Pin
Hamid_RT3-Aug-07 18:44
Hamid_RT3-Aug-07 18:44 
QuestionCreate a wav file using existing wav files Pin
vikrant kpr3-Aug-07 7:56
vikrant kpr3-Aug-07 7:56 
AnswerRe: Create a wav file using existing wav files Pin
mid=57413-Aug-07 17:07
mid=57413-Aug-07 17:07 
AnswerRe: Create a wav file using existing wav files Pin
Karismatic3-Aug-07 20:38
Karismatic3-Aug-07 20:38 
QuestionAfxBeginThread() with a function in the main class Pin
Johpoke3-Aug-07 5:59
Johpoke3-Aug-07 5:59 
AnswerRe: AfxBeginThread() with a function in the main class Pin
led mike3-Aug-07 6:13
led mike3-Aug-07 6:13 
QuestionRe: AfxBeginThread() with a function in the main class Pin
David Crow3-Aug-07 6:15
David Crow3-Aug-07 6:15 

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.