Jump to content
Software FX Community

help! preview works, print fails

User (Legacy)

Recommended Posts


I've been working through this newsgroup for hints about printing my charts.

I control the reports in my program. Text and other drawn objects (boxes,

bitmaps, etc.) work fine, both printed and previewed. My app is developed in

VC++ and uses MFC. I'm using the following code to print a chart. When

previewing, the chart prints fine. When I go to the printer, I get a

application error and my program terminates. I've tried to set the

coordinates into the printer DC like suggested in the screen/printer

conversion, but it doesn't seem to help. Also, please look at my

CreateControl function. I found no examples of how to instantiate the object

on the page. This seemed to work for me. If I set the window property to

WS_VISIBLE, then weirdness ensued. Setting the property to WS_VISIBLE, and

using and IDC_CHART value from another dialog box that uses the chart object

seems to work fine. Suggestions would be helpful.

Thanks in advance,


// code...

// at this point, I have a printer DC (pDC) and a CRect object based on

printer coordinates.

m_ChartFX.CreateControl(__uuidof(ChartFX), "", WS_DISABLED, rect,

pDC->GetWindow(), IDC_CHART1,NULL);

m_pChartFX = m_ChartFX.GetControlUnknown();

if (m_bPreview)


CDC *sdc;

sdc = AfxGetMainWnd()->GetDC(); // Screen DC

int xScrRes = sdc->GetDeviceCaps(LOGPIXELSX); // Screen resolution (X)

int yScrRes = sdc->GetDeviceCaps(LOGPIXELSY) / 3; // Screen resolution (Y)

int xPrnRes = pDC->GetDeviceCaps(LOGPIXELSX); // Printer resolution (X)

int yPrnRes = pDC->GetDeviceCaps(LOGPIXELSY) / 3; // Printer resolution (Y)

int nPrnWidth = pDC->GetDeviceCaps(HORZRES); // Page size (X)

int nPrnHeight = pDC->GetDeviceCaps(VERTRES) / 3; // Page size (Y)


pDC->SaveDC(); // Save settings

// Maps to equivalent SCREN resolution










(CfxChartPaint) 0,0);

pDC->RestoreDC(-1); // Restore settings




// the example I found in this newsgroup went right to the Paint fn, that

failed, I tried to map the coordinates to ensure they were set properly,

that failed, I read in one posting that the MapMode should be MM_TEXT, that

failed. I used different coordinates for the print boundaries, that

failed -- I'm lost!

int yPrnRes = pDC->GetDeviceCaps(LOGPIXELSY) / 3;

int nPrnWidth = pDC->GetDeviceCaps(HORZRES);

int nPrnHeight = pDC->GetDeviceCaps(VERTRES) / 3;

pDC->SaveDC(); // Save settings







(CfxChartPaint) CPAINT_PRINT,0);

pDC->RestoreDC(-1); // Restore settings


Link to comment
Share on other sites

The code in KB article Q1381013. Drawing the chart into an MFC preview

window (from which you took the code for preview) works fine for me in both

print and print preview AS IS in an MFC application created from the

scratch. No other code was added.

Notice that this code is to be put in the OnPrint function NOT in OnDraw.

Attached is the MFC Application.

>Also, please look at my CreateControl function. I found no examples of how

to instantiate the object ...

This code indeed takes a control in a DIALOG and uses it. If you want to

create one from scratch, you can do so by doing:

m_ChartFX.CreateControl(__uuidof(ChartFX), NULL, WS_VISIBLE, rc, this,


This is the CCfxDynamic.cpp sample provided.

Notice that this creates a chart WINDOW. You can make it invissible if you

want (by removing WS_VISIBLE from the previous call). It is important to

notice that if the chart is not created at design-time, the license

information is not going to be stored and therefore, the program is not

going to work when deployed. This is explained in KB article "Q1381002.

Using Chart FX 98 in VC++ without MFC".

Here is how:

IClassFactory2 *pFactory2;



if (SUCCEEDED(hr =


ry2,(LPVOID FAR *) &pFactory2))) {

hr = pFactory2->CreateInstanceLic(NULL,NULL,IID_IUnknown,_bstr_t(L"<Your

License String Goes Here>"),(LPVOID FAR *) &pUnk);

if (SUCCEEDED(hr)) {

m_pChartFX = pUnk;





The sample program I sent you includes this code. Simply add your license

string to run it. You can obtain your License String by running : "Start ->

All Programs -> Chart FX Client Server -> About Chart FX Client Server".



Software FX

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...