我写了一个打印函数,在windows 2000 下调试通过 但在windows 98 下在CDC::EndPage() 返回-1 请高手执教。

James 2005-04-11 09:44:40
函数体如下:
CDC dc;
CTextList *t;
CLineList *l;
CTableList *r;
DOCINFO doc;
int x,y;
double ox,oy;

ox=m_oFFSETX*0.01;
oy=m_oFFSETY*0.01;

DebugToFile("GetDefaultPrinter");
CPrintDialog dlg(FALSE);
if (dlg.GetDefaults()==FALSE)
{
DebugToFile("无法获得默认打印机!");
return FALSE;
}
if (dc.Attach(dlg.GetPrinterDC())==FALSE)
{
DebugToFile("获得打印机失败!");
m_errorCode=-1;
return FALSE;
}

x=dc.GetDeviceCaps(LOGPIXELSX);
y=dc.GetDeviceCaps(LOGPIXELSY);
dc.SetMapMode(MM_TEXT);

memset(&doc, 0, sizeof(doc));
doc.fwType=DI_APPBANDING;
doc.lpszDocName=DocName;
doc.cbSize=sizeof(DOCINFO);

DebugToFile("StartDoc="+DocName);
if (dc.StartDoc(&doc)<0)
{
SetErrorCode("StartDocError=",GetLastError());
return FALSE;
}

DebugToFile("StartPage");
if (dc.StartPage()<0)
{
SetErrorCode("StartPageError=",GetLastError());
dc.AbortDoc();
return FALSE;
}
//输出

//输出结束
int retval;
CString str;
DebugToFile("EndPage");
retval=dc.EndPage();
str.Format("%ld",retval);
if (retval<0)
{
DebugToFile("EndPageError="+str);
dc.AbortDoc();
return FALSE;
}
DebugToFile("EndDoc");
retval=dc.EndDoc();

if (retval<0)
{
DebugToFile("EndDocError="+str);
dc.AbortDoc();
return FALSE;
}

ClearPro();
DebugToFile(DocName);
DebugToFile("");
return TRUE;
...全文
88 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
koko1998 2005-04-11
  • 打赏
  • 举报
回复
你在对话框中画图用OnPaint,那么打印就是得到printer的CDC然后,往上打啊
CDC *pDC;

//打印机的初始化设置
CPrintDialog dlg(FALSE,PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS | PD_HIDEPRINTTOFILE | PD_NOSELECTION,this);

CRuixingZJApp* pApp = static_cast<CRuixingZJApp*>( ::AfxGetApp() );
long Len;
WORD nWord = 0;
HANDLE hPrinter;

DEVMODE* pDevMode;
DEVNAMES* lpDevNames;
LPTSTR lpszDeviceName;

///////////////////////////////////////////////////////////////////
if(pApp->GetPrinterDeviceDefaults(&dlg.m_pd) == 0)
{
::AfxMessageBox("错误!当前没有连接打印机!",MB_OK);
return;
}
pDevMode = (DEVMODE*)GlobalLock(dlg.m_pd.hDevMode);
lpDevNames = (LPDEVNAMES)GlobalLock(dlg.m_pd.hDevNames);
lpszDeviceName = (LPTSTR )lpDevNames + lpDevNames->wDeviceOffset;

if (::OpenPrinter((char *) pDevMode->dmDeviceName, &hPrinter, NULL) == FALSE)
return;
Len = DocumentProperties(NULL, NULL, lpszDeviceName, pDevMode, NULL, 0);


pDevMode->dmPaperSize = DMPAPER_A4;
lstrcpy((char *)pDevMode->dmFormName,"A4 210 *297 mm");

Len = DocumentProperties(NULL, hPrinter, lpszDeviceName, NULL, pDevMode, DM_IN_BUFFER);
GlobalUnlock(dlg.m_pd.hDevMode);
GlobalUnlock(dlg.m_pd.hDevNames);

////////////////////////////////////////////////////////////////////////////////
if (dlg.CreatePrinterDC() == NULL)
return ;
if (dlg.DoModal() == IDOK)
{
m_pPrintDC.Attach(dlg.m_pd.hDC);
if(m_pPrintDC.GetSafeHdc() == NULL)
{
::AfxMessageBox("当前没有连接打印机!", MB_OK);
return;
}

pDC = &m_pPrintDC;
ASSERT_VALID( pDC );

DOCINFO di;
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = "微机监测实时数据表格";
di.lpszOutput = NULL;
di.lpszDatatype = NULL;
di.fwType = 0;


//取打印机的横方向和纵方向的分辨率
//即每英寸点数
cxInch = pDC->GetDeviceCaps(LOGPIXELSX);
cyInch = pDC->GetDeviceCaps(LOGPIXELSY);
//取打印纸张高度和宽度
nPageHeight = pDC->GetDeviceCaps(VERTRES);
nPageWidth = pDC->GetDeviceCaps(HORZRES);

m_CharWidth = (long)((float)(nPageWidth*37/40)/(float)80);
m_LineHeight = (long)((float)(nPageHeight*19/20)/(float)80);

//==========================================
CFont font,*oldFont,titleFont;

//设定新字体
m_lf.lfWidth = m_CharWidth;
m_lf.lfHeight = m_LineHeight;
m_lf.lfCharSet = GB2312_CHARSET;
m_lf.lfOutPrecision = 3;
m_lf.lfClipPrecision = 2;
m_lf.lfQuality = PROOF_QUALITY;
m_lf.lfPitchAndFamily = 49;
m_lf.lfEscapement = 0;
m_lf.lfOrientation = 0;
m_lf.lfUnderline = 0;
m_lf.lfPitchAndFamily = DEFAULT_PITCH|FF_ROMAN;
font.CreateFont(m_lf.lfHeight,
m_lf.lfWidth,
m_lf.lfEscapement,
m_lf.lfOrientation,
FW_NORMAL,
FALSE,
m_lf.lfUnderline,
0,
m_lf.lfCharSet,
m_lf.lfOutPrecision,
m_lf.lfClipPrecision,
m_lf.lfQuality,
m_lf.lfPitchAndFamily,
"宋体");
LOGFONT curLogFont, newLogFont;

//取当前字体
font.GetLogFont( &curLogFont );
newLogFont = curLogFont;

//保存旧字体
if (!titleFont.CreateFontIndirect(&newLogFont))
return;
oldFont = pDC->SelectObject(&titleFont);

int Height = 400;
int ye = 1;//第一页

if (pDC->StartDoc(&di) > 0)
{
//打印整页
while (ye < m_ye)
{
pDC->StartPage();
pDC->MoveTo(nPageWidth/20, m_LineHeight);
DrawTable(pDC, Height, ye, 35);
pDC->EndPage();
ye = ye + 1;
}

//没有数据
if ((m_ye == 1)&&(m_other == 0))
{return;}

//打印剩下的数据或不为一页的数据
if (((m_ye == 1)&&(m_other > 0))||(ye == m_ye))
{
pDC->StartPage();
pDC->MoveTo(nPageWidth/20, m_LineHeight);
DrawTable(pDC, Height, ye, m_other);
pDC->EndPage();
}
pDC->EndDoc();
}
pDC->SelectObject(oldFont);
m_pPrintDC.Detach();
}
koko1998 2005-04-11
  • 打赏
  • 举报
回复

CPrintDialog printDlg(FALSE);
void CPrintWndDlg::Print()
{
CDC printDC;
CPrintDialog printDlg(FALSE);
DOCINFO docInfo;

if(printDlg.DoModal()==IDOK)
{
HDC hDC;
hDC=printDlg.GetPrinterDC();
if(!printDC.Attach(hDC))
return;
}
else
{
printDC.Detach();
return;
}

memset(&docInfo,0,sizeof(docInfo));
docInfo.cbSize=sizeof(DOCINFO);
docInfo.lpszDocName="Print Window";
if(printDC.StartDoc(&docInfo)<=0)
{
printDC.Detach();
return;
}
printDC.StartPage();

//这里进行你的打印工作

printDC.EndPage();
printDC.EndDoc();

printDC.Detach();

}
上面的代码取得一个打印机的DC:-)
koko1998 2005-04-11
  • 打赏
  • 举报
回复
参考:
下面的代码取得一个DC的位图(GetDC取得一个窗口的DC)
HBITMAP GetSrcBit(HDC hDC,DWORD BitWidth, DWORD BitHeight)

{

HDC hBufDC;

HBITMAP hBitmap, hBitTemp;

hBufDC = CreateCompatibleDC(hDC);

hBitmap = CreateCompatibleBitmap(hDC, BitWidth, BitHeight);

hBitTemp = (HBITMAP) SelectObject(hBufDC, hBitmap);

StretchBlt(hBufDC, 0, 0, BitWidth, BitHeight,

hDC, 0, 0, BitWidth, BitHeight, SRCCOPY);

hBitmap = (HBITMAP) SelectObject(hBufDC, hBitTemp);

::DeleteDC(hBufDC);

return hBitmap;

}

下面的代码打印一个位图

/*


HDC hDC, 打印机dc

HBITMAP hBmp, 待打印位图

int iX, 位图的左上角点x坐标

int iY, 位图的左上角点y坐标

double dScaleX, 打印机分辨率和屏幕分辨率x方向的比值,在没有指定位图显示高度和宽度的时候用的上

double dScaleY, 打印机分辨率和屏幕分辨率y方向的比值,在没有指定位图显示高度和宽度的时候用的上

int iWidth=0, 位图的显示宽度

int iLength=0 位图的显示高度

*/

void Draw(HDC hDC,HBITMAP hBmp,int iX,int iY,double dScaleX=1.0,double dScaleY=1.0,int iWidth=0,int iLength=0)

{

HPALETTE hPal;

BITMAP bm;

BITMAPINFOHEADER bi;

LPBITMAPINFOHEADER lpbi;

DWORD dwLen;

HANDLE hDIB;

HANDLE handle;

HDC hDC1;

if(GetDeviceCaps(hDC,RASTERCAPS) & RC_PALETTE )

{

UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256);

LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];

pLP->palVersion = 0x300;

pLP->palNumEntries =GetSystemPaletteEntries( hDC, 0, 255, pLP->palPalEntry );

hPal=CreatePalette(pLP );

delete[] pLP;

}

if (hPal==NULL) hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);

::GetObject(hBmp,sizeof(bm),(LPSTR)&bm);

bi.biSize = sizeof(BITMAPINFOHEADER);

bi.biWidth = bm.bmWidth;

bi.biHeight = bm.bmHeight;

bi.biPlanes = 1;

bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel;

bi.biCompression = BI_RGB;

bi.biSizeImage = 0;

bi.biXPelsPerMeter = 0;

bi.biYPelsPerMeter = 0;

bi.biClrUsed = 0;

bi.biClrImportant = 0;

int nColors = (1 << bi.biBitCount);

if( nColors > 256 )

nColors = 0;

dwLen = bi.biSize + nColors * sizeof(RGBQUAD);

hDC1 = ::GetDC(NULL);

hPal = SelectPalette(hDC1,hPal,FALSE);

RealizePalette(hDC1);

hDIB = GlobalAlloc(GMEM_FIXED,dwLen);

if (!hDIB)

{

SelectPalette(hDC1,hPal,FALSE);

::ReleaseDC(NULL,hDC1);

DeleteObject(hPal);

return ;

}

lpbi = (LPBITMAPINFOHEADER)hDIB;

*lpbi = bi;

::GetDIBits(hDC1, hBmp, 0L, (DWORD)bi.biHeight,

(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);

bi = *lpbi;

if (bi.biSizeImage == 0)

bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)* bi.biHeight;

dwLen += bi.biSizeImage;

if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))

hDIB = handle;

else

{

GlobalFree(hDIB);

SelectPalette(hDC1,hPal,FALSE);

::ReleaseDC(NULL,hDC1);

DeleteObject(hPal);

return ;

}

lpbi = (LPBITMAPINFOHEADER)hDIB;

BOOL bGotBits = GetDIBits( hDC1, hBmp,0L,(DWORD)bi.biHeight,(LPBYTE)lpbi+ (bi.biSize + nColors * sizeof(RGBQUAD)),

(LPBITMAPINFO)lpbi,(DWORD)DIB_RGB_COLORS);

if( !bGotBits )

{

GlobalFree(hDIB);

SelectPalette(hDC1,hPal,FALSE);

::ReleaseDC(NULL,hDC1);

DeleteObject(hPal);

return;

}

if(iWidth==0||iLength==0)

{

iWidth=lpbi->biWidth;

iLength=lpbi->biHeight;

iWidth=(int)(dScaleX*iWidth);

iLength=(int)(iLength*dScaleY);

}

StretchDIBits(hDC,iX,iY,iWidth,iLength,0,0,lpbi->biWidth,lpbi->biHeight,(LPBYTE)lpbi // address for bitmap bits

+ (bi.biSize + nColors * sizeof(RGBQUAD)),(LPBITMAPINFO)lpbi,DIB_RGB_COLORS,SRCCOPY);

SelectPalette(hDC1,hPal,FALSE);

::ReleaseDC(NULL,hDC1);

DeleteObject(hDIB);

DeleteObject(hPal);

}
James 2005-04-11
  • 打赏
  • 举报
回复
我现在头晕的很,我无法知道到底是什么错误。General error到底指的是什么意思?我觉得我的程序没有写错,而且用标准的MFC类CDC来操作打印任务,应该是通用的。可windows98就是报这个错。是在是不明白,谁可以救救我啊。
luokun 2005-04-11
  • 打赏
  • 举报
回复
SP_ERROR General error.
我知道2k和xp有一些差异,但是98就不知道了。

16,550

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧