SHARE
Facebook X Pinterest WhatsApp

Printing from CFormView

Download demo 54K Printing from CFormView There are several reasons for using a CFormView derived View Class, all of which are documented in the MFC documentation. As you may have found out, CFormView does not print the controls rendered in the View automatically. The code snippet below shows a quick way to get your CFormView […]

Written By
thumbnail
CodeGuru Staff
CodeGuru Staff
Aug 8, 1998
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More


Download demo 54K

Printing from CFormView


There are several reasons for using a CFormView derived View Class, all of which are documented in


the MFC documentation. As you may have found out, CFormView does not print the controls rendered in


the View automatically. The code snippet below shows a quick way to get your CFormView application


printing your screen, if that is all that you need. Please note, youll need a much better exception


handling mechanism than my example. Nevertheless, this example demonstrates a quick way to get a


printable CFormView View.

Our view class, called CPrintView is derived from CFormView; only 3 additional variables are needed
to make CPrintView printable:

CDC * m_pMemDC;  //A memory device context compatible with our printer DC.
CRect m_rect;    //To hold the dimensions of our printing area while scaling.
CBitmap * m_pBm; //Capture the screen image as a Bitmap

Thats it. Lets see this in action:
In our views contructor, we initialize our variables:

CPrintView::CPrintView() : CFormView(CPrintView::IDD)
{
	m_pMemDC = new CDC ;
	m_pBm = new CBitmap;

//{{AFX_DATA_INIT(CPrintView)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT

}

Next we override CPrintView::OnBeginPrinting(..) as follows:

void CPrintView::OnBeginPrinting(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	if (m_pMemDC->GetSafeHdc()) m_pMemDC->DeleteDC();
	m_pMemDC->CreateCompatibleDC(pDC);

CClientDC dc(this);
CRect rect;
GetClientRect(rect);
m_pMemDC->SetMapMode(MM_ANISOTROPIC);
m_pMemDC->SetWindowExt(dc.GetDeviceCaps(LOGPIXELSX),dc.GetDeviceCaps(LOGPIXELSY));
m_pMemDC->SetViewportExt(m_pMemDC->GetDeviceCaps(LOGPIXELSX),m_pMemDC->GetDeviceCaps(LOGPIXELSY));

if (m_pBm->GetSafeHandle()) m_pBm->DeleteObject();
m_pBm->CreateCompatibleBitmap(&dc,rect.Width(),rect.Height());
m_pMemDC->SelectObject(m_pBm);
dc.DPtoLP(rect); //Convert to Logical Coordinates
m_rect = rect; //Save Logical Coordinates
m_pMemDC->BitBlt(0,0,rect.Width(),rect.Height(),&dc,0,0,SRCCOPY);
}

Next we need to override CPrintView::OnPrint(..)

void CPrintView::OnPrint(CDC* pDC, CPrintInfo*)
{
	//The Following code scales the image based on printer resolution.
	pDC->SetMapMode(MM_ANISOTROPIC);
	pDC->SetWindowExt(m_pMemDC->GetDeviceCaps(LOGPIXELSX),m_pMemDC->GetDeviceCaps(LOGPIXELSY));
	pDC->SetViewportExt(pDC->GetDeviceCaps(LOGPIXELSX),pDC->GetDeviceCaps(LOGPIXELSY));
	pDC->StretchBlt(0,0,m_rect.Width(),m_rect.Height(),m_pMemDC,0,0,m_rect.Width(),m_rect.Height(),SRCCOPY);

}

Lastly, we need to handle the destruction of our variables:

CPrintView::~CPrintView()
{
	delete m_pMemDC; //CLEAN UP OUR VARIABLES
	delete	m_pBm;
}

Thats it. Vinay Desai, Ph.D.


Note from section manager:
with this system you can print what is visible in the client area of the mainframe; it’s a quick way
to obtain a printed snapshot of the mainframe screen and so can have trouble with MDI formviews
and cannot print what it’s not show (if your formview is bigger than the mainframe, only portions will
be printed; and that cannot be fixed with this approach). What is printed is influenced by the
actual window size.


Last updated: 11 July 1998

Recommended for you...

How To Make Windows 11 Faster
Enrique Stone
Nov 30, 2022
Working with the Windows Registry in C#
Joydip Kanjilal
Jun 17, 2022
Using Multiple Programming Languages to Create an ASP.NET Website
Tariq Siddiqui
Jun 11, 2022
Finding a Microsoft Office version with .NET
Hannes DuPreez
May 20, 2022
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.