mschart图象打印的VC程序处理:
在VC中,实现打印比在VB中困难很多,在如题的问题中,VB的解决方案很简单:
VB的解决方案:
原理:mschart.editcopy(拷贝控件显示图象)
printer.print mschart.openclipboard(VB语句解释)(打印剪贴板内容)
但在VC中,打印不是一件容易的事,我把在网友帮助下解决的Mschart控件的打印实现源码给大家贴出来,作为参考
具体实现,
HANDLE hDib;
HPALETTE hPalette;
m_mschart.GetPlot().GetBackdrop()
.GetFill().GetBrush().GetFillColor().Set(255, 255, 255);
m_mschart.GetPlot().GetBackdrop().GetShadow().SetStyle(FALSE);//阴影
m_mschart.EditCopy();
if(OpenClipboard())//m_mschart.
{
hDib = GetClipboardData(CF_DIB);
hPalette = (HPALETTE)GetClipboardData(CF_PALETTE);
CloseClipboard();
}
else
return;
m_mschart.GetPlot().GetBackdrop()
.GetFill().GetBrush().GetFillColor().Set(192, 192, 192);
m_mschart.GetPlot().GetBackdrop().GetShadow().SetStyle(TRUE);//阴影
CPrintDialog mypd(FALSE);// 构造打印设置对话框对象
mypd.GetDefaults(); // 检取缺省打印设备的设备环境
DEVMODE *dm = mypd.GetDevMode();// 提取缺省打印机的DEVMODE数据
dm->dmOrientation = 2;
// dm->dmPrintQuality = 1200;
PRINTDLG pd;
{
pd.lStructSize = sizeof(PRINTDLG);
pd.Flags = PD_RETURNDC;
pd.hDC = NULL;
pd.hwndOwner = NULL;
pd.hInstance = NULL;
pd.nMaxPage = 256;
pd.nMinPage = 1;
pd.nFromPage = 1;
pd.nToPage = 256;
pd.nCopies = 1;
pd.hDevMode = dm;
pd.hDevNames = NULL;
}
if(!PrintDlg(&pd))
{
return;
}
ASSERT(pd.hDC != NULL);
CDC pDC;
pDC.Attach(pd.hDC);
int nHorRes = GetDeviceCaps(pd.hDC, HORZRES);//返回设备的物理显示宽度mm
int nVerRes = GetDeviceCaps(pd.hDC, VERTRES);//返回设备的物理显示高度mm
if(pd.hDC != NULL)
{
DOCINFO di;
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = "图象打印";
di.lpszOutput = (LPTSTR)NULL;
di.lpszDatatype = (LPTSTR)NULL;
di.fwType = 0;
pDC.StartDoc(&di); file://通知打印机驱动程序执行一新的打印任务
pDC.StartPage(); file://通知打印机驱动程序打印新页
SetMapMode(pd.hDC, MM_TEXT);//设置当前影射模式为:单位0.001英寸
CRect rectPrint(0, 0, nHorRes, nVerRes);
file://打印图形
BITMAPINFO* pbi = (BITMAPINFO*)GlobalLock(hDib);
if (pbi != NULL)
{
BYTE* Data = (BYTE*)(pbi->bmiColors);
if (pbi->bmiHeader.biBitCount <= 8)
{
int nColor = pbi->bmiHeader.biClrUsed
? pbi->bmiHeader.biClrUsed : (1<<pbi->bmiHeader.biBitCount);
Data += sizeof(RGBQUAD) *nColor;
}
if (hPalette != NULL)
SelectPalette(pd.hDC, hPalette, TRUE);
StretchDIBits(pd.hDC, rectPrint.left, rectPrint.top, rectPrint.Width(),
rectPrint.Height(), 0, 0, pbi->bmiHeader.biWidth,
pbi->bmiHeader.biHeight, Data, pbi, DIB_RGB_COLORS, SRCCOPY);
GlobalUnlock(hDib);
}
EndPage(pd.hDC); file://通知打印机驱动程序页结束
EndDoc(pd.hDC);
pDC.DeleteDC();
}
本文地址:http://com.8s8s.com/it/it2041.htm