//印刷初期化API
void WINAPI InitPrinterSet(LPPRINTDLG pPd){
pPd->lStructSize = sizeof(PRINTDLG);
DWORD dwFlags = pPd->Flags;
dwFlags = dwFlags ^ (PD_USEDEVMODECOPIESANDCOLLATE | PD_HIDEPRINTTOFILE);
dwFlags |= PD_NOSELECTION | PD_NOPAGENUMS;
pPd->Flags = dwFlags;
}
//画像印刷API
LONG WINAPI CDCToPRinterCDC(CDC *pFromDC, CBitmap *pFromBmp, int nFromX, int nFromY, int nWidth, int nHeight,
CDC* pPrinterCD, int nPrinterX, int nPrinterY){
//-------------------------------------------------------------------------
//APIの判定準備
LONG lFlug = pPrinterCD->GetDeviceCaps(RASTERCAPS);
BOOL bBitBlt = (lFlug & RC_BITBLT) == RC_BITBLT;
BOOL bStretchBlt = (lFlug & RC_STRETCHBLT) == RC_STRETCHBLT;
BOOL bStretchDIBits = (lFlug & RC_STRETCHDIB) == RC_STRETCHDIB;
BOOL bSetDIBitsToDevice = (lFlug & RC_DIBTODEV) == RC_DIBTODEV;
BOOL bOK;
BITMAP bmp;
int x = 0;
HBITMAP hBmp;
try{
//-------------------------------------------------------------------------
//BITMAP構造体を用意
memset(&bmp, 0, sizeof(BITMAP));
hBmp = *pFromBmp;
pFromBmp->GetBitmap(&bmp);
}catch (CException *e){
ShowLastError(GetLastError());
char *pError = new char[MAX_PATH];
e->GetErrorMessage(pError, MAX_PATH);
AfxMessageBox(pError, MB_OK);
return CDCPRN_ERROR_0001;
}
//-------------------------------------------------------------------------
// バッファの1ラインの長さを計算
// pixCnt は 画面の色のビット数をバイトに変換したもの。
// バッファの1ラインは4の倍数バイトでなければならない。
DWORD dwLength;
int pixCnt = bmp.bmBitsPixel;
int nPixX = 4;
BYTE *lpBits = NULL;
try{
if ((nWidth * pixCnt) % nPixX == 0){
dwLength = nWidth * pixCnt;
}else{
dwLength = nWidth * pixCnt + (nPixX - (nWidth * pixCnt) % nPixX);
}
lpBits = new BYTE[dwLength * nHeight];
if(lpBits == NULL){
AfxMessageBox("Printer buffer Create Error", MB_OK);
return -1;
}
}catch (CException *e){
ShowLastError(GetLastError());
char *pError = new char[MAX_PATH];
e->GetErrorMessage(pError, MAX_PATH);
AfxMessageBox(pError, MB_OK);
return CDCPRN_ERROR_0002;
}
try{
pFromBmp->GetBitmapBits(dwLength * nHeight, lpBits);
bmp.bmBits = lpBits;
}catch (CException *e){
ShowLastError(GetLastError());
char *pError = new char[MAX_PATH];
e->GetErrorMessage(pError, MAX_PATH);
AfxMessageBox(pError, MB_OK);
return CDCPRN_ERROR_0003;
}
//-------------------------------------------------------------------------
//パレット情報を取得
CPalette palette;
int nPltCnt;
PALETTEENTRY *pPEntry = NULL;
try{
palette.CreateHalftonePalette ( pFromDC );
nPltCnt = palette.GetEntryCount();
pPEntry = new PALETTEENTRY[nPltCnt];
if(pPEntry == NULL){
AfxMessageBox("Printer buffer Create Error", MB_OK);
delete lpBits;
return -1;
}
palette.GetPaletteEntries(0, nPltCnt-1, pPEntry);
}catch (CException *e){
ShowLastError(GetLastError());
char *pError = new char[MAX_PATH];
e->GetErrorMessage(pError, MAX_PATH);
AfxMessageBox(pError, MB_OK);
return CDCPRN_ERROR_0004;
}
//-------------------------------------------------------------------------
//BITMAPINFO構造体を用意
int nSize;
BITMAPINFO *pBmpInfo;
try{
nSize = sizeof(BITMAPINFOHEADER) + (256 * sizeof(RGBQUAD));
pBmpInfo = (BITMAPINFO*)new char[nSize];
memset(pBmpInfo, 0, sizeof(BITMAPINFO));
pBmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pBmpInfo->bmiHeader.biWidth = bmp.bmWidth;
pBmpInfo->bmiHeader.biHeight = bmp.bmHeight * -1;
pBmpInfo->bmiHeader.biPlanes = bmp.bmPlanes;
pBmpInfo->bmiHeader.biBitCount = bmp.bmBitsPixel;
pBmpInfo->bmiHeader.biCompression = BI_RGB;
pBmpInfo->bmiHeader.biSizeImage = 0;
pBmpInfo->bmiHeader.biXPelsPerMeter = 0;
pBmpInfo->bmiHeader.biYPelsPerMeter = 0;
pBmpInfo->bmiHeader.biClrUsed = 0;
pBmpInfo->bmiHeader.biClrImportant = 0;
for(int i = 0; i < nPltCnt; i++){
pBmpInfo->bmiColors[i].rgbRed = pPEntry[i].peRed;
pBmpInfo->bmiColors[i].rgbGreen = pPEntry[i].peGreen;
pBmpInfo->bmiColors[i].rgbBlue = pPEntry[i].peBlue;
}
delete pPEntry;
//DIBitmapを扱うAPIでも安全に利用できるように、CBitmapをデバイスコンテキストから切り離す。
pFromBmp->DeleteObject();
}catch (CException *e){
ShowLastError(GetLastError());
char *pError = new char[MAX_PATH];
e->GetErrorMessage(pError, MAX_PATH);
AfxMessageBox(pError, MB_OK);
return CDCPRN_ERROR_0005;
}
try{
//-------------------------------------------------------------------------
//Try BitBlt
if(bBitBlt){
//********************************************************************
bOK = BitBlt(pPrinterCD->m_hDC,
nPrinterX, nPrinterY, nWidth, nHeight,
pFromDC->m_hDC,
nFromX, nFromY, SRCCOPY);
if(bOK){
delete pBmpInfo;
delete lpBits;
return RC_BITBLT;
}
//********************************************************************
bOK = StretchBlt(pPrinterCD->m_hDC,
nPrinterX, nPrinterY, nWidth, nHeight,
pFromDC->m_hDC,
nFromX, nFromY, nWidth, nHeight, SRCCOPY);
if(bOK){
delete pBmpInfo;
delete lpBits;
return RC_STRETCHBLT;
}
//********************************************************************
bOK = StretchDIBits(pPrinterCD->m_hDC,
nPrinterX, nPrinterY, nWidth, nHeight,
nFromX, nFromY, nWidth, nHeight,
bmp.bmBits, pBmpInfo, DIB_RGB_COLORS, SRCCOPY);
if(bOK){
delete pBmpInfo;
delete lpBits;
return RC_STRETCHDIB;
}
//********************************************************************
bOK = SetDIBitsToDevice(pPrinterCD->m_hDC,
nPrinterX, nPrinterY, //転送先座標
nWidth, nHeight, //転送元長方形の幅
nFromX, nFromY, //転送元座標
0, //配列内の最初の走査行
nHeight, //走査行の数
bmp.bmBits, pBmpInfo,
DIB_PAL_COLORS);
if(bOK){
delete pBmpInfo;
delete lpBits;
return -1;
}
//********************************************************************
//描画に失敗
delete pBmpInfo;
delete lpBits;
return 0;
}
}catch (CException *e){
ShowLastError(GetLastError());
char *pError = new char[MAX_PATH];
e->GetErrorMessage(pError, MAX_PATH);
AfxMessageBox(pError, MB_OK);
return CDCPRN_ERROR_0006;
}
try{
//-------------------------------------------------------------------------
//Try StretchBlt
if(bStretchBlt){
//********************************************************************
bOK = StretchBlt(pPrinterCD->m_hDC,
nPrinterX, nPrinterY, nWidth, nHeight,
pFromDC->m_hDC,
nFromX, nFromY, nWidth, nHeight, SRCCOPY);
if(bOK){
delete pBmpInfo;
delete lpBits;
return RC_STRETCHBLT;
}
//********************************************************************
bOK = StretchDIBits(pPrinterCD->m_hDC,
nPrinterX, nPrinterY, nWidth, nHeight,
nFromX, nFromY, nWidth, nHeight,
bmp.bmBits, pBmpInfo, DIB_RGB_COLORS, SRCCOPY);
if(bOK){
delete pBmpInfo;
delete lpBits;
return RC_STRETCHDIB;
}
//********************************************************************
bOK = BitBlt(pPrinterCD->m_hDC,
nPrinterX, nPrinterY, nWidth, nHeight,
pFromDC->m_hDC,
nFromX, nFromY, SRCCOPY);
if(bOK){
delete pBmpInfo;
delete lpBits;
return RC_BITBLT;
}
//********************************************************************
bOK = SetDIBitsToDevice(pPrinterCD->m_hDC,
nPrinterX, nPrinterY, //転送先座標
nWidth, nHeight, //転送元長方形の幅
nFromX, nFromY, //転送元座標
0, //配列内の最初の走査行
nHeight, //走査行の数
bmp.bmBits, pBmpInfo,
DIB_PAL_COLORS);
if(bOK){
delete pBmpInfo;
delete lpBits;
return -1;
}
//********************************************************************
//描画に失敗
delete pBmpInfo;
delete lpBits;
return 0;
}
}catch (CException *e){
ShowLastError(GetLastError());
char *pError = new char[MAX_PATH];
e->GetErrorMessage(pError, MAX_PATH);
AfxMessageBox(pError, MB_OK);
return CDCPRN_ERROR_0007;
}
try{
//-------------------------------------------------------------------------
//Try StretchDIBits
if(bStretchDIBits){
//********************************************************************
bOK = StretchDIBits(pPrinterCD->m_hDC,
nPrinterX, nPrinterY, nWidth, nHeight,
nFromX, nFromY, nWidth, nHeight,
bmp.bmBits, pBmpInfo, DIB_RGB_COLORS, SRCCOPY);
if(bOK){
delete pBmpInfo;
delete lpBits;
return RC_STRETCHDIB;
}
//********************************************************************
bOK = StretchBlt(pPrinterCD->m_hDC,
nPrinterX, nPrinterY, nWidth, nHeight,
pFromDC->m_hDC,
nFromX, nFromY, nWidth, nHeight, SRCCOPY);
if(bOK){
delete pBmpInfo;
delete lpBits;
return RC_STRETCHBLT;
}
//********************************************************************
bOK = BitBlt(pPrinterCD->m_hDC,
nPrinterX, nPrinterY, nWidth, nHeight,
pFromDC->m_hDC,
nFromX, nFromY, SRCCOPY);
if(bOK){
delete pBmpInfo;
delete lpBits;
return RC_BITBLT;
}
//********************************************************************
bOK = SetDIBitsToDevice(pPrinterCD->m_hDC,
nPrinterX, nPrinterY, //転送先座標
nWidth, nHeight, //転送元長方形の幅
nFromX, nFromY, //転送元座標
0, //配列内の最初の走査行
nHeight, //走査行の数
bmp.bmBits, pBmpInfo,
DIB_PAL_COLORS);
if(bOK){
delete pBmpInfo;
delete lpBits;
return -1;
}
//********************************************************************
//描画に失敗
delete pBmpInfo;
delete lpBits;
return 0;
}
}catch (CException *e){
ShowLastError(GetLastError());
char *pError = new char[MAX_PATH];
e->GetErrorMessage(pError, MAX_PATH);
AfxMessageBox(pError, MB_OK);
return CDCPRN_ERROR_0008;
}
try{
//-------------------------------------------------------------------------
//Try SetDIBitsToDevice
if(bSetDIBitsToDevice){
//********************************************************************
bOK = SetDIBitsToDevice(pPrinterCD->m_hDC,
nPrinterX, nPrinterY, //転送先座標
nWidth, nHeight, //転送元長方形の幅
nFromX, nFromY, //転送元座標
0, //配列内の最初の走査行
nHeight, //走査行の数
bmp.bmBits, pBmpInfo,
DIB_PAL_COLORS);
if(bOK){
delete pBmpInfo;
delete lpBits;
return RC_DIBTODEV;
}
//********************************************************************
bOK = StretchDIBits(pPrinterCD->m_hDC,
nPrinterX, nPrinterY, nWidth, nHeight,
nFromX, nFromY, nWidth, nHeight,
bmp.bmBits, pBmpInfo, DIB_RGB_COLORS, SRCCOPY);
if(bOK){
delete pBmpInfo;
delete lpBits;
return RC_STRETCHDIB;
}
//********************************************************************
bOK = StretchBlt(pPrinterCD->m_hDC,
nPrinterX, nPrinterY, nWidth, nHeight,
pFromDC->m_hDC,
nFromX, nFromY, nWidth, nHeight, SRCCOPY);
if(bOK){
delete pBmpInfo;
delete lpBits;
return RC_STRETCHBLT;
}
//********************************************************************
bOK = BitBlt(pPrinterCD->m_hDC,
nPrinterX, nPrinterY, nWidth, nHeight,
pFromDC->m_hDC,
nFromX, nFromY, SRCCOPY);
if(bOK){
delete pBmpInfo;
delete lpBits;
return RC_BITBLT;
}
//********************************************************************
//描画に失敗
delete pBmpInfo;
delete lpBits;
return 0;
}
}catch (CException *e){
ShowLastError(GetLastError());
char *pError = new char[MAX_PATH];
e->GetErrorMessage(pError, MAX_PATH);
AfxMessageBox(pError, MB_OK);
return CDCPRN_ERROR_0009;
}
delete lpBits;
delete pBmpInfo;
return 0;
}
|