1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
void print()
{
//getting window title for use it on print name
string windowtitle;
GetWindowString(hwnd,windowtitle);
DOCINFO doc = {sizeof(DOCINFO), windowtitle.c_str()};
//getting the default printer name:
//getting the string size for the printer name
//and then the printer name
unsigned long lPrinterNameLength;
GetDefaultPrinter( NULL, &lPrinterNameLength );
char szPrinterName[lPrinterNameLength];
GetDefaultPrinter( szPrinterName, &lPrinterNameLength );
//getting the printer DC, using the deafault printer name
HDC PrinterDC =CreateDC( "WINSPOOL",szPrinterName, NULL, NULL);
//getting the window DC and it client rect
HDC WindowDC=GetDC(hwnd);
RECT windowrect;
GetClientRect(hwnd, &windowrect);
//convert from pixels to twips
int PrinterWidth = MulDiv(windowrect.right, GetDeviceCaps(PrinterDC , LOGPIXELSX), GetDeviceCaps(WindowDC, LOGPIXELSX));;
int PrinterHeight = MulDiv(windowrect.bottom, GetDeviceCaps(PrinterDC , LOGPIXELSY), GetDeviceCaps(WindowDC , LOGPIXELSY)); ;
//start doc and page
//the print thing starts here
StartDoc(PrinterDC, &doc);
StartPage(PrinterDC);
//here we can draw what we want using GDI functions
//but never forget convert from pixels to twips
//draw the windc to printerdc
StretchBlt(PrinterDC, 0,0, PrinterWidth, PrinterHeight, WindowDC, 0, 0, windowrect.right, windowrect.bottom, SRCCOPY);
//end page and doc
//the print thing ends here
EndPage (PrinterDC);
EndDoc(PrinterDC);
//delete GDI resources
DeleteDC(PrinterDC);
ReleaseDC(hwnd,WindowDC);
DeleteDC(WindowDC);
}
|