can i print a window without printer dialog?

Recommended Answers

All 7 Replies

ok.. almost done:

void print()
        {
            //getting the default printer name
            char szPrinterName[255];
            unsigned long lPrinterNameLength;
            GetDefaultPrinter( szPrinterName, &lPrinterNameLength );

            //getting printer DC
            HDC printerDC = CreateDC( "WINSPOOL",szPrinterName, NULL, NULL);

            //starting doc and pages:
            DOCINFO doc;
            doc.cbSize = sizeof(DOCINFO);
            doc.lpszDocName = "1st doc";
            StartDoc(printerDC, &doc);
            StartPage(printerDC);

            //draw on printer using GDI functions
            RECT windowRect;
            GetClientRect(hwnd,&windowRect);
            SetMapMode(printerDC, GetMapMode(GetDC(hwnd)));
            BitBlt(printerDC,0,0,windowRect.right,windowRect.bottom,GetDC(hwnd),0,0,SRCCOPY);

            //end pages and doc
            EndPage(printerDC);
            EndDoc(printerDC);

            //delete the printer DC
            DeleteDC(printerDC);
        }

my problem is: how can i convert pixels to printer scale mode(i don't know.. is realy dpi?)?

You are leaking by calling GetDC(hwnd) and not releasing it.

DOCINFO di = {sizeof(DOCINFO), "Bitmap Printing"};

HDC pDC = GetPrinterDC(hwnd);
int w = GetDeviceCaps(pDC, HORZRES);
int h = GetDeviceCaps(pDC, VERTRES);
HDC hdcMem = CreateCompatibleDC(pDC);
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, hBitmap);

StartDoc(pDC, &di);
StartPage(pDC);
SetMapMode(pDC, MM_ISOTROPIC);
SetWindowExtEx(pDC, w,h, nullptr);
SetViewportExtEx(pDC, w, h, nullptr);

SetViewportOrgEx(pDC, 0, 0, nullptr);
StretchBlt(pDC, 0, 0, w, h, hdcMem, 0, 0, bmpWidth, bmpHeight, SRCCOPY);
EndPage (pDC);
EndDoc(pDC);
DeleteDC(pDC);
SelectObject(hdcMem, hbmOld);
DeleteDC(hdcMem);
commented: thanks for all +3

but i don't use VS20015 and i don't want show the printer dialog. these is just for print what is on window

I don't know what you mean.. The above code does not show a printer dialog and it certainly does not require VS2015.

It does not show the printer dialog because it does not use:

PrintDlg/PrintDlgEx

You need to set the viewport in your code.

i'm sorry i can't see what is the include file.
i can't use the GetPrinterDC() function.

i was close to finish the code... but i lose the cartridges on printer :(
in time i will came back here

can i print it on a pdf file(or other virtual printer) for don't lose so many cartridges?
i had installed 1, but i only get a empty page :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.