Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Printing Document

Printing Document

Scheduled Pinned Locked Moved C / C++ / MFC
help
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Ganesh_T
    wrote on last edited by
    #1

    I am facing the problem in printing document. I haev craeted the WIn32 Apllicatio and then in WM_PAINT I amdraing the ellipse. Now I want to printwhatever I see on client area. I tried with it but was giving whole scrren aftyer printing with reduced size. I need it in Win32. "A winner is not one who never fails...but the one who never quits"

    H M 2 Replies Last reply
    0
    • G Ganesh_T

      I am facing the problem in printing document. I haev craeted the WIn32 Apllicatio and then in WM_PAINT I amdraing the ellipse. Now I want to printwhatever I see on client area. I tried with it but was giving whole scrren aftyer printing with reduced size. I need it in Win32. "A winner is not one who never fails...but the one who never quits"

      H Offline
      H Offline
      Hamid Taebi
      wrote on last edited by
      #2

      Do you want to prin your shapes did you saw PRINTDLGand StartDoc_**


      **_

      whitesky


      G 1 Reply Last reply
      0
      • G Ganesh_T

        I am facing the problem in printing document. I haev craeted the WIn32 Apllicatio and then in WM_PAINT I amdraing the ellipse. Now I want to printwhatever I see on client area. I tried with it but was giving whole scrren aftyer printing with reduced size. I need it in Win32. "A winner is not one who never fails...but the one who never quits"

        M Offline
        M Offline
        Monty2
        wrote on last edited by
        #3

        Ganesh_T wrote:

        I tried with it but was giving whole scrren aftyer printing with reduced size.

        Thats cause the printers resolution is more than Screens resolution heres some sample code

        CDC prtDC;
        CPrintInfo printInfo;
        CSize size;
        DOCINFO di;

        CSize paper_size; //printer paper size in mm
        int xLogPPI = 0;
        int yLogPPI = 0;

        if( AfxGetApp()->GetPrinterDeviceDefaults(&printInfo.m_pPD->m_pd) )
        {
        HDC hDC = printInfo.m_pPD->m_pd.hDC;
        if (hDC == NULL)
        hDC = printInfo.m_pPD->CreatePrinterDC();
        if(hDC !=NULL)
        {
        prtDC.Attach(hDC);
        paper_size.cx = prtDC.GetDeviceCaps(HORZSIZE);
        paper_size.cy = prtDC.GetDeviceCaps(VERTSIZE);
        xLogPPI = prtDC.GetDeviceCaps(LOGPIXELSX);
        yLogPPI = prtDC.GetDeviceCaps(LOGPIXELSY);
        }
        else
        {
        AfxMessageBox("Can not find printer. Please check installed/default printers.");
        return ;
        }
        }
        int scr_xLogPPI = pDC->GetDeviceCaps(LOGPIXELSX);
        int scr_yLogPPI = pDC->GetDeviceCaps(LOGPIXELSY);
        int paper_width = (int) ((double) paper_size.cx * (double) xLogPPI / 25.4); //width of a printed page in pixels
        int paper_height = (int) ((double) paper_size.cy * (double) yLogPPI / 25.4);
        double ratio_x = (double) xLogPPI / (double) scr_xLogPPI;
        double ratio_y = (double) yLogPPI / (double) scr_yLogPPI;

        //Create GDI stuff
        CFont \*pPrnFont = new CFont();
        LOGFONT lf = {0};
        lf.lfHeight = -14 \* ratio\_x;
        lf.lfWeight = FW\_NORMAL;
        \_tcscpy(lf.lfFaceName,"Times new roman");
        pPrnFont->CreateFontIndirect(&lf);
        

        ..
        ..
        ..

        you need to multiply everything with ratio_x and ratio_y so that everythings printed like it is diplayed on the screen Hope it Helps


        **You know you're obsessed with computer graphics when you're outside and you look up at the trees and think, "Wow! That's spectacular resolution!"

        Only kings, presidents, editors, and people with tapeworms have the right to use the editorial "we."**

        G 1 Reply Last reply
        0
        • H Hamid Taebi

          Do you want to prin your shapes did you saw PRINTDLGand StartDoc_**


          **_

          whitesky


          G Offline
          G Offline
          Ganesh_T
          wrote on last edited by
          #4

          I Have seen ... I need whatever is there in CLient area to be printed....I have written the sample code here: void PrintMyPage(HWND hWndMain,HDC hClientDC) { try { DOCINFO lpdi; lpdi.cbSize = sizeof( DOCINFO ); lpdi.lpszDocName = _T( "ReadMe.txt" ); lpdi.lpszOutput = NULL; lpdi.lpszDatatype = _T("RAW"); lpdi.fwType = 0; MessageBox(NULL,_T("Before CreateDC"),_T("Step by Step"),MB_OK); // CreateDC for printer //HP LaserJet 1020-rkhale@AJAY HDC hPrinterDC = CreateDC(_T("HP LaserJet 1020"),_T("HPLaserJ"),NULL,0); if(!hPrinterDC) { TCHAR szMessage[256]; wsprintf(szMessage,_T("CreateDC Failed, Last Error:%d"),GetLastError()); MessageBox(NULL,szMessage,_T("Error"),MB_OK); return; } // Lets print into the hdc if(!StartDoc(hPrinterDC, &lpdi)) { MessageBox(NULL,_T("StartDoc Failed"),_T("Error"),MB_OK); return; } // Start Page if(!StartPage(hPrinterDC)) { MessageBox(NULL,_T("StartPage Failed"),_T("Error"),MB_OK); return; } int xPage,yPage; xPage = GetDeviceCaps(hPrinterDC,HORZRES); yPage = GetDeviceCaps(hPrinterDC,VERTRES); SetMapMode (hPrinterDC, MM_ISOTROPIC) ; SetWindowExtEx (hPrinterDC, 1000, 1000, NULL) ; SetViewportExtEx (hPrinterDC, xPage / 2, -yPage / 2, NULL) ; SetViewportOrgEx (hPrinterDC, xPage / 2, yPage / 2, NULL) ; HDC hCompatibleDC; hCompatibleDC = CreateCompatibleDC(hClientDC); RECT rt; GetClientRect(hWndMain,&rt); HBITMAP hbmScreen = CreateCompatibleBitmap(hClientDC, GetDeviceCaps(hClientDC,HORZRES), GetDeviceCaps(hClientDC,VERTRES)); SelectObject(hCompatibleDC, hbmScreen); HDC tempDC = CreateCompatibleDC(NULL); SelectObject(tempDC,hbmScreen); BITMAP bmp; GetObject(hbmScreen,sizeof(bmp),&bmp); // Do BitBlt if(!::BitBlt(hPrinterDC,0,0, GetDeviceCaps(hCompatibleDC,HORZRES), GetDeviceCaps(hCompatibleDC,VERTRES),hCompatibleDC,0, 0,SRCCOPY)) { TCHAR szMessage[256]; wsprintf(szMessage,_T("BitBlt failed, Last Error:%d"),GetLastError()); MessageBox(NULL,szMessage,_T("Error"),MB_OK); return; } // Actual Printing occurs here if(!EndPage(hPrinterDC)) { MessageBox(NULL,_T("EndPage Failed"),_T("Error"),MB_OK); return; } // End Document !!! if(!EndDoc(hPrinterDC)) { MessageBox(NULL,_T("EndDoc Failed"),_T("Error"),MB_OK); return; } MessageBox(NULL

          H 1 Reply Last reply
          0
          • M Monty2

            Ganesh_T wrote:

            I tried with it but was giving whole scrren aftyer printing with reduced size.

            Thats cause the printers resolution is more than Screens resolution heres some sample code

            CDC prtDC;
            CPrintInfo printInfo;
            CSize size;
            DOCINFO di;

            CSize paper_size; //printer paper size in mm
            int xLogPPI = 0;
            int yLogPPI = 0;

            if( AfxGetApp()->GetPrinterDeviceDefaults(&printInfo.m_pPD->m_pd) )
            {
            HDC hDC = printInfo.m_pPD->m_pd.hDC;
            if (hDC == NULL)
            hDC = printInfo.m_pPD->CreatePrinterDC();
            if(hDC !=NULL)
            {
            prtDC.Attach(hDC);
            paper_size.cx = prtDC.GetDeviceCaps(HORZSIZE);
            paper_size.cy = prtDC.GetDeviceCaps(VERTSIZE);
            xLogPPI = prtDC.GetDeviceCaps(LOGPIXELSX);
            yLogPPI = prtDC.GetDeviceCaps(LOGPIXELSY);
            }
            else
            {
            AfxMessageBox("Can not find printer. Please check installed/default printers.");
            return ;
            }
            }
            int scr_xLogPPI = pDC->GetDeviceCaps(LOGPIXELSX);
            int scr_yLogPPI = pDC->GetDeviceCaps(LOGPIXELSY);
            int paper_width = (int) ((double) paper_size.cx * (double) xLogPPI / 25.4); //width of a printed page in pixels
            int paper_height = (int) ((double) paper_size.cy * (double) yLogPPI / 25.4);
            double ratio_x = (double) xLogPPI / (double) scr_xLogPPI;
            double ratio_y = (double) yLogPPI / (double) scr_yLogPPI;

            //Create GDI stuff
            CFont \*pPrnFont = new CFont();
            LOGFONT lf = {0};
            lf.lfHeight = -14 \* ratio\_x;
            lf.lfWeight = FW\_NORMAL;
            \_tcscpy(lf.lfFaceName,"Times new roman");
            pPrnFont->CreateFontIndirect(&lf);
            

            ..
            ..
            ..

            you need to multiply everything with ratio_x and ratio_y so that everythings printed like it is diplayed on the screen Hope it Helps


            **You know you're obsessed with computer graphics when you're outside and you look up at the trees and think, "Wow! That's spectacular resolution!"

            Only kings, presidents, editors, and people with tapeworms have the right to use the editorial "we."**

            G Offline
            G Offline
            Ganesh_T
            wrote on last edited by
            #5

            I want to use only SDK. no mfc classes. I haveposted the code in my reply to whitesky "A winner is not one who never fails...but the one who never quits" -- modified at 4:41 Tuesday 11th July, 2006

            M 1 Reply Last reply
            0
            • G Ganesh_T

              I want to use only SDK. no mfc classes. I haveposted the code in my reply to whitesky "A winner is not one who never fails...but the one who never quits" -- modified at 4:41 Tuesday 11th July, 2006

              M Offline
              M Offline
              Monty2
              wrote on last edited by
              #6

              Ganesh_T wrote:

              I want to use only SDK. no mfc classes. I haveposted the code in my reply to whitesky

              Ok Ganesh listen up, The printer resolution is far more than screen's resolution. So you need to calculate the number of times Printer resolution is greater than Screen's So get printers Resolution use

              int xPrnRes = GetDeviceCaps(hPrinterDC,LOGPIXELSX);
              int yPrnRes = GetDeviceCaps(hPrinterDC,LOGPIXELSY);

              Get Screens Res

              int xScrnRes = GetDeviceCaps(hScreenDC,LOGPIXELSX);
              int yScrnRes = GetDeviceCaps(hScreenDC,LOGPIXELSY);

              Get Ratio

              double ratio_x = (double)xPrnRes/(double)xScrnRes;
              double ratio_y = (double)yPrnRes/(double)yScrnRes;

              ok so if you have an image of say 100x100 px on screen and you want to print it the destination image will be of 100*ratio_x and 100*ratio_y so instead of using BitBlt you have to use StretchBlt to stretch the Bitmap


              **You know you're obsessed with computer graphics when you're outside and you look up at the trees and think, "Wow! That's spectacular resolution!"

              Only kings, presidents, editors, and people with tapeworms have the right to use the editorial "we."**

              1 Reply Last reply
              0
              • G Ganesh_T

                I Have seen ... I need whatever is there in CLient area to be printed....I have written the sample code here: void PrintMyPage(HWND hWndMain,HDC hClientDC) { try { DOCINFO lpdi; lpdi.cbSize = sizeof( DOCINFO ); lpdi.lpszDocName = _T( "ReadMe.txt" ); lpdi.lpszOutput = NULL; lpdi.lpszDatatype = _T("RAW"); lpdi.fwType = 0; MessageBox(NULL,_T("Before CreateDC"),_T("Step by Step"),MB_OK); // CreateDC for printer //HP LaserJet 1020-rkhale@AJAY HDC hPrinterDC = CreateDC(_T("HP LaserJet 1020"),_T("HPLaserJ"),NULL,0); if(!hPrinterDC) { TCHAR szMessage[256]; wsprintf(szMessage,_T("CreateDC Failed, Last Error:%d"),GetLastError()); MessageBox(NULL,szMessage,_T("Error"),MB_OK); return; } // Lets print into the hdc if(!StartDoc(hPrinterDC, &lpdi)) { MessageBox(NULL,_T("StartDoc Failed"),_T("Error"),MB_OK); return; } // Start Page if(!StartPage(hPrinterDC)) { MessageBox(NULL,_T("StartPage Failed"),_T("Error"),MB_OK); return; } int xPage,yPage; xPage = GetDeviceCaps(hPrinterDC,HORZRES); yPage = GetDeviceCaps(hPrinterDC,VERTRES); SetMapMode (hPrinterDC, MM_ISOTROPIC) ; SetWindowExtEx (hPrinterDC, 1000, 1000, NULL) ; SetViewportExtEx (hPrinterDC, xPage / 2, -yPage / 2, NULL) ; SetViewportOrgEx (hPrinterDC, xPage / 2, yPage / 2, NULL) ; HDC hCompatibleDC; hCompatibleDC = CreateCompatibleDC(hClientDC); RECT rt; GetClientRect(hWndMain,&rt); HBITMAP hbmScreen = CreateCompatibleBitmap(hClientDC, GetDeviceCaps(hClientDC,HORZRES), GetDeviceCaps(hClientDC,VERTRES)); SelectObject(hCompatibleDC, hbmScreen); HDC tempDC = CreateCompatibleDC(NULL); SelectObject(tempDC,hbmScreen); BITMAP bmp; GetObject(hbmScreen,sizeof(bmp),&bmp); // Do BitBlt if(!::BitBlt(hPrinterDC,0,0, GetDeviceCaps(hCompatibleDC,HORZRES), GetDeviceCaps(hCompatibleDC,VERTRES),hCompatibleDC,0, 0,SRCCOPY)) { TCHAR szMessage[256]; wsprintf(szMessage,_T("BitBlt failed, Last Error:%d"),GetLastError()); MessageBox(NULL,szMessage,_T("Error"),MB_OK); return; } // Actual Printing occurs here if(!EndPage(hPrinterDC)) { MessageBox(NULL,_T("EndPage Failed"),_T("Error"),MB_OK); return; } // End Document !!! if(!EndDoc(hPrinterDC)) { MessageBox(NULL,_T("EndDoc Failed"),_T("Error"),MB_OK); return; } MessageBox(NULL

                H Offline
                H Offline
                Hamid Taebi
                wrote on last edited by
                #7

                I insert lines to this code but check your code i used PrintDlg and use from pd.hDC instead CreateDC and i cant find a ellipse in your code but when you run this code you see a ellipse almost in center void PrintMyPage(HWND hWndMain,HDC hClientDC) { try { DOCINFO lpdi; lpdi.cbSize = sizeof( DOCINFO ); lpdi.lpszDocName = _T( "ReadMe.txt" ); lpdi.lpszOutput = NULL; //lpdi.lpszDatatype = _T("RAW"); //lpdi.fwType = 0; //MessageBox(NULL,_T("Before CreateDC"),_T("Step by Step"),MB_OK); // CreateDC for printer //HP LaserJet 1020-rkhale@AJAY //HDC hPrinterDC = CreateDC(_T("HP DeskJet 1220C Printer"),"HP DeskJet 1220C Printer",NULL,0); PRINTDLG pd; memset((void *)&pd, 0,sizeof(PRINTDLG)); pd.lStructSize = sizeof(PRINTDLG); pd.hwndOwner = hWndMain; pd.Flags =PD_RETURNDC|PD_ENABLESETUPTEMPLATE; pd.hInstance = NULL; pd.nMaxPage=1; pd.nMinPage=1; pd.nCopies=1; pd.hDevMode=0; pd.nFromPage=1; pd.nToPage=1; PrintDlg(&pd); HDC hPrinterDC = pd.hDC; if(!hPrinterDC) { TCHAR szMessage[256]; wsprintf(szMessage,_T("CreateDC Failed, Last Error:%d"),GetLastError()); MessageBox(NULL,szMessage,_T("Error"),MB_OK); return; } // Lets print into the hdc if(!StartDoc(hPrinterDC, &lpdi)) { MessageBox(NULL,_T("StartDoc Failed"),_T("Error"),MB_OK); return; } // Start Page if(!StartPage(hPrinterDC)) { MessageBox(NULL,_T("StartPage Failed"),_T("Error"),MB_OK); return; } int xPage,yPage; xPage = GetDeviceCaps(pd.hDC,HORZRES); yPage = GetDeviceCaps(pd.hDC,VERTRES); SetMapMode (pd.hDC, MM_ISOTROPIC) ; SetWindowExtEx (pd.hDC, 1000, 1000, NULL) ; SetViewportExtEx (pd.hDC, xPage / 2, -yPage / 2, NULL) ; SetViewportOrgEx (pd.hDC, xPage / 2, yPage / 2, NULL) ; HDC hCompatibleDC; hCompatibleDC = CreateCompatibleDC(hClientDC); RECT rt; GetClientRect(hWndMain,&rt); HBITMAP hbmScreen = CreateCompatibleBitmap(hClientDC,GetDeviceCaps(hClientDC,HORZRES), GetDeviceCaps(hClientDC,VERTRES)); SelectObject(hCompatibleDC, hbmScreen); HDC tempDC = CreateCompatibleDC(NULL); SelectObject(tempDC,hbmScreen); BITMAP bmp; GetObject(hbmScreen,sizeof(bmp),&bmp); Ellipse(pd.hDC,100,100,400,400); // Do BitBlt /*if(!::BitBlt(pd.hDC,0,0, GetDeviceCaps(hCompatibleDC,HORZRES), GetDeviceCaps(hCompatibleDC,VERTRES),hCompatibleDC,0, 0,SRCCOPY)) { TCHAR szMessage[256]; wsprintf(szMessage,_T("BitBlt failed, Last Error:%d"),GetLastError()); MessageBox(NULL,szMess

                G 1 Reply Last reply
                0
                • H Hamid Taebi

                  I insert lines to this code but check your code i used PrintDlg and use from pd.hDC instead CreateDC and i cant find a ellipse in your code but when you run this code you see a ellipse almost in center void PrintMyPage(HWND hWndMain,HDC hClientDC) { try { DOCINFO lpdi; lpdi.cbSize = sizeof( DOCINFO ); lpdi.lpszDocName = _T( "ReadMe.txt" ); lpdi.lpszOutput = NULL; //lpdi.lpszDatatype = _T("RAW"); //lpdi.fwType = 0; //MessageBox(NULL,_T("Before CreateDC"),_T("Step by Step"),MB_OK); // CreateDC for printer //HP LaserJet 1020-rkhale@AJAY //HDC hPrinterDC = CreateDC(_T("HP DeskJet 1220C Printer"),"HP DeskJet 1220C Printer",NULL,0); PRINTDLG pd; memset((void *)&pd, 0,sizeof(PRINTDLG)); pd.lStructSize = sizeof(PRINTDLG); pd.hwndOwner = hWndMain; pd.Flags =PD_RETURNDC|PD_ENABLESETUPTEMPLATE; pd.hInstance = NULL; pd.nMaxPage=1; pd.nMinPage=1; pd.nCopies=1; pd.hDevMode=0; pd.nFromPage=1; pd.nToPage=1; PrintDlg(&pd); HDC hPrinterDC = pd.hDC; if(!hPrinterDC) { TCHAR szMessage[256]; wsprintf(szMessage,_T("CreateDC Failed, Last Error:%d"),GetLastError()); MessageBox(NULL,szMessage,_T("Error"),MB_OK); return; } // Lets print into the hdc if(!StartDoc(hPrinterDC, &lpdi)) { MessageBox(NULL,_T("StartDoc Failed"),_T("Error"),MB_OK); return; } // Start Page if(!StartPage(hPrinterDC)) { MessageBox(NULL,_T("StartPage Failed"),_T("Error"),MB_OK); return; } int xPage,yPage; xPage = GetDeviceCaps(pd.hDC,HORZRES); yPage = GetDeviceCaps(pd.hDC,VERTRES); SetMapMode (pd.hDC, MM_ISOTROPIC) ; SetWindowExtEx (pd.hDC, 1000, 1000, NULL) ; SetViewportExtEx (pd.hDC, xPage / 2, -yPage / 2, NULL) ; SetViewportOrgEx (pd.hDC, xPage / 2, yPage / 2, NULL) ; HDC hCompatibleDC; hCompatibleDC = CreateCompatibleDC(hClientDC); RECT rt; GetClientRect(hWndMain,&rt); HBITMAP hbmScreen = CreateCompatibleBitmap(hClientDC,GetDeviceCaps(hClientDC,HORZRES), GetDeviceCaps(hClientDC,VERTRES)); SelectObject(hCompatibleDC, hbmScreen); HDC tempDC = CreateCompatibleDC(NULL); SelectObject(tempDC,hbmScreen); BITMAP bmp; GetObject(hbmScreen,sizeof(bmp),&bmp); Ellipse(pd.hDC,100,100,400,400); // Do BitBlt /*if(!::BitBlt(pd.hDC,0,0, GetDeviceCaps(hCompatibleDC,HORZRES), GetDeviceCaps(hCompatibleDC,VERTRES),hCompatibleDC,0, 0,SRCCOPY)) { TCHAR szMessage[256]; wsprintf(szMessage,_T("BitBlt failed, Last Error:%d"),GetLastError()); MessageBox(NULL,szMess

                  G Offline
                  G Offline
                  Ganesh_T
                  wrote on last edited by
                  #8

                  I have the ellipse created on WM_PAINT. case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... ::Ellipse(hdc,100,100,500,500); RECT rect; rect.left = 200; rect.top = 200; rect.right = 400; rect.bottom = 400; ::DrawText(hdc,_T("I am the best"),(int)strlen(_T("I am the best")),&rect,DT_CENTER); EndPaint(hWnd, &ps); break; and on the menu click: case IDM_MENU1: hClientDC = GetDC(hWnd); PrintSelected(hWnd,hClientDC); break; I don't want directly to draw ont print dc. First I want to take the handel of my Client DC such taht whatever is drawn on the client are shouldbe printed on page "A winner is not one who never fails...but the one who never quits"

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups