How to Enable Selection Button in Print Dialog BoxID: Q150288
|
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.
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.
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;
...
};
CMyEditView::CMyEditView()
{
// Initially, no selected text is found from CEditView window.
m_bPrintSelection = FALSE;
m_nNextSelCharToBePrint = m_nFirstNonSelChar = 0;
...
}
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;
}
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);
}
}
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);
}
}
For more information about the MFC printing, see:
"Programming with the Microsoft Foundation Class Library" in Books Online.
Additional query words: 1.50 1.51 1.52 1.00 2.00 2.10 2.20 4.00 4.10
Keywords : kbcode kbMFC kbPrinting kbVC
Version : 1.50 1.51 1.52 | 2.00 2.10 2.20
Platform : NT WINDOWS
Issue type :
Last Reviewed: August 2, 1999