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

            //draw on printer using GDI functions
            RECT windowRect;
            SetMapMode(printerDC, GetMapMode(GetDC(hwnd)));

            //end pages and doc

            //delete the printer DC

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);
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);
SelectObject(hdcMem, hbmOld);
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:


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, learning, and sharing knowledge.