Microsoft KB Archive/150288

= HOWTO: How to Enable Selection Button in Print Dialog Box =

Article ID: 150288

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 1.52 Professional Edition

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 2.2</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 4.1 Subscription</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q150288

<div class="summary_section">

SUMMARY
By design, the Selection button is disabled on the Print dialog box created by the MFC framework. This article describes how to enable the Selection button and provides a code segment illustrating how to support selection printing in a CEditView class.

<div class="moreinformation_section">

MORE INFORMATION
When you choose Print from the File menu from any AppWizard-generated application, the PD_NOSELECTION style for the CPrintDialog disables the Selection button on the Print dialog box. The enabling task can be performed before the Print dialog box is displayed in the OnPreparePrinting function of any CView derived class.

The CEditView class in MFC does not support selection printing. However, selection printing can be accomplished in a CEditView class as outlined in the "Sample Code" section in this article.

Sample Code
Steps to support selection printing in a CEditView class:

<ol> Derive a class from CEditView if one is not found.</li>  Add the following member variables related to selection printing to the CEditView derived class: class CMyEditView : public CEditView {      // TRUE for selection printing; FALSE otherwise. BOOL   m_bPrintSelection;

// Position of the next character to be printed in the current // selection. It is 0 when no selection is found. If      // selection is found, then initially it will be equal to the // position of the first character in the current selection. // And its value will be incremented until you are done with // selection printing. int   m_nNextSelCharToBePrint;

// Position of the first unselected character past the end of      // the current selection. It is 0 when no selection is found. int   m_nFirstNonSelChar;

...      };                        </li>  Initialize the member variables from step 2 in the constructor function of the CEditView derived class: CMyEditView::CMyEditView {      // Initially, no selected text is found from CEditView window. m_bPrintSelection = FALSE; m_nNextSelCharToBePrint = m_nFirstNonSelChar = 0;

...      }                        </li>  Override the OnPreparePrinting function in the class derived from the CEditView class. Add the following code to enable the Selection button if selected text is found from CEditView. Also, make selection printing the default printing in Print dialog box: BOOL CMyEditView::OnPreparePrinting(CPrintInfo* pInfo) {      // Is there any selected text in CEditView window? If the // selected text is found, enable the Selection button // and then have it checked so selection printing is performed // by default. GetEditCtrl.GetSel(m_nNextSelCharToBePrint,                           m_nFirstNonSelChar); if (m_nNextSelCharToBePrint != m_nFirstNonSelChar) {          // Enable the Selection button. pInfo->m_pPD->m_pd.Flags &= ~PD_NOSELECTION;

// Checked the Selection button. pInfo->m_pPD->m_pd.Flags |= PD_SELECTION; }

// Call the base class OnPreparePrinting to display the // Print dialog box. BOOL rvalue = CEditView::OnPreparePrinting(pInfo);

// Now check to see whether selection printing should be      // performed or not. TRUE if the user selects the selection // printing; FALSE otherwise. m_bPrintSelection = pInfo->m_pPD->PrintSelection;

return rvalue; }                       </li>  Override the OnPrepareDC function of the CEditView derived class. The function is called for every page to be printed and before OnPrint is called. Stop the MFC framework from calling OnPrint again if the selection printing from the last call to OnPrint function is finished. Set the pInfo->m_bContinuePrinting to TRUE to continue selection printing or FALSE to discontinue. void CMyEditView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo) {      // If this is for selection printing, you should check whether // you are at the end of the selected text yet, i.e.      // m_nNextSelCharToBePrint equals to m_SelectionStop. // m_bContinuePrinting is TRUE if you are not at the end of the // selected text; FALSE otherwise. if (m_bPrintSelection) {          pInfo->m_bContinuePrinting = m_nNextSelCharToBePrint < m_nFirstNonSelChar; }      else {          CEditView::OnPrepareDC(pDC, pInfo); }      }                        </li>  Override the OnPrint function of the CEditView derived class. Use PrintInsideRect, a public member function of CEditView, to print the selected text in the rectangle specified by the pInfo->m_rectDraw variable. The return value of PrintInsideRect is the index of the next character to be printed; the character following the last character rendered: void CMyEditView::OnPrint(CDC* pDC, CPrintInfo* pInfo) {      if (m_bPrintSelection) {          m_nNextSelCharToBePrint = PrintInsideRect(pDC, pInfo->m_rectDraw,              m_nNextSelCharToBePrint, m_nFirstNonSelChar); }      else {          CEditView::OnPrint(pDC, pInfo); }      } </li></ol>

<div class="references_section">