向高手求救,打印与打印预览不一致。主要是用到了SelectClipRgn函数,但打印时这个函数好像没有起任何作用。

jetzhang 2003-07-17 09:58:34
向高手求救,打印与打印预览不一致。主要是用到了SelectClipRgn函数,但打印时这个函数好像没有起任何作用。本来用SelectClipRgn设定画图区域,但这个函数在打印时没有起起任何效果,真的不知道该怎么办?
...全文
231 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
daylight1980 2003-08-08
  • 打赏
  • 举报
回复
生成位图贴到剪贴板里再打印啊。

代码如下:

BOOL CSyPasView::OnPreparePrinting(CPrintInfo* pInfo)
{
// make preparation to print
// order chart to copy itself onto clipboard

/////////////////////////////////
/*
功能:取得棒图显示的图形并打印,zxd,2003.8.8
*/

/////////////////////////////
if(m_iShowStyle == FVSS_BAR)
{
CBitmap bm;
CBitmap *pOld;
int Width,Height;
CDC tdc;
BITMAP btm;
DWORD size;
CDC dc;

RECT Rect;

pChartWnd->GetWindowRect(&Rect);

RECT Rect;

pView->m_wndToolBar.GetWindowRect(&Rect);

dc.CreateDC("DISPLAY",NULL,NULL,NULL);

Width=Rect.right-Rect.left;
Height=Rect.bottom-Rect.top;

bm.CreateCompatibleBitmap(&dc,Width,Height);

tdc.CreateCompatibleDC(&dc);
pOld=tdc.SelectObject(&bm);

tdc.BitBlt( 0 , 0 , Width, Height, &dc, Rect.left , Rect.top , SRCCOPY);
//BOOL BitBlt(int x,int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, DWORD dwRop );

tdc.SelectObject(pOld);
bm.GetBitmap(&btm);
size=btm.bmWidthBytes*btm.bmHeight;
LPSTR lpData=(LPSTR)GlobalAllocPtr(GPTR,size);

/////////////////////////////////////////////
BITMAPINFOHEADER bih;
bih.biBitCount=btm.bmBitsPixel;
bih.biClrImportant=0;
bih.biClrUsed=0;
bih.biCompression=0;
bih.biHeight=btm.bmHeight;
bih.biPlanes=1;
bih.biSize=sizeof(BITMAPINFOHEADER);
bih.biSizeImage=size;
bih.biWidth=btm.bmWidth;
bih.biXPelsPerMeter=0;
bih.biYPelsPerMeter=0;
///////////////////////////////////

GetDIBits(dc,bm,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS);

if (OpenClipboard()) //hWnd为程序窗口句柄
{
//清空剪贴板
EmptyClipboard();
//把屏幕内容粘贴到剪贴板上,bm为刚才的屏幕位图句柄
SetClipboardData(CF_BITMAP, bm);
//关闭剪贴板
// CloseClipboard();

}

if(IsClipboardFormatAvailable(CF_BITMAP))
{
// if(OpenClipboard())
{
m_hbitmap = (HBITMAP)::GetClipboardData(CF_BITMAP); CloseClipboard();
}
}

}

return DoPreparePrinting(pInfo);
}

//////////////////////////////////////


void CSyPasView::OnPrint(CDC *pDC, CPrintInfo *pInfo)
{

// Create a text for the title
sTitleHeader = "";
// Get the page size and boundaries
CRect rectPage = pInfo->m_rectDraw;

TEXTMETRIC tm;
CFont font;
CSize textSize;
int cyChar;

// Create the font we will be using
font.CreatePointFont(240, "Arial", pDC);
CFont *pOldFont=pDC->SelectObject(&font);

//Set Margin
rectPage.top+=rectPage.bottom/48;
rectPage.bottom-=rectPage.bottom/48;
rectPage.left+=200;
rectPage.right-=200;

// Get Text size in order to center
pDC->GetTextMetrics(&tm);
textSize=pDC->GetTextExtent(sTitleHeader);
cyChar = tm.tmHeight;

// Draw Text (centered)
pDC->TextOut(((rectPage.right+rectPage.left)/2)-(textSize.cx/2), rectPage.top, sTitleHeader);
rectPage.top += cyChar + cyChar / 4;

// Draw header line divider
pDC->MoveTo(rectPage.left, rectPage.top);
pDC->LineTo(rectPage.right, rectPage.top);

// Go to next line
rectPage.top += cyChar / 4;

if(m_hbitmap)
{
BITMAP bm;
::GetObject(m_hbitmap, sizeof(BITMAP), &bm);
CSize chartSize(bm.bmWidth, bm.bmHeight);

CDC dcMemory,dcScreen;
dcScreen.Attach(::GetDC(NULL));

// create "from" device context and select the loaded bitmap into it
dcMemory.CreateCompatibleDC(&dcScreen);
dcMemory.SelectObject(m_hbitmap);

// Print at 85% size within left/right margin
CSize printSize;
printSize.cx=(int)(rectPage.right*.85);
printSize.cy=printSize.cx/chartSize.cx*chartSize.cy;

// Print chart centered
pDC->StretchBlt( ((rectPage.right+rectPage.left)/2)-(printSize.cx/2), rectPage.top, printSize.cx, printSize.cy, &dcMemory, 0, 0, chartSize.cx, chartSize.cy, SRCCOPY);
dcMemory.DeleteDC();
}
// Revert and Destroy

pDC->SelectObject(pOldFont);
font.DeleteObject();
}

匆忙发贴,可能有错,改改吧
jetzhang 2003-07-18
  • 打赏
  • 举报
回复
生成位图我会,可怎么怎么样和打印预览窗口的连在一起呢?
fingerfox 2003-07-18
  • 打赏
  • 举报
回复
打印机有支持裁剪的么?没有吧!
jetzhang 2003-07-18
  • 打赏
  • 举报
回复
使用位图能有类型的示例代码吗?:)
jetzhang 2003-07-18
  • 打赏
  • 举报
回复
哪怎么办呢?
ld_sfzc 2003-07-18
  • 打赏
  • 举报
回复
打印机不支持SelectClipRgn.
ilovevc 2003-07-18
  • 打赏
  • 举报
回复
什么叫连在一起?
打印预览你也也可以使用位图
jetzhang 2003-07-17
  • 打赏
  • 举报
回复
高手高手呀,帮我看看吧,我都快急死了。:)
ilovevc 2003-07-17
  • 打赏
  • 举报
回复
在DC中生成位图再打印, 所有的打印机都应该支持位图.
jetzhang 2003-07-17
  • 打赏
  • 举报
回复
大家推荐一个不换硬件的方法吧。:):):)
jetzhang 2003-07-17
  • 打赏
  • 举报
回复
恐怕所有的打印机都不支持呢?:):):)哪不没法了。:)
zfr 2003-07-17
  • 打赏
  • 举报
回复
其实最简单的方法就是 : 换打印机

哈哈哈
jetzhang 2003-07-17
  • 打赏
  • 举报
回复
打印机设备有类似SelectClipRgn设置区域的函数吗?因为程序中已经多处引用了SelectClipRgn函数,我在想能不能找到类似这个的打印机支持的函数。
jetzhang 2003-07-17
  • 打赏
  • 举报
回复
确实在打印时SelectClipRgn函数返回失败。唉,有什么解决的方法吗?:)打印机不支持SelectClipRgn函数怎么办?
zfr 2003-07-17
  • 打赏
  • 举报
回复
可能是和你的打印机的Device context有关,调试一下

看看打印时调用SelectClipRgn的返回值

返回是error表明Device context is not valid.
ilovevc 2003-07-17
  • 打赏
  • 举报
回复
这是因为你的打印机不支持ClipRgn, 有个函数可以通过DC得到
打印机的capacity

GetDeviceCaps(CP_RECTANGLE)
或者
GetDeviceCaps(CP_REGION)
试试
jetzhang 2003-07-17
  • 打赏
  • 举报
回复
我是指实际用打印机打印,在屏幕上或是打印预览都非常正常。可就是实际打印时就不同了。我不解的是怎么打印预览和实际打印怎么会不相同呢?
jetzhang 2003-07-17
  • 打赏
  • 举报
回复
CRgn rgnDraw;
CRect rcTemp=rcDraw;
if(m_pObjWell->m_pDoc->m_bPrintPreview)
{
::DpToPrinterDp(pDC,rcTemp); //如果是打印的话改变其映射关系
}

//新建区域
rgnDraw.CreateRectRgn(rcTemp.left,rcTemp.top,rcTemp.right,rcTemp.bottom);

//选择区域
pDC->SelectClipRgn(&rgnDraw,1);

//开始画
//Draw ..........

//取消区域
pDC->SelectClipRgn(NULL);

说明:这段代码在屏幕显示和打印预览都是正确的,就是真实打印时SelectClipRgn函数好像没有起任何作用,不是只打印了区域里面的东东,而且打印了所有的东东。真是感到头疼不解!望高手指点。不胜感激!
luogucai 2003-07-17
  • 打赏
  • 举报
回复
在我的机器上好像有用
zfr 2003-07-17
  • 打赏
  • 举报
回复
把你的代码贴出来看看

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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