Click here to Skip to main content
13,899,687 members
Click here to Skip to main content
Add your own
alternative version


44 bookmarked
Posted 4 Aug 2002

How to print an ActiveX MSChart control

, 4 Aug 2002
Rate this:
Please Sign up or sign in to vote.
A demonstration of printing or print previewing a MsChart component
<!-- Article image -->

Sample Image - printchart.jpg

<!-- Add the rest of your HTML here -->


Just recently, I was given a task: "Create a report with a chart." Sounds simple, right? That was what I though too, at first. My first reaction was to use a 3rd party plug-in of some sort, such as Crystal Report. "Sorry, we don't have budget to use a third party item which requires royalty or additional fee." All righty then, how about exporting the data to an excel template? "Sorry, exporting a 10x30000 spread sheet on a Pentium 2-300 would take about an hour before printing." How about I code the report onto the printer's device context? "Great! We'll need that in 3 days. And make sure it has some flexibility for us do some custom configuration and filtering"

*Gasp* Okay, this doesn't leave me with too many choices, so I looked into the ActiveX component, MsChart. I've seen postings of people talking about MsChart in VC++ forums, but they were mostly questions without answers. This lead me to believe that it might not be a very good component. Furthermore, I've never been a very big far for ActiveX. Frankly, I find ActiveX pretty ugly; Just another nightmare for C++ coders like other COM objects. Usually, I am pretty good at avoiding ActiveX when I can, but this time I'm cornered. Searching my favorite VC++ forums came up either empty or unanswered questions when I entered "print + MsChart". Oh boy, this is going to take me forever to figure out myself, right?

Not really. Surprisingly, it only took 5 hours of my Saturday afternoon.

Getting Started

I have to admit, MsChart isn't as difficult to use as I though. Actually, it is fairly easy to incorporate it into your project or report.

First thing you must do is add the MsChart Component into your project. For this, you can refer to JL Colson's article Passing an Array of Values to the Visual C++ MSChart OCX. It's a really nice article to get you started with adding the chart control. Either that, or you can can try to compile the sample source code. However, you might have to run the .reg file enclosed in the zip file so that everything that must be in the registry is there. The only thing you must make sure is that MsChart must reside in a form, so it's easier to create a SDI project with a CFormView as your view base.

How to Print MsChart

Alright. Now let's get down to the code. First thing you would need to do is go into your resource's toolbar and change the printer icon from ID_FILE_PRINT to ID_FILE_PRINT_PREVIEW. This makes it easier for us to access the print preview without always having to go to the File Menu. Next, use the ClassWizard and add OnPrint to your CFormView.

In the header of your view class (i.e. PrintMyChart.h) add this private member:

    HBITMAP m_hbitmap;

Also be sure to use the ClassWizard to add a member variable m_chart to your MsChart component.

The actual logic to print the chart is actually quite simple. First, you tell the chart component to copy itself into the clipboard using m_chart.EditCopy(). Once it is in memory, create a DC and past the clipboard bitmap onto it and then transfer it to the printer's DC (pDC). Now, go to your view class and edit the two functions as shown below.

BOOL CPrintMyChartView::OnPreparePrinting(CPrintInfo* pInfo)
	// make preparation to print

	// order chart to copy itself onto clipboard

	// make sure the item in clipboard is a bitmap
			m_hbitmap = (HBITMAP)::GetClipboardData(CF_BITMAP);

	return DoPreparePrinting(pInfo);

void CPrintMyChartView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
	// Create a text for the title
	CString sTitleHeader=_T("My First Chart");

	// Get the page size and boundaries
	CRect rectPage = pInfo->m_rectDraw;

	CFont font;
	CSize textSize;
	int cyChar;

	// Create the font we will be using
	font.CreatePointFont(240, "Arial", pDC);
	CFont *pOldFont=pDC->SelectObject(&font);

	//Set Margin;

	// Get Text size in order to center
	cyChar = tm.tmHeight;

	// Draw Text (centered)
	      , sTitleHeader); += cyChar + cyChar / 4;

	// Draw header line divider

	// Go to next line += cyChar / 4;

		BITMAP bm;
		::GetObject(m_hbitmap, sizeof(BITMAP), &bm);
		CSize chartSize(bm.bmWidth, bm.bmHeight);

		CDC dcMemory,dcScreen;

		// create "from" device context and select the
		// loaded bitmap into it

		// Print at 85% size within left/right margin 
		CSize printSize;*.85);*;

		// Print chart centered
		pDC->StretchBlt( ((rectPage.right+rectPage.left)/2)-
				  0, 0,,, SRCCOPY);

	// Revert and Destroy


Alrighty then. You're done! Now go and design that killer application report you've been dying to do! :)


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Victor Porter
Web Developer Shohero Technologies Ltd
Taiwan Taiwan
Victor Porter is a software engineer who is in Taiwan learning martial arts in his virtually little free time. He also enjoys playing golf occasionally at 5:30 in the morning by sacrificing his dearly needed sleep. He is constantly racking his brains trying to decided if his next project should use his near-decade-experienced-C++ or his near-half-year-C# skills. An American from Pennsylvania, he is currently living in Taipei, Taiwan with his wife and lovely 6 months old daughter, Christine.

You may also be interested in...


Comments and Discussions

Generalconvert any file into tiff formatt Pin
mohan.iton2-Nov-09 19:43
groupmohan.iton2-Nov-09 19:43 
QuestionIs it possible to copy Chart Pin
Christopher Stratmann27-Jun-06 8:22
memberChristopher Stratmann27-Jun-06 8:22 
AnswerRe: Is it possible to copy Chart Pin
Ananth.tm13-Oct-06 9:54
memberAnanth.tm13-Oct-06 9:54 
QuestionGood Works But ,a Bug Exitting in it.... Pin
mrxwh2-Mar-06 19:24
membermrxwh2-Mar-06 19:24 
AnswerRe: Good Works But ,a Bug Exitting in it.... Pin
mrxwh7-Mar-06 20:46
membermrxwh7-Mar-06 20:46 
GeneralMuch better way to print MSChart Control Pin
Victor Ricklefs9-Jan-06 3:35
memberVictor Ricklefs9-Jan-06 3:35 
QuestionRe: Much better way to print MSChart Control Pin
Christopher Stratmann27-Jun-06 8:05
memberChristopher Stratmann27-Jun-06 8:05 
Generalmschart label Pin
edge22-Jan-06 2:57
memberedge22-Jan-06 2:57 
AnswerRe: mschart label Pin
Christopher Stratmann27-Jun-06 8:08
memberChristopher Stratmann27-Jun-06 8:08 
Generaltext over or above the columns Pin
Marc Soleda2-Aug-05 4:55
memberMarc Soleda2-Aug-05 4:55 
GeneralRe: text over or above the columns Pin
wengjay27-Oct-05 22:19
memberwengjay27-Oct-05 22:19 
GeneralOne Other Thing Pin
jlatham10-Apr-05 10:15
memberjlatham10-Apr-05 10:15 
QuestionClipboard Use? Pin
jlatham10-Apr-05 10:10
memberjlatham10-Apr-05 10:10 
Julihc5-Apr-04 18:57
memberJulihc5-Apr-04 18:57 
mingjwan29-Apr-04 16:45
membermingjwan29-Apr-04 16:45 
Qadddd6-May-04 6:31
memberQadddd6-May-04 6:31 
wangzhenguo30-Dec-09 21:38
memberwangzhenguo30-Dec-09 21:38 
Questionhow to work with legend in mschar Pin
miguelmedina2-Mar-04 4:37
membermiguelmedina2-Mar-04 4:37 
AnswerRe: how to work with legend in mschar Pin
Ananth.tm13-Oct-06 9:56
memberAnanth.tm13-Oct-06 9:56 
GeneralIs there any such graph which can be printed and drawn to Scale Pin
Kapoor Vikrant3-Oct-03 8:13
memberKapoor Vikrant3-Oct-03 8:13 
GeneralDIB Pin
smirre24-Apr-03 3:00
membersmirre24-Apr-03 3:00 
GeneralRe: DIB Pin
tree_house1-May-03 5:37
membertree_house1-May-03 5:37 
GeneralRe: DIB Pin
smirre5-May-03 21:08
membersmirre5-May-03 21:08 
Questionhow to do the print when chart is in a dialog? Pin
Qadddd4-Dec-02 13:21
memberQadddd4-Dec-02 13:21 
AnswerRe: how to do the print when chart is in a dialog? Pin
Victor Porter4-Dec-02 15:08
memberVictor Porter4-Dec-02 15:08 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190306.1 | Last Updated 5 Aug 2002
Article Copyright 2002 by Victor Porter
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid