CXXXDlg::OnBtnprint() //响应答应按钮
{
CRect m_rect,framerect;
//获取对话框大小
GetWindowRect(m_rect);
//获取图片控件大小
m_Frame.GetWindowRect(framerect);
int x,y;
x=framerect.left-m_rect.left;
CDC* pImageDC=GetDC();
int formx,formy;
formx=pImageDC->GetDeviceCaps(LOGPIXELSX);
formy=pImageDC->GetDeviceCaps(LOGPIXELSY);
ReleaseDC(pImageDC);
DWORD dwflags=PD_PAGENUMS|PD_HIDEPRINTTOFILE|PD_SELECTION; //设置打印对话框风格
CPrintDialog m_printdlg(FALSE,dwflags,NULL); //创建打印对话框
if(m_printdlg.DoModal()==IDOK)
{
CDC pdc;
pdc.Attach(m_printdlg.GetPrinterDC());
//获取打印机与屏幕的分辨率
int printerx,printery;
printerx=pdc.GetDeviceCaps(LOGPIXELSX);
printery=pdc.GetDeviceCaps(LOGPIXELSY);
double ratex,ratey;
ratex=(double)printerx/formx;
ratey=(double)printery/formy;
CClientDC dc(this);
CBitmap bmp;
bmp.CreateCompatibleBitmap(&dc,framerect.Width()+x,framerect.Height()+x);
CDC imagedc;
imagedc.CreateCompatibleDC(&dc);
imagedc.SelectObject(&bmp);
imagedc.BitBlt(0,0,framerect.Width()+x,framerect.Height()+x,&dc,0,0,SRCCOPY);
BITMAP bmap;
bmp.GetBitmap(&bmap);
int panelsize=0;
//非真彩色位图
if(bmap.bmBitsPixel<16)
{
panelsize=pow(2,bmap.bmBitsPixel*sizeof(RGBQUAD));
}
BITMAPINFO* bInfo=(BITMAPINFO*)LocalAlloc(LPTR,sizeof(BITMAPINFO)+panelsize);
bInfo->bmiHeader.biClrImportant=0;
bInfo->bmiHeader.biBitCount=bmap.bmBitsPixel;
bInfo->bmiHeader.biCompression=0;
bInfo->bmiHeader.biHeight=bmap.bmHeight;
bInfo->bmiHeader.biPlanes=bmap.bmPlanes;
bInfo->bmiHeader.biSize=sizeof(BITMAPINFO);
bInfo->bmiHeader.biSizeImage=bmap.bmWidthBytes*bmap.bmHeight;
bInfo->bmiHeader.biWidth=bmap.bmWidth;
bInfo->bmiHeader.biXPelsPerMeter=0;
bInfo->bmiHeader.biYPelsPerMeter=0;
char *pData=new char[bmap.bmWidthBytes*bmap.bmHeight];
::GetDIBits(imagedc.m_hDC,bmp,0,bmap.bmHeight,pData,bInfo,DIB_RGB_COLORS);
//打印图像
pdc.StartDoc("print");
::StretchDIBits(pdc.m_hDC,50,50,(int)((framerect.Width()*ratex)+x),
(int)((framerect.Height()+x)*ratey),0,0,framerect.Width()+x,
framerect.Height()+x,pData,bInfo,DIB_RGB_COLORS,SRCCOPY);
pdc.EndDoc();
LocalFree(bInfo);
delete[] pData;
bmp.DeleteObject();
}
}