|
要实现界面抓图并打印,写了如下的代码:
HDC hScrDC, hMemDC;
int scr_width_pels, scr_height_pels;
BITMAPINFO RGB24BitsBITMAPINFO;
BITMAPFILEHEADER bmBITMAPFILEHEADER;
BYTE *lpBitmapBits = NULL;
HANDLE directBmp;
HGDIOBJ previousObject;
HDC hMemoryDC = NULL;
BITMAP stBitmap;
hScrDC = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
//获取屏幕的宽和高
scr_width_pels = GetDeviceCaps(hScrDC, HORZRES);
scr_height_pels = GetDeviceCaps(hScrDC, VERTRES);
hMemDC = CreateCompatibleDC(hScrDC);
//对RGB24BitsBITMAPINFO进行赋值,省略
....
//创建bmp
directBmp = CreateDIBSection(hMemDC, (BITMAPINFO*)&RGB24BitsBITMAPINFO, DIB_RGB_COLORS,
(void **) &lpBitmapBits, NULL, 0);
previousObject = SelectObject(hMemDC, directBmp);
BitBlt(hMemDC, 0, 0, RGB24BitsBITMAPINFO.bmiHeader.biWidth, RGB24BitsBITMAPINFO.bmiHeader.biHeight, hScrDC, 0, 0, SRCCOPY);
//对bmBITMAPFILEHEADER进行赋值,省略
...
//写文件进行图像保存
mStream = fopen(_T("\\windows\\screen.bmp"), "wb");
fwrite(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER), 1, mStream);
fwrite(&(RGB24BitsBITMAPINFO.bmiHeader), sizeof(BITMAPINFOHEADER), 1, mStream);
fwrite(lpBitmapBits, 3*scr_width_pels*scr_height_pels, 1, mStream);
fclose(mStream)
SelectObject(hMemDC,previousObject);
DeleteDC(hMemDC);
DeleteDC(hScrDC);
//进行打印
hPrinterDC = CreateDC(szDriver,NULL,szPort,&printDM)
//打印机对应参数进行赋值
.....
//创建内存dc
hMemoryDC = CreateCompatibleDC(NULL);
previousObject = SelectObject(hMemoryDC, directBmp);
//开始打印
StartDoc(hPrinterDC,&di);
StartPage(hPrinterDC);
//获取图像信息
GetObject( directBmp, sizeof(stBitmap), (PVOID)&stBitmap );
StretchBlt(hPrinterDC, 0, 0, stBitmap.bmWidth, stBitmap.bmHeight, hMemoryDC, 0, 0,stBitmap.bmWidth,stBitmap.bmHeight, SRCCOPY);
//结束打印
EndPage(hPrinterDC);
EndDoc(hPrinterDC);
SelectObject(hMemoryDC,previousObject);
DeleteDC(hMemoryDC);
DeleteDC(hPrinterDC);
DeleteObject(directBmp);
这个代码在windows ce 5.0下操作正常,保存和打印都是正常的,但是在windows ce 4.2下发现图像保存正确,但是在打印的时候,屏幕左边的部分正常打印,但是右边的一部分打印出来是黑块。不知道产生这个现象的原因是什么,如果要在ce4.2下使用,该怎么改进。谢谢!
|
|