MFC Printing with CDC just works on some Printers

1.4k views Asked by At

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?

1

There are 1 answers

0
hypnomaki On BEST ANSWER

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.