28. juni 2002 - 10:36
Der er
3 kommentarer og
1 løsning
Indstille margin når der printes
Kan man selv styre margins hvis man har en funktion der udskriver til printer? dvs. kodemæssigt indstille de margins man ellers kan sætte under file/page setup. Så vidt jeg har forstået kan man ikke gøre dette i vb, der kan man kun sætte en margin i forhold til den der i forvejen er i page setup. Derfor søger jeg svaret her under C++
Når man skal udskrive til en printer fra Windows (?) bruger man en device context (DC) ligesom til skærmen. DC'en indeholder nogle oplysninger om printerens udskriftsområde og oplysning. Det er disse oplysninger du skal bruge til at beregne hvor mange punkter du skal sætte margin til, for at sætte den i en bestemt bredde i cm.
Hvilket udviklingsmiljø bruger du og skal det fx være ren C++ eller..?
Her er et eksempel:
// Zero and then initialize the members of a DOCINFO structure.
memset( &di, 0, sizeof(DOCINFO) );
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = "Bitmap Printing Test";
di.lpszOutput = (LPTSTR) NULL;
di.lpszDataType = (LPTSTR) NULL;
di.fwType = 0;
// Begin a print job by calling the StartDoc function.
nError = StartDoc(pd.hDC, &di);
if (nError == SP_ERROR)
{
errhandler("StartDoc", hwnd);
goto Error;
}
// Inform the driver that the application is about to begin
// sending data.
nError = StartPage(pd.hDC);
if (nError <= 0)
{
errhandler("StartPage", hwnd);
goto Error;
}
// Retrieve the number of pixels-per-logical-inch in the
// horizontal and vertical directions for the display upon which
// the bitmap was created. These are likely the same as for
// the present display, so we use those values here.
hWinDC = GetDC(hWnd);
fLogPelsX1 = (float) GetDeviceCaps(hWinDC, LOGPIXELSX);
fLogPelsY1 = (float) GetDeviceCaps(hWindDC, LOGPIXELSY);
// Retrieve the number of pixels-per-logical-inch in the
// horizontal and vertical directions for the printer upon which
// the bitmap will be printed.
fLogPelsX2 = (float) GetDeviceCaps(pd.hDC, LOGPIXELSX);
fLogPelsY2 = (float) GetDeviceCaps(pd.hDC, LOGPIXELSY);
// Determine the scaling factors required to print the bitmap and
// retain its original proportions.
if (fLogPelsX1 > fLogPelsX2)
fScaleX = (fLogPelsX1 / fLogPelsX2);
else fScaleX = (fLogPelsX2 / fLogPelsX1);
if (fLogPelsY1 > fLogPelsY2)
fScaleY = (fLogPelsY1 / fLogPelsY2);
else fScaleY = (fLogPelsY2 / fLogPelsY1);
// Compute the coordinates of the upper left corner of the
// centered bitmap.
cWidthPels = GetDeviceCaps(pd.hDC, HORZRES);
xLeft = ((cWidthPels / 2) - ((int) (((float) bmih.biWidth)
* fScaleX)) / 2);
cHeightPels = GetDeviceCaps(pd.hDC, VERTRES);
yTop = ((cHeightPels / 2) - ((int) (((float) bmih.biHeight)
* fScaleY)) / 2);
// Use StretchDIBits 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 (StretchDIBits(pd.hDC, xLeft, yTop, (int) ((float) bmih.biWidth
* fScaleX), (int) ((float) bmih.biHeight * fScaleY), 0, 0,
bmih.biWidth, bmih.biHeight, lpBits, lpBitsInfo, iUsage,
SRCCOPY) == GDI_ERROR)
{
errhandler("StretchDIBits Failed", hwnd);
}
// Retrieve the width of the string that specifies the full path
// and filename for the file that contains the bitmap.
GetTextExtentPoint32(pd.hDC, ofn.lpstrFile,
ofn.nFileExtension + 3, &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, ofn.lpstrFile,
ofn.nFileExtension + 3);
// 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.
nError = EndPage(pd.hDC);
if (nError <= 0)
{
errhandler("EndPage", hwnd);
goto Error;
}
// Inform the driver that document has ended.
nError = EndDoc(pd.hDC);
if (nError <= 0)
errhandler("EndDoc", hwnd);
Error:
// Enable the application's window.
EnableWindow(hwnd, TRUE);
// Remove the AbortPrintJob dialog box.
DestroyWindow(hdlgCancel);
// Delete the printer DC.
DeleteDC(pd.hDC);
det ser godt ud, jeg er ikke lige på arbejde så derfor tester jeg det først senere på ugen, men tak for hjælpen, du får selvfølgelig dine velfortjente points