Печать рисунка winapi
Принтер usb laser jet pro mfp m125ra. Я подключаю принтер, вызываю диалог печати, принтер там отображается, жму ок и всё, дальше ничего не происходит.
Код:
PRINTDLG pd;
memset(&pd, 0, sizeof(pd));
pd.lStructSize = sizeof(pd);
pd.hwndOwner = hWnd;
pd.hDevMode = NULL;
pd.hDevNames = NULL;
pd.Flags = PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC;
pd.nCopies = 1;
pd.nFromPage = 0xFFFF;
pd.nToPage = 0xFFFF;
pd.nMinPage = 1;
pd.nMaxPage = 0xFFFF;
if (PrintDlg(&pd))
{
HDC printerDC = pd.hDC;
DOCINFO di;
memset(&di, 0, sizeof(di));
di.cbSize = sizeof(di);
StartDoc(printerDC, &di);
StartPage(printerDC);
Rectangle(printerDC, 100, 100, 200, 200);
EndPage(printerDC);
EndDoc(printerDC);
DeleteDC(pd.hDC);
}
memset(&pd, 0, sizeof(pd));
pd.lStructSize = sizeof(pd);
pd.hwndOwner = hWnd;
pd.hDevMode = NULL;
pd.hDevNames = NULL;
pd.Flags = PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC;
pd.nCopies = 1;
pd.nFromPage = 0xFFFF;
pd.nToPage = 0xFFFF;
pd.nMinPage = 1;
pd.nMaxPage = 0xFFFF;
if (PrintDlg(&pd))
{
HDC printerDC = pd.hDC;
DOCINFO di;
memset(&di, 0, sizeof(di));
di.cbSize = sizeof(di);
StartDoc(printerDC, &di);
StartPage(printerDC);
Rectangle(printerDC, 100, 100, 200, 200);
EndPage(printerDC);
EndDoc(printerDC);
DeleteDC(pd.hDC);
}
Необходимо установить в каких относительных величинах вы будете печатать: в миллиметрах, в 10 долях милиметра или в 100тых и тд и тп.
И самое главное перед каждым выводом делать преобразование координат в зависимости от ориентации бумаги.
Ниже маленький кусочек моего кода, который я написал много лет назад и работающий по сей день без модификаций...
Прошу прощения, но пишу исключительно на С с классами :)
Код:
//*************************************************************
BOOL XPrintGraphics::SetPortraitOrientation(HDC hdc, int * pWidth, int * pHeight)
{
PaperWidth = 10 * GetDeviceCaps(hdc,HORZSIZE);
PaperHeight = 10 * GetDeviceCaps(hdc,VERTSIZE);
SetMapMode(hdc,MM_LOMETRIC);
if( PaperWidth > PaperHeight )
{
PaperOrientation = XPG_ORIENTATION_LANDSCAPE_PORTRAIT;
if( pWidth ) *(pWidth) = PaperHeight;
if( pHeight ) *(pHeight) = PaperWidth;
}
else
{
PaperOrientation = XPG_ORIENTATION_PORTRAIT_PORTRAIT;
if( pWidth ) *(pWidth) = PaperWidth;
if( pHeight ) *(pHeight) = PaperHeight;
}
return TRUE;
}
//*************************************************************
BOOL XPrintGraphics::SetLandscapeOrientation(HDC hdc, int * pWidth, int * pHeight)
{
PaperWidth = 10 * GetDeviceCaps(hdc,HORZSIZE);
PaperHeight = 10 * GetDeviceCaps(hdc,VERTSIZE);
SetMapMode(hdc,MM_LOMETRIC);
if( PaperWidth > PaperHeight )
{
PaperOrientation = XPG_ORIENTATION_LANDSCAPE_LANDSCAPE;
if( pWidth ) *(pWidth) = PaperWidth;
if( pHeight ) *(pHeight) = PaperHeight;
}
else
{
PaperOrientation = XPG_ORIENTATION_PORTRAIT_LANDSCAPE;
if( pWidth ) *(pWidth) = PaperHeight;
if( pHeight ) *(pHeight) = PaperWidth;
}
return TRUE;
}
//*************************************************************
BOOL XPrintGraphics::ScreenToPrinter(LPPOINT pPoints, DWORD Count)
{
//****
DWORD i;
int exchange;
if( !pPoints ) return FALSE;
for( i=0; i<Count; i++ )
{
switch( PaperOrientation )
{
case XPG_ORIENTATION_DEFAULT:
case XPG_ORIENTATION_PORTRAIT_PORTRAIT:
case XPG_ORIENTATION_LANDSCAPE_LANDSCAPE:
(pPoints+i)->y = -((pPoints+i)->y);
break;
case XPG_ORIENTATION_PORTRAIT_LANDSCAPE:
case XPG_ORIENTATION_LANDSCAPE_PORTRAIT:
exchange = (pPoints+i)->x;
(pPoints+i)->x = (pPoints+i)->y;
(pPoints+i)->y = exchange - PaperHeight;
break;
}
}
return TRUE;
}
//*************************************************************
void XPrintGraphics::DrawRectangle(HDC hdc, int left, int top, int right, int bottom)
{
RECT draw;
draw.left = left;
draw.top = top;
draw.right = right;
draw.bottom = bottom;
ScreenToPrinter((LPPOINT)&draw,2);
MoveToEx(hdc,draw.left,draw.top,NULL);
LineTo(hdc,draw.right,draw.top);
LineTo(hdc,draw.right,draw.bottom);
LineTo(hdc,draw.left,draw.bottom);
LineTo(hdc,draw.left,draw.top);
}
BOOL XPrintGraphics::SetPortraitOrientation(HDC hdc, int * pWidth, int * pHeight)
{
PaperWidth = 10 * GetDeviceCaps(hdc,HORZSIZE);
PaperHeight = 10 * GetDeviceCaps(hdc,VERTSIZE);
SetMapMode(hdc,MM_LOMETRIC);
if( PaperWidth > PaperHeight )
{
PaperOrientation = XPG_ORIENTATION_LANDSCAPE_PORTRAIT;
if( pWidth ) *(pWidth) = PaperHeight;
if( pHeight ) *(pHeight) = PaperWidth;
}
else
{
PaperOrientation = XPG_ORIENTATION_PORTRAIT_PORTRAIT;
if( pWidth ) *(pWidth) = PaperWidth;
if( pHeight ) *(pHeight) = PaperHeight;
}
return TRUE;
}
//*************************************************************
BOOL XPrintGraphics::SetLandscapeOrientation(HDC hdc, int * pWidth, int * pHeight)
{
PaperWidth = 10 * GetDeviceCaps(hdc,HORZSIZE);
PaperHeight = 10 * GetDeviceCaps(hdc,VERTSIZE);
SetMapMode(hdc,MM_LOMETRIC);
if( PaperWidth > PaperHeight )
{
PaperOrientation = XPG_ORIENTATION_LANDSCAPE_LANDSCAPE;
if( pWidth ) *(pWidth) = PaperWidth;
if( pHeight ) *(pHeight) = PaperHeight;
}
else
{
PaperOrientation = XPG_ORIENTATION_PORTRAIT_LANDSCAPE;
if( pWidth ) *(pWidth) = PaperHeight;
if( pHeight ) *(pHeight) = PaperWidth;
}
return TRUE;
}
//*************************************************************
BOOL XPrintGraphics::ScreenToPrinter(LPPOINT pPoints, DWORD Count)
{
//****
DWORD i;
int exchange;
if( !pPoints ) return FALSE;
for( i=0; i<Count; i++ )
{
switch( PaperOrientation )
{
case XPG_ORIENTATION_DEFAULT:
case XPG_ORIENTATION_PORTRAIT_PORTRAIT:
case XPG_ORIENTATION_LANDSCAPE_LANDSCAPE:
(pPoints+i)->y = -((pPoints+i)->y);
break;
case XPG_ORIENTATION_PORTRAIT_LANDSCAPE:
case XPG_ORIENTATION_LANDSCAPE_PORTRAIT:
exchange = (pPoints+i)->x;
(pPoints+i)->x = (pPoints+i)->y;
(pPoints+i)->y = exchange - PaperHeight;
break;
}
}
return TRUE;
}
//*************************************************************
void XPrintGraphics::DrawRectangle(HDC hdc, int left, int top, int right, int bottom)
{
RECT draw;
draw.left = left;
draw.top = top;
draw.right = right;
draw.bottom = bottom;
ScreenToPrinter((LPPOINT)&draw,2);
MoveToEx(hdc,draw.left,draw.top,NULL);
LineTo(hdc,draw.right,draw.top);
LineTo(hdc,draw.right,draw.bottom);
LineTo(hdc,draw.left,draw.bottom);
LineTo(hdc,draw.left,draw.top);
}