Advertisement
Guest User

Printing test

a guest
Feb 20th, 2017
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <windows.h>
  2. #include <commctrl.h>
  3. #include <stdio.h>
  4.  
  5.  
  6. void drawPage(HDC hdc)
  7. {
  8.     HBITMAP logo = (HBITMAP)LoadImage(NULL, "logo.bmp", IMAGE_BITMAP, 0,0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
  9.  
  10.     int pageWidth, pageHeight;
  11.     pageWidth = GetDeviceCaps( hdc, HORZRES ),
  12.     pageHeight = GetDeviceCaps( hdc, VERTRES );
  13.     int dpiX, dpiY;
  14.  
  15.     dpiX = GetDeviceCaps(hdc, LOGPIXELSX);
  16.     dpiY = GetDeviceCaps(hdc, LOGPIXELSY);
  17.  
  18.     double dpiMmX, dpiMmY;
  19.  
  20.     dpiMmX = dpiX / 25.4;
  21.     dpiMmY = dpiY / 25.4;
  22.  
  23.     HBRUSH redBrush = CreateSolidBrush( RGB(255,0,0) );
  24.     RECT pageRect = {0,0,pageWidth,pageHeight};
  25.  
  26.     int borderWidth = dpiMmX * 10;
  27.     int borderHeight = dpiMmY * 10;
  28.  
  29.     RECT borderRect = {borderWidth, borderHeight, pageWidth-(0.5*dpiMmX)-borderWidth, pageHeight-(0.5*dpiMmY)-borderHeight};
  30.  
  31.     FrameRect(hdc, &pageRect, redBrush);
  32.     int i;
  33.     for (i=0; i<5; i++)
  34.     {
  35.         FrameRect(hdc, &borderRect, redBrush);
  36.         InflateRect(&borderRect, -1, -1);
  37.     }
  38.  
  39.     HDC memDC = CreateCompatibleDC(hdc);
  40.     HBITMAP old = (HBITMAP)SelectObject(memDC, logo);
  41.     StretchBlt(hdc,
  42.                2*borderWidth,2*borderHeight,
  43.                1000,540,                        // <-- should be calculated in code. I know the aspect ratio of the image
  44.                memDC,                           //     so am using these values. Divide these by dpiMmX and dpiMmY for size in
  45.                0,0,                             //     mm
  46.                250,125,
  47.                SRCCOPY);
  48.     SelectObject(memDC, old);
  49.     DeleteObject(logo);
  50.     DeleteDC(memDC);
  51.  
  52.     printf("Logo size: %d x %d mm\n", (int)(1000/dpiMmX), (int)(540/dpiMmY) );
  53.  
  54.     DeleteObject(redBrush);
  55. }
  56.  
  57.  
  58. void testPrint(char *szPrinterName)
  59. {
  60.     DOCINFO diDocInfo = {0};
  61.     diDocInfo.cbSize = sizeof( DOCINFO );
  62.     diDocInfo.lpszDocName = "printTest";
  63.  
  64.     HANDLE printerHandle;
  65.     if (OpenPrinter(szPrinterName, &printerHandle, NULL) != 0)
  66.     {
  67.         HDC pDC2 = CreateDC("", szPrinterName, NULL, NULL);
  68.  
  69.         if( StartDoc( pDC2, &diDocInfo ) > 0 )
  70.         {
  71.             if( StartPage( pDC2 ) > 0 )
  72.             {
  73.                 drawPage(pDC2);
  74.                 EndPage(pDC2);
  75.             }
  76.             EndDoc( pDC2 );
  77.         }
  78.         ClosePrinter(printerHandle);
  79.     }
  80. }
  81.  
  82. int main()
  83. {
  84.     testPrint("CutePDF Writer");
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement