Printing Multiple Images (C++ 5.0 and later)
Take the following steps to add code that prints two images on one page. This code shows you how to control the size and position of images on the page.
1. |
Start with the project that you created in Loading and Displaying an Image. | |
2. |
Go to the Project WorkSpace and click the ResourceView tab. | |
3. |
Double-click Dialog and double-click IDD_TUTOR_DIALOG to bring up the application's dialog box. | |
4. |
Add a new button under the Cancel button. To do this, select the button control on the Controls toolbar. Then, click and drag to position the button on the dialog box. | |
5. |
Double-click the button and change IDC_BUTTON1 to IDC_PRINT. Also, set the Caption to &Print. | |
6. |
Press Ctrl-F4 to close all windows back to the Project Workspace. | |
7. |
Press Ctrl-W to go to the MFC Class Wizard; then do the following: | |
|
a. |
Click the Message Maps tab. |
|
b. |
In the Class Name combo box, select CTutorDlg. |
|
c. |
In the Object IDs list box, select IDC_PRINT. |
|
d. |
In the Messages list box, select BN_CLICKED. |
|
e. |
Click the Add function button. Choose OK for the default function name (OnPrint). |
|
f. |
Click the Edit Code button to start entering the code. |
8. |
Enter new code as follows: |
void CTutorDlg::OnPrint()
{
// TODO: Add your control notification handler code here
//Declare the variable for printed text
static TCHAR szMessage1[] = TEXT("This is a print example using LEADTOOLS ActiveX.");
//Declare the variables for pixel measurements
int TextHeightInPixels;
int UsableWidth;
int UsableHeight;
int MaxImageHeight;
//Declare the variables for sizing and positioning the image
long PrintLeft;
long PrintTop;
long PrintHeight;
long PrintWidth;
//Declare variables used for preserving aspect ratios
int WidthFactor;
int HeightFactor;
HDC hdcPrinter;
PRINTDLG printdlg;
int yPrinter;
DOCINFO docinfo;
SIZE size;
//Set the variables used for preserving the aspect ratio
HeightFactor = (int)m_LEADRasterView1.GetRaster().GetBitmapHeight();
WidthFactor = (int)m_LEADRasterView1.GetRaster().GetBitmapWidth();
// get the printer DC here
memset( &printdlg, 0, sizeof(printdlg) );
printdlg.lStructSize = sizeof(printdlg);
printdlg.Flags = PD_RETURNDC;
if( !PrintDlg(&printdlg) )
return;
if( (hdcPrinter = printdlg.hDC) == NULL )
{
if( printdlg.hDevMode ) GlobalFree(printdlg.hDevMode);
if( printdlg.hDevNames) GlobalFree(printdlg.hDevNames);
return;
}
docinfo.cbSize = sizeof(docinfo);
docinfo.lpszDocName = TEXT("Print example using LEADTOOLS ActiveX.");
docinfo.lpszOutput= NULL;
docinfo.fwType = 0L;
if( StartDoc(hdcPrinter, &docinfo) <= 0)
{
if( printdlg.hDevMode ) GlobalFree(printdlg.hDevMode);
if( printdlg.hDevNames) GlobalFree(printdlg.hDevNames);
DeleteDC(hdcPrinter);
}
StartPage(hdcPrinter);
//Set the pointer to an hourglass
HCURSOR hOldCursor = SetCursor(LoadCursor(NULL,IDC_WAIT));
//Get the page width and height in pixels (dots)
UsableWidth = GetDeviceCaps(hdcPrinter,HORZRES);
UsableHeight = GetDeviceCaps(hdcPrinter,VERTRES);
//Print a title at the top of the page
yPrinter = 0;
TextOut( hdcPrinter, 0, yPrinter, szMessage1, strlen(szMessage1));
//Get the maximum height of one image,
//assuming two equal-size images and space for 12 lines of text
GetTextExtentPoint32( hdcPrinter, szMessage1, strlen(szMessage1), &size );
TextHeightInPixels = (int)size.cy;
yPrinter += TextHeightInPixels;
MaxImageHeight = (UsableHeight - (12 * TextHeightInPixels)) / 2;
//Size and position the first image, preserving the aspect ratio.
//Check to see if using the maximum width will make the image too tall.
//Set the dimensions based on the result.
if ( ((UsableWidth * HeightFactor) / WidthFactor) < MaxImageHeight)
{
PrintLeft = 1; //This is for flush left. Use 0 for centering.
PrintTop = yPrinter;
PrintWidth = UsableWidth;
PrintHeight = (PrintWidth * HeightFactor) / WidthFactor;
}
else
{
PrintLeft = 1; //This is for flush left. Use 0 for centering.
PrintTop = yPrinter;
PrintHeight = MaxImageHeight;
PrintWidth = (PrintHeight * WidthFactor) / HeightFactor;
}
//Print the first image
m_LEADRasterView1.Render( (long)hdcPrinter, (float)PrintLeft, (float)PrintTop, (float)PrintWidth, (float)PrintHeight );
//Update the current printing position
yPrinter += PrintHeight;
//Leave a blank line, then print a caption for the next picture
yPrinter += TextHeightInPixels;
static TCHAR szMessage2[] = TEXT("This is a print example using LEADTOOLS ActiveX.");
TextOut( hdcPrinter, 0, yPrinter, szMessage2, strlen(szMessage2) );
yPrinter += TextHeightInPixels;
//Stretch the intensity of the bitmap for the second printing
//to show the difference between the second picture and the first
ILEADRasterProcess *pRasterProc=NULL;
CoCreateInstance(CLSID_LEADRasterProcess, NULL, CLSCTX_ALL,
IID_ILEADRasterProcess, (void**)&pRasterProc);
pRasterProc->StretchIntensity(m_LEADRasterView1.GetRaster());
pRasterProc->Release();
//Size and position the second image, preserving the aspect ratio.
//The coding is the same as for the first image.
if( ((UsableWidth * HeightFactor) / WidthFactor) < MaxImageHeight )
{
PrintLeft = 1; //This is for flush left. Use 0 for centering.
PrintTop = yPrinter;
PrintWidth = UsableWidth;
PrintHeight = (PrintWidth * HeightFactor) / WidthFactor;
}
else
{
PrintLeft = 1; //This is for flush left. Use 0 for centering.
PrintTop = yPrinter;
PrintHeight = MaxImageHeight;
PrintWidth = (PrintHeight * WidthFactor) / HeightFactor;
}
//Print the second image
m_LEADRasterView1.Render( (long)hdcPrinter, (float)PrintLeft, (float)PrintTop, (float)PrintWidth, (float)PrintHeight );
//Finish the page and finish the print job
EndPage( hdcPrinter );
EndDoc( hdcPrinter );
//Set the mouse pointer back to the default
SetCursor( hOldCursor );
if( printdlg.hDevMode ) GlobalFree(printdlg.hDevMode);
if( printdlg.hDevNames) GlobalFree(printdlg.hDevNames);
DeleteDC(hdcPrinter);
}
9. |
Rebuild and run the application. |