1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| // Recherche de l'imprimante par defaut
PRINTDLG pd;
memset( &pd, 0, sizeof( pd ) );
pd.lStructSize = sizeof( pd );
pd.Flags = PD_RETURNDEFAULT | PD_RETURNDC;
if(! PrintDlg( &pd ))
{
cout << "Erreur recherche imprimante par defaut" << endl;
return;
}
HDC hPrinter = pd.hDC;
// Begin a print job by calling the StartDoc function.
DOCINFO di;
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = "Bitmap Printing Test";
di.lpszOutput = (LPTSTR) NULL;
di.lpszDatatype = (LPTSTR) NULL;
di.fwType = 0;
if (StartDoc(pd.hDC, &di) == SP_ERROR)
{
cout << "Erreur StardDoc" << endl;
return;
}
// Inform the driver that the application is about to begin
// sending data.
if (StartPage(pd.hDC) <= 0)
{
cout << "Erreur StartPage" << endl;
return;
}
HDC hdcScreen = CreateDC("DISPLAY", NULL, NULL, NULL);
int screenWidth = GetDeviceCaps(hdcScreen, HORZRES);
int screenHeight = GetDeviceCaps(hdcScreen, VERTRES);
int printerWidth = GetDeviceCaps(pd.hDC, HORZRES);
int printerHeight = GetDeviceCaps(pd.hDC, VERTRES);
// Calcul du fctaur de transformation Ecran -> Imprimante
float fScaleX = 0.0;
float fScaleY = 0.0;
float quota = 0.9f;
float fscale = 0.0;
fScaleX = printerWidth / screenWidth;
fScaleY = (printerHeight * quota) / screenHeight;
if (fScaleX <= fScaleY) fscale = fScaleX;
else fscale = fScaleY;
float dx = (printerWidth - screenWidth * fscale) / 2.0f;
float dy = printerWidth * (1.0 - quota);
xLeft = (int) dx;
yTop = (int) dy;
HDC hdcMem = CreateCompatibleDC(hdcScreen);
// Create a compatible bitmap for hdcScreen.
HBITMAP hbmScreen = CreateCompatibleBitmap(hdcScreen,
GetDeviceCaps(hdcScreen, HORZRES),
GetDeviceCaps(hdcScreen, VERTRES));
if (!SelectObject(hdcMem, hbmScreen))
{
cout << "Erreur SelectObject" << endl;
return;
}
// Use the StretchBlt function to scale the bitmap and maintain
// its original proportions (that is, if the bitmap was square
// when it appeared in the application's client area, it should
// also appear square on the page).
if(!StretchBlt(hPrinter, xLeft, yTop, (int) (screenWidth *
fscale), (int) (screenHeight * fscale),
hdcMem, 0, 0, (int) screenWidth, (int) screenHeight, SRCCOPY))
{
int err = GetLastError ();
cout << "Erreur StretchBlt = " << err << endl;
return;
}
DeleteDC(hdcMem);
// Impression d'un titre
// =====================
// Retrieve the width of the string that specifies the full path
// and filename for the file that contains the bitmap.
SIZE szMetric;
szMetric.cx = (long) 12;
szMetric.cy = (long) 5;
GetTextExtentPoint32(pd.hDC, "Ma copie d'écran",
titre.length (), &szMetric);
// Compute the starting point for the text-output operation. The
// string will be centered horizontally and positioned three lines
// down from the top of the page.
xLeft = ((cWidthPels / 2) - (szMetric.cx / 2));
yTop = (szMetric.cy * 3);
// Print the path and filename for the bitmap, centered at the top
// of the page.
TextOut(pd.hDC, xLeft, yTop, titre.chars (),
titre.length ());
// Fin de l'impression :
// Determine whether the user has pressed the Cancel button in the
// AbortPrintJob dialog box; if the button has been pressed, call
// the AbortDoc function. Otherwise, inform the spooler that the
// page is complete.
if (EndPage(pd.hDC) <= 0)
{
cout << "Erreur EndPage" << endl;
return;
}
// Inform the driver that document has ended.
if (EndDoc(pd.hDC) <= 0)
{
cout << "Erreur EndDoc" << endl;
return;
} |
Partager