Click here to Skip to main content
15,883,868 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C++
//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?
Posted
Updated 28-Jun-15 20:35pm
v2

1 solution

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.
 
Share this answer
 
Comments
Michael Rampl 29-Jun-15 4:27am    
thx for the response,
I tried but with the same result

PrintWindow(&pDC ,PW_CLIENTONLY)
Richard MacCutchan 29-Jun-15 9:23am    
How do you think that will change anything? As I said, and the documentation explains, it is the receiving window that must do the rendering, it is not done by the system.
Michael Rampl 30-Jun-15 1:54am    
sry i think i miss unterstood you,
yet now i dont handle any messages at all. All i did was creating a new mfc dialog based app, added a button with the function code from above and a function to get the default printer.
The rendering as it is in my Application, is done by the default mfc code, i haven't touched it at all so my guess was that it would "just run" with the default implementation.
The only Thing thats related to rendering in my Code is the OnPaint default mfc override.

void CPrintingTestDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
Richard MacCutchan 30-Jun-15 3:28am    
You still need to handle the WM_PRINT message, the framework will not do it for you. You need to add a message map entry for this message, and write the implementation which displays the infomration on the DC.
Michael Rampl 30-Jun-15 4:21am    
so you mean something like this?
http://stackoverflow.com/questions/12000458/how-to-handle-wm-print-message-in-a-cwnd-using-mfc

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900