CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2009
    Posts
    103

    RichEdit - Print - Print page 2-3?????

    We have a RichEditCtrl that we need to print. We enabled PD_PAGENUMS so we could select the pages we want to print but not sure how to modify the standard code to actually print the specified pages. I know we can get it from the nFromPage & nToPage but not sure how to "process" those selections properly (I didn't write the print routine although it looks pretty verbatim to some samples on the net).

    Here is the code we have. for our PRINT call....

    ---------------------------------------

    BOOL RichEditCtrl:nPrint()
    {
    CDC ThePrintDC; //create a device context to use
    CPrintDialog PrintDialog(FALSE,PD_ALLPAGES); //make a print dialog too
    PrintDialog.m_pd.nMinPage=1;
    PrintDialog.m_pd.nMaxPage=10;
    if(PrintDialog.DoModal()==IDOK) //pressed the ok button on the print dialog?
    {
    ThePrintDC.Attach(PrintDialog.GetPrinterDC()); //if so get settings
    }
    else
    return FALSE; //leave this procedure, before anything bad happens

    DOCINFO di; //make a docinfo structure
    ::ZeroMemory(&di, sizeof(DOCINFO)); //need to be claer, so clear stuff from it
    di.cbSize=sizeof(DOCINFO); //set size member
    di.lpszDocName=psPrintTitle; //set doc name, was passed via another funtion
    FORMATRANGE fr; //make a format range structure
    ::ZeroMemory(&fr, sizeof(FORMATRANGE)); //clear stuff from it
    HDC hdc=ThePrintDC.GetSafeHdc(); //get handle to DC we are using
    fr.hdc=hdc; //Set meber in FormatRange struct
    fr.hdcTarget=hdc; //Set member in FormatRange struct
    //This bit here will get all the dimentions of the printer setup
    int nHorizRes = ThePrintDC.GetDeviceCaps(HORZRES), //width P in MM
    nVertRes = ThePrintDC.GetDeviceCaps(VERTRES), //hight in raster lines
    nLogPixelsX = ThePrintDC.GetDeviceCaps(LOGPIXELSX), //pixels per inch along x
    nLogPixelsY = ThePrintDC.GetDeviceCaps(LOGPIXELSY); //pixels per inch along y
    //set the printable area of printer in the FormatRange struct
    fr.rcPage.left = 0; //these 2 mean top left
    fr.rcPage.top = 0;
    fr.rcPage.right = (nHorizRes/nLogPixelsX) * 1440; //these 2 mean bottom right
    fr.rcPage.bottom = (nVertRes/nLogPixelsY) * 1440; //equation changes pixel to TWIPS
    // Set up some margins all around. Make them one inch
    //results vary on printers depending on setup
    fr.rc.left = fr.rcPage.left + 1440; // 1440 TWIPS = 1 inch.
    fr.rc.top = fr.rcPage.top + 1440;
    fr.rc.right = fr.rcPage.right - 1440;
    fr.rc.bottom = fr.rcPage.bottom - 1440;
    //select all text for printing
    fr.chrg.cpMin = 0;
    fr.chrg.cpMax = -1; //-1 selects all
    //get length of document, used for more than one page
    long CharRange = GetTextLength();
    int ErrorStatus=0;
    //create cancel dialog
    //Start Printing
    //ThePrintDC.SetAbortProc(&AbortProc(hdc, )); //dont know how callbacks works yet

    ThePrintDC.StartDoc(&di); //start printing document
    do
    {
    ThePrintDC.StartPage(); //start the page
    fr.chrg.cpMin = FormatRange( &fr, TRUE );//send text to DC, and record index
    //of last fitting char
    ThePrintDC.EndPage();//end this page
    }
    }while(fr.chrg.cpMin <= CharRange); //while there is stuff to print ot there
    ThePrintDC.EndDoc();

    return TRUE;
    }


    -------------------------------------------------

    Thanks for any recommendations... this code is really pretty greek to me!!!

    Pete

  2. #2
    Join Date
    Apr 2009
    Location
    Cochin
    Posts
    83

    Exclamation Re: RichEdit - Print - Print page 2-3?????

    @ OP



    Please use "Code Tags"

    It is difficult to read your code
    "I studied everything but never topped. Today, toppers of the world's best universities are my employees"

    -William Henry Gates (Bill Gates)

  3. #3
    Join Date
    May 2009
    Posts
    103

    Resolved Re: RichEdit - Print - Print page 2-3?????

    Sorry, wasn't aware how to do that.....

    Here is the fixed code:

    Code:
    BOOL RichEditCtrl:nPrint()
    {
         CDC ThePrintDC;                                                        //create a device context to use
         CPrintDialog PrintDialog(FALSE,PD_ALLPAGES);         //make a print dialog too
         PrintDialog.m_pd.nMinPage=1;
         PrintDialog.m_pd.nMaxPage=10;
         if(PrintDialog.DoModal()==IDOK)                                //pressed the ok button
         {
               ThePrintDC.Attach(PrintDialog.GetPrinterDC());   //if so get settings
         }
         else
             return FALSE;                                    
    
         DOCINFO di; //make a docinfo structure
         ::ZeroMemory(&di, sizeof(DOCINFO));                      //need to be claer, so clear stuff from it
         di.cbSize=sizeof(DOCINFO);                                     //set size member
         di.lpszDocName=psPrintTitle;                                  //set doc name, was passed via another funtion
         FORMATRANGE fr;                                                    //make a format range structure
         ::ZeroMemory(&fr, sizeof(FORMATRANGE));            //clear stuff from it
         HDC hdc=ThePrintDC.GetSafeHdc();                       //get handle to DC we are using
         fr.hdc=hdc;                                                             //Set meber in FormatRange struct
         fr.hdcTarget=hdc;                                                  //Set member in FormatRange struct
    
         //This bit here will get all the dimentions of the printer setup
         int nHorizRes = ThePrintDC.GetDeviceCaps(HORZRES),          //width P in MM
         nVertRes = ThePrintDC.GetDeviceCaps(VERTRES),                 //hight in raster lines
         nLogPixelsX = ThePrintDC.GetDeviceCaps(LOGPIXELSX),       //pixels per inch along x
         nLogPixelsY = ThePrintDC.GetDeviceCaps(LOGPIXELSY);       //pixels per inch along y
    
         //set the printable area of printer in the FormatRange struct
         fr.rcPage.left = 0;                                                                //these 2 mean top left
         fr.rcPage.top = 0;
         fr.rcPage.right = (nHorizRes/nLogPixelsX) * 1440;             //these 2 mean bottom right
         fr.rcPage.bottom = (nVertRes/nLogPixelsY) * 1440;           //equation changes pixel to TWIPS
    
         // Set up some margins all around. Make them one inch
         //results vary on printers depending on setup
         fr.rc.left = fr.rcPage.left + 1440; // 1440 TWIPS = 1 inch.
         fr.rc.top = fr.rcPage.top + 1440;
         fr.rc.right = fr.rcPage.right - 1440;
         fr.rc.bottom = fr.rcPage.bottom - 1440;
    
         //select all text for printing
         fr.chrg.cpMin = 0;
         fr.chrg.cpMax = -1; //-1 selects all
    
         //get length of document, used for more than one page
         long CharRange = GetTextLength();
         int ErrorStatus=0;
    
         //create cancel dialog
         //Start Printing
         //ThePrintDC.SetAbortProc(&AbortProc(hdc, )); //dont know how callbacks works yet
         
         ThePrintDC.StartDoc(&di);                                           //start printing document
         do
         {
              ThePrintDC.StartPage();                                        //start the page
              fr.chrg.cpMin = FormatRange( &fr, TRUE );            //send text to DC, and record index
              //of last fitting char
              ThePrintDC.EndPage();                                        //end this page
         }while(fr.chrg.cpMin <= CharRange);                       //while there is stuff to print ot there
         ThePrintDC.EndDoc();
    
         return TRUE;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured