PrintWindow()有时只能工作 [英] PrintWindow() only works sometimes

查看:142
本文介绍了PrintWindow()有时只能工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作CWnd的屏幕截图,然后将其打印出来。我已经创建了一些用于测试目的的基本代码,但是如果我实际打印它有时只能工作(在某些打印机上与我正在打印的某些PC组合)。什么总是工作是直接绘制一个矩形到设备上下文,但当我调用printWindow它有时没有效果(它总是返回true)。我也不认为它是PC的驱动程序问题,因为我从来没有遇到任何问题,而且我还在另外2个Pc上进行了测试。



因此我打印时有2种情景:

1)我只看到一个矩形(白色/透明内部)

2)我看到窗口和它周围的矩形



在我的示例中,我创建了一个基本的MFC应用程序(DialogBased),只是添加了一个Button打印窗口。我还创建了另一个函数来获取默认打印机名称,但我不认为这是因为我没有在这里上传。



这是代码:

Hi, I want to make a Screenshot of a CWnd and then Print it out. I already created some basic Code for testing purposes but if I'm actually printing it only works sometimes (on some Printers in combination with some PC's I'm printing from). What's always working is to directly draw a Rectangle to the Device Context but when I'm calling the "printWindow" It sometimes has no effect (it always returns true). I also don't think that its a driver problem of the PC because I never had any problems and I also tested it on 2 more Pc's.

So there Are 2 Scenarios when I'm printing:
1) I only see a Rectangle (white/transparent inside)
2) I see the Window and a Rectangle around it

In my Example I created a Basic MFC App (DialogBased) and just added a Button to Print the Window. I also created another Function to get the Default Printer Name but I dont think thats the cause so I din't uploaded it here.

Here is the Code:

//Create Members
CDC pDC;
HDC hdc;

//Get Printer/Printer Settings
LPCSTR buffer = NULL;
GetDefaultPrinterName(buffer);
hdc = CreateDC(_T( "winspool" ), buffer, NULL, NULL);
pDC.Attach(hdc);

//Start Document Printing
pDC.StartDoc("test");
pDC.StartPage();

if (! ::PrintWindow(this->GetSafeHwnd(), pDC.GetSafeHdc(), PW_CLIENTONLY))
    AfxMessageBox("Error when calling \"PrintWindow()\"");

//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();





您是否知道导致问题的原因或我可以做什么作为解决方法?



Do you know what causes the problem or what could I do as an Workaround?

推荐答案

参见 https://msdn.microsoft.com/en-us/library/windows/desktop/dd162869(v = vs.85).aspx [ ^ ]。此函数向函数调用中的窗口发送 WM_PRINT 消息,因此该窗口负责对DC进行实际渲染。
See https://msdn.microsoft.com/en-us/library/windows/desktop/dd162869(v=vs.85).aspx[^]. This function sends a WM_PRINT message to the window in the function call, so it is that window's responsibility to do the actual rendering into the DC.


这篇关于PrintWindow()有时只能工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆