MFC使用CDC进行打印仅适用于某些打印机 [英] MFC Printing with CDC just works on some Printers
问题描述
我在一个大型项目中实现一个打印功能,打印所谓的小工具(源自CWnd)。在Gadget类中,我创建了一个函数来渲染它到设备上下文
PrintPageContent(CDC * pDC,const CRect& ; rContent,int page,int numPages)
{
PrintWindow(pDC,PW_CLIENTONLY);
pDC-> Rectangle(rContent.left,rContent.top,rContent.right,rContent.bottom);
}
要轻松渲染小工具我想到了使用PrintWindow函数 https://msdn.microsoft.com/en-us/library/x51z0shh.aspx
什么是工作?
- PrintPreview中的所有内容
- 实际打印时的边框矩形
什么不工作?
- 在实际打印过程/打印到纸张的某些情况下,小工具不打印。
所以我试图通过PDF Creator打印,并通过我局域网中的3台本地打印机在2个不同的Windows机器(Win7,Win8)与不同的结果(他们似乎总是相同的,所以我不认为它的某种未初始化的成员)。非常奇怪的是,我在机器上有不同的结果,例如有一台打印机适用于PC A,但不是PC B。
我可以告诉你,只是不工作在我的应用程序,所以它不是一个驱动程序问题。打印普通文档,图像完全工作。正如我已经说过,边界总是打印出来的。
这可能是什么原因?你知道这种问题的任何案例吗?
提示:
作为解决方法,我试图通过BitBlt直接从UI的CPaintDC复制Gadget 。在这种情况下,我有同样的问题
创建了一个小测试项目以重新创建情境。
这是源代码
//创建成员
CDC pDC;
HDC hdc;
//获取打印机/打印机设置
LPCSTR buffer = NULL;
GetDefaultPrinterName(buffer);
hdc = CreateDC(WINSPOOL,buffer,NULL,NULL);
pDC.Attach(hdc);
pDC.m_bPrinting = TRUE;
//开始文档打印
pDC.StartDoc(TEST);
pDC.StartPage();
//渲染窗口
PrintWindow(& pDC,PW_CLIENTONLY);
//渲染框架矩形
CRect WindowRect;
GetClientRect(WindowRect);
WindowRect.MoveToXY(0,0);
CBrush brush;
brush.CreateSolidBrush(RGB(0,0,0));
pDC.FrameRect(WindowRect,& brush);
//完成打印
pDC.EndPage();
pDC.EndDoc();
我在这里面临同样的问题。相同的打印机正在为同一台电脑工作。
我认为唯一的问题可能是我创建HDC的行
hdcBuffer = CreateDC(WINSPOOL buffer,NULL,NULL);
我认为这个调用与连接的printWindow或bitblt p>
或者这可能是一个MFC错误?
MFC Bug。无论如何,打印机驱动程序未正确初始化。我试过几个解决方案,但不能得到这个工作。它真的失败了在不同的机器上的不同结果的最简单的例子。
I'm implementing a Printing Function in a big Project to print so called gadgets (derived from CWnd). In the Gadget Class I've created a Function to Render it to the Device Context
PrintPageContent(CDC * pDC, const CRect & rContent, int page, int numPages)
{
PrintWindow(pDC, PW_CLIENTONLY);
pDC->Rectangle(rContent.left,rContent.top, rContent.right, rContent.bottom);
}
To Render the Gadget easily I thought about using the PrintWindow Function https://msdn.microsoft.com/en-us/library/x51z0shh.aspx
What is always working?
- Everything in the PrintPreview
- The Border Rectangle when I'm Actually Printing
What isnt Working?
- the Gadget isnt printed in some cases of the "actual Printing Process" / "Printing to Paper".
So I tried to print via PDF Creator and via 3 Local Printers in my LAN on 2 Different Windows Machines (Win7, Win8) with different Results (they seem to be always the same so i don't think its some kind of uninitialized member). Whats very weird is that I have different Results on the Machines for example there is one Printer which works for PC A but not for PC B.
I can tell you that printing just wont work within my Application so it isn't a Driver Problem. Printing normal Documents, Images fully works. And as I already told the Border is always printed.
What could be the cause of this? Do you know any Cases of such kind of Problem?
Hint: As an workaround I tried to Copy the Gadget from the CPaintDC of the UI directly via BitBlt. In this case I have the Same Problem
To find the Issue I created a Small Test Project to recreate the Situation. Here Is the Source Code
//Create Members
CDC pDC;
HDC hdc;
//Get Printer/Printer Settings
LPCSTR buffer = NULL;
GetDefaultPrinterName(buffer);
hdc = CreateDC("WINSPOOL", buffer, NULL, NULL);
pDC.Attach(hdc);
pDC.m_bPrinting = TRUE;
//Start Document Printing
pDC.StartDoc("TEST");
pDC.StartPage();
//Render Window
PrintWindow(&pDC,PW_CLIENTONLY);
//Render Frame Rectangle
CRect WindowRect;
GetClientRect(WindowRect);
WindowRect.MoveToXY(0,0);
CBrush brush;
brush.CreateSolidBrush(RGB(0,0,0));
pDC.FrameRect(WindowRect, &brush);
// Finish Printing
pDC.EndPage();
pDC.EndDoc();
I'm facing the same Problems here. The Same Printers are working for the same PC's. I think the only Problem could be the Line where I create the HDC
hdcBuffer = CreateDC("WINSPOOL", buffer, NULL, NULL);
I think this call in Connection with the "printWindow" or "bitblt" is the problem.
Or could this be a MFC bug?
It looks like a MFC Bug. Somehow the Printer Driver isn't initialized Correctly. I tried several Solution's but wasn't able to got this working. It really fails in the most Simple Examples with different Results on Differnt Machines.
这篇关于MFC使用CDC进行打印仅适用于某些打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!