CRichEditView Class

 

With CRichEditDoc and CRichEditCntrItem, provides the functionality of the rich edit control within the context of MFC's document view architecture.

class CRichEditView : public CCtrlView  

Public Constructors

NameDescription
CRichEditView::CRichEditViewConstructs a CRichEditView object.

Public Methods

NameDescription
CRichEditView::AdjustDialogPositionMoves a dialog box so that it doesn't obscure the current selection.
CRichEditView::CanPasteTells whether the Clipboard contains data that can be pasted into the rich edit view.
CRichEditView::DoPastePastes an OLE item into this rich edit view.
CRichEditView::FindTextFinds the specified text, invoking the wait cursor.
CRichEditView::FindTextSimpleFinds the specified text.
CRichEditView::GetCharFormatSelectionRetrieves the character formatting attributes for the current selection.
CRichEditView::GetDocumentRetrieves a pointer to the related CRichEditDoc.
CRichEditView::GetInPlaceActiveItemRetrieves the OLE item that is currently in-place active in the rich edit view.
CRichEditView::GetMarginsRetrieves the margins for this rich edit view.
CRichEditView::GetPageRectRetrieves the page rectangle for this rich edit view.
CRichEditView::GetPaperSizeRetrieves the paper size for this rich edit view.
CRichEditView::GetParaFormatSelectionRetrieves the paragraph formatting attributes for the current selection.
CRichEditView::GetPrintRectRetrieves the print rectangle for this rich edit view.
CRichEditView::GetPrintWidthRetrieves the print width for this rich edit view.
CRichEditView::GetRichEditCtrlRetrieves the rich edit control.
CRichEditView::GetSelectedItemRetrieves the selected item from the rich edit view.
CRichEditView::GetTextLengthRetrieves the length of the text in the rich edit view.
CRichEditView::GetTextLengthExRetrieves the number of characters or bytes in the rich edit view. Expanded flag list for method of determining the length.
CRichEditView::InsertFileAsObjectInserts a file as an OLE item.
CRichEditView::InsertItemInserts a new item as an OLE item.
CRichEditView::IsRichEditFormatTells whether the Clipboard contains data in a rich edit or text format.
CRichEditView::OnCharEffectToggles the character formatting for the current selection.
CRichEditView::OnParaAlignChanges the alignment of paragraphs.
CRichEditView::OnUpdateCharEffectUpdates the Command UI for character public member functions.
CRichEditView::OnUpdateParaAlignUpdates the Command UI for paragraph public member functions.
CRichEditView::PrintInsideRectFormats the specified text within the given rectangle.
CRichEditView::PrintPageFormats the specified text within the given page.
CRichEditView::SetCharFormatSets the character formatting attributes for the current selection.
CRichEditView::SetMarginsSets the margins for this rich edit view.
CRichEditView::SetPaperSizeSets the paper size for this rich edit view.
CRichEditView::SetParaFormatSets the paragraph formatting attributes for the current selection.
CRichEditView::TextNotFoundResets the internal search state of the control.

Protected Methods

NameDescription
CRichEditView::GetClipboardDataRetrieves a Clipboard object for a range in this rich edit view.
CRichEditView::GetContextMenuRetrieves a context menu to use on a right mouse-button down.
CRichEditView::IsSelectedIndicates if the given OLE item is selected or not.
CRichEditView::OnFindNextFinds the next occurrence of a substring.
CRichEditView::OnInitialUpdateRefreshes a view when it is first attached to a document.
CRichEditView::OnPasteNativeObjectRetrieves native data from an OLE item.
CRichEditView::OnPrinterChangedSets the print characteristics to the given device.
CRichEditView::OnReplaceAllReplaces all occurrences of a given string with a new string.
CRichEditView::OnReplaceSelReplaces the current selection.
CRichEditView::OnTextNotFoundHandles user notification that the requested text was not found.
CRichEditView::QueryAcceptDataQueries to see about the data on the IDataObject.
CRichEditView::WrapChangedAdjusts the target output device for this rich edit view, based on the value of m_nWordWrap.

Public Data Members

NameDescription
CRichEditView::m_nBulletIndentIndicates the amount of indent for bullet lists.
CRichEditView::m_nWordWrapIndicates the word wrap constraints.

A "rich edit control" is a window in which the user can enter and edit text. The text can be assigned character and paragraph formatting, and can include embedded OLE objects. Rich edit controls provide a programming interface for formatting text. However, an application must implement any user interface components necessary to make formatting operations available to the user.

CRichEditView maintains the text and formatting characteristic of text. CRichEditDoc maintains the list of OLE client items which are in the view. CRichEditCntrItem provides container-side access to the OLE client item.

This Windows Common control (and therefore the CRichEditCtrl and related classes) is available only to programs running under Windows 95/98 and Windows NT versions 3.51 and later.

For an example of using a rich edit view in an MFC application, see the WORDPAD sample application.

CObject

CCmdTarget

CWnd

CView

CCtrlView

CRichEditView

Header: afxrich.h

Call this function to move the given dialog box so that it does not obscure the current selection.

void AdjustDialogPosition(CDialog* pDlg);

Parameters

pDlg
Pointer to a CDialog object.

Call this function to determine if the Clipboard contains information that can be pasted into this rich edit view.

BOOL CanPaste() const;

 

Return Value

Nonzero if the Clipboard contains data in a format which this rich edit view can accept; otherwise, 0.

Call this function to create a CRichEditView object.

CRichEditView();

Call this function to paste the OLE item in dataobj into this rich edit document/view.

void DoPaste(
    COleDataObject& dataobj,  
    CLIPFORMAT cf,  
    HMETAFILEPICT hMetaPict);

Parameters

dataobj
The COleDataObject containing the data to paste.

cf
The desired Clipboard format.

hMetaPict
The metafile that represents the item to be pasted.

Remarks

The framework calls this function as part of the default implementation of QueryAcceptData.

This function determines the type of paste based on the results of the handler for Paste Special. If cf is 0, the new item uses the current iconic representation. If cf is nonzero and hMetaPict is not NULL, the new item uses hMetaPict for its representation.

Call this function to find the specified text and set it to be the current selection.

BOOL FindText(
    LPCTSTR lpszFind,  
    BOOL bCase = TRUE,  
    BOOL bWord = TRUE,  
    BOOL bNext = TRUE);

Parameters

lpszFind
Contains the string to search for.

bCase
Indicates if the search is case sensitive.

bWord
Indicates if the search should match whole words only, not parts of words.

bNext
Indicates the direction of the search. If TRUE, the search direction is toward the end of the buffer. If FALSE, the search direction is toward the beginning of the buffer.

Return Value

Nonzero if the lpszFind text is found; otherwise 0.

Remarks

This function displays the wait cursor during the find operation.

Example

void CMyRichEditView::OnReplaceAll(LPCTSTR lpszFind, LPCTSTR lpszReplace, 
   BOOL bCase, BOOL bWord)
{
   CWaitCursor wait;
   // no selection or different than what we are looking for
   if (!FindText(lpszFind, bCase, bWord))
   {
      OnTextNotFound(lpszFind);
      return;
   }

   GetRichEditCtrl().HideSelection(TRUE, FALSE);
   m_nNumReplaced = 0;
   do
   {
      GetRichEditCtrl().ReplaceSel(lpszReplace);
      m_nNumReplaced++;  // Record the number of replacements

   } while (FindTextSimple(lpszFind));
   GetRichEditCtrl().HideSelection(FALSE, FALSE);
}

Call this function to find the specified text and set it to be the current selection.

BOOL FindTextSimple(
    LPCTSTR lpszFind,  
    BOOL bCase = TRUE,  
    BOOL bWord = TRUE,  
    BOOL bNext = TRUE);

Parameters

lpszFind
Contains the string to search for.

bCase
Indicates if the search is case sensitive.

bWord
Indicates if the search should match whole words only, not parts of words.

bNext
Indicates the direction of the search. If TRUE, the search direction is toward the end of the buffer. If FALSE, the search direction is toward the beginning of the buffer.

Return Value

Nonzero if the lpszFind text is found; otherwise 0.

Example

See the example for CRichEditView::FindText.

Call this function to get the character formatting attributes of the current selection.

CHARFORMAT2& GetCharFormatSelection();

Return Value

A CHARFORMAT2 structure which contains the character formatting attributes of the current selection.

Remarks

For more information, see the EM_GETCHARFORMAT message and the CHARFORMAT2 structure in the Windows SDK.

Example

void CMyRichEditView::OnCharUnderline ()
{
    CHARFORMAT2 cf;
    cf = GetCharFormatSelection();

    if (!(cf.dwMask & CFM_UNDERLINE) || !(cf.dwEffects & CFE_UNDERLINE))
        cf.dwEffects = CFE_UNDERLINE;
    else
        cf.dwEffects = 0;

    cf.dwMask = CFM_UNDERLINE;
    SetCharFormat(cf);
}

The framework calls this function as part of the processing of IRichEditOleCallback::GetClipboardData.

virtual HRESULT GetClipboardData(
    CHARRANGE* lpchrg,  
    DWORD dwReco,  
    LPDATAOBJECT lpRichDataObj,  
    LPDATAOBJECT* lplpdataobj);

Parameters

lpchrg
Pointer to the CHARRANGE structure specifying the range of characters (and OLE items) to copy to the data object specified by lplpdataobj.

dwReco
Clipboard operation flag. Can be one of these values.

  • RECO_COPY Copy to the Clipboard.

  • RECO_CUT Cut to the Clipboard.

  • RECO_DRAG Drag operation (drag and drop).

  • RECO_DROP Drop operation (drag and drop).

  • RECO_PASTE Paste from the Clipboard.

lpRichDataObj
Pointer to an IDataObject object containing the Clipboard data from the rich edit control ( IRichEditOle::GetClipboardData).

lplpdataobj
Pointer to the pointer variable that receives the address of the IDataObject object representing the range specified in the lpchrg parameter. The value of lplpdataobj is ignored if an error is returned.

Return Value

An HRESULT value reporting the success of the operation. For more information on HRESULT, see Structure of COM Error Codes in the Windows SDK.

Remarks

If the return value indicates success, IRichEditOleCallback::GetClipboardData returns the IDataObject accessed by lplpdataobj; otherwise, it returns the one accessed by lpRichDataObj. Override this function to supply your own Clipboard data. The default implementation of this function returns E_NOTIMPL.

This is an advanced overridable.

For more information, see IRichEditOle::GetClipboardData, IRichEditOleCallback::GetClipboardData, and CHARRANGE in the Windows SDK and see IDataObject in the Windows SDK.

The framework calls this function as part of the processing of IRichEditOleCallback::GetContextMenu.

virtual HMENU GetContextMenu(
    WORD seltyp,  
    LPOLEOBJECT lpoleobj,  
    CHARRANGE* lpchrg);

Parameters

seltyp
The selection type. The selection type values are described in the Remarks section.

lpoleobj
Pointer to a OLEOBJECT structure specifying the first selected OLE object if the selection contains one or more OLE items. If the selection contains no items, lpoleobj is NULL. The OLEOBJECT structure holds a pointer to an OLE object v-table.

lpchrg
Pointer to a CHARRANGE structure containing the current selection.

Return Value

Handle to the context menu.

Remarks

This function is a typical part of right mouse-button down processing.

The selection type can be any combination of the following flags:

  • SEL_EMPTY Indicates that there is no current selection.

  • SEL_TEXT Indicates that the current selection contains text.

  • SEL_OBJECT Indicates that the current selection contains at least one OLE item.

  • SEL_MULTICHAR Indicates that the current selection contains more than one character of text.

  • SEL_MULTIOBJECT Indicates that the current selection contains more than one OLE object.

The default implementation returns NULL. This is an advanced overridable.

For more information, see IRichEditOleCallback::GetContextMenu and CHARRANGE in the Windows SDK.

For more information on the OLEOBJECT type, see the OLE Data Structures and Structure Allocation article in the OLE Knowledge Base.

Call this function to get a pointer to the CRichEditDoc associated with this view.

CRichEditDoc* GetDocument() const;

 

Return Value

Pointer to a CRichEditDoc object associated with your CRichEditView object.

Call this function to get the OLE item that is currently activated in place in this CRichEditView object.

CRichEditCntrItem* GetInPlaceActiveItem() const;

 

Return Value

A pointer to the single, in-place active CRichEditCntrItem object in this rich edit view; NULL if there is no OLE item currently in the in-place active state.

Call this function to retrieve the current margins used in printing.

CRect GetMargins() const;

 

Return Value

The margins used in printing, measured in MM_TWIPS.

Call this function to get the dimensions of the page used in printing.

CRect GetPageRect() const;

 

Return Value

The bounds of the page used in printing, measured in MM_TWIPS.

Remarks

This value is based on the paper size.

Call this function to retrieve the current paper size.

CSize GetPaperSize() const;

 

Return Value

The size of the paper used in printing, measured in MM_TWIPS.

Example

void CMyRichEditView::OnPrint(CDC* pDC, CPrintInfo* pInfo) 
{
   UNREFERENCED_PARAMETER(pInfo);

   // Get the current paper size and construct an actual printing
   // rectangle by leaving out one half inch margin from each side.
   CSize sizePaper = GetPaperSize();
   CRect rectMargins(720, 720, sizePaper.cx - 720, 
      sizePaper.cy - 720);

   // Need to set the margins when printing from CRichEditView
   SetMargins(rectMargins);

   // Set up three rectangular regions spaced an inch apart
   CRect rectHeader(0, 0, rectMargins.right, 1440);
   CRect rectBody(0, 1440, rectMargins.right, 1440 * 2);
   CRect rectFooter(0, 1440 * 2, rectMargins.right, 1440 * 3);

   // Format the first 10 characters in the buffer.
   int nSavedDC = pDC->SaveDC();
   PrintInsideRect(pDC, rectHeader, 0, 10, TRUE); // characters 0-10
   pDC->RestoreDC(nSavedDC);

   // Format the second 10 characters in the buffer.
   nSavedDC = pDC->SaveDC();
   PrintInsideRect(pDC, rectBody, 10, 20, TRUE); // characters 10-20
   pDC->RestoreDC(nSavedDC);

   // Format the third 10 characters in the buffer.
   nSavedDC = pDC->SaveDC();
   PrintInsideRect(pDC, rectFooter, 20, 30, TRUE); // characters 20-30
   pDC->RestoreDC(nSavedDC);   

//   CRichEditView::OnPrint(pDC, pInfo);
}

Call this function to get the paragraph formatting attributes of the current selection.

PARAFORMAT2& GetParaFormatSelection();

Return Value

A PARAFORMAT2 structure which contains the paragraph formatting attributes of the current selection.

Remarks

For more information, see EM_GETPARAFORMAT message and PARAFORMAT2 structure in the Windows SDK.

Call this function to retrieve the bounds of the printing area within the page rectangle.

CRect GetPrintRect() const;

 

Return Value

The bounds of the image area used in printing, measured in MM_TWIPS.

Example

void CMyRichEditView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
   CRect rectPrintPage = GetPrintRect();  // Measured in MM_TWIPS

   pInfo->SetMaxPage((m_nDocSizeInInches * 1440)/rectPrintPage.Height());

   CRichEditView::OnBeginPrinting(pDC, pInfo);
}

Call this function to determine the width of the printing area.

int GetPrintWidth() const;

 

Return Value

The width of the printing area, measured in MM_TWIPS.

Call this function to retrieve the CRichEditCtrl object associated with the CRichEditView object.

CRichEditCtrl& GetRichEditCtrl() const;

 

Return Value

The CRichEditCtrl object for this view.

Example

See the example for CRichEditView::FindText.

Call this function to retrieve the OLE item (a CRichEditCntrItem object) currently selected in this CRichEditView object.

CRichEditCntrItem* GetSelectedItem() const;

 

Return Value

Pointer to a CRichEditCntrItem object selected in the CRichEditView object; NULL if no item is selected in this view.

Call this function to retrieve the length of the text in this CRichEditView object.

long GetTextLength() const;

 

Return Value

The length of the text in this CRichEditView object.

Call this member function to calculate the length of the text in this CRichEditView object.

long GetTextLengthEx(
    DWORD dwFlags,  
    UINT uCodePage = -1) const;

 

Parameters

dwFlags
Value specifying the method to be used in determining the text length. This member can be one or more of the values listed in the flags member of GETTEXTLENGTHEX described in the Windows SDK.

uCodePage
Code page for translation (CP_ACP for ANSI Code Page, 1200 for Unicode).

Return Value

The number of characters or bytes in the edit control. If incompatible flags were set in dwFlags, this member function returns E_INVALIDARG.

Remarks

GetTextLengthEx provides additional ways of determining the length of the text. It supports the Rich Edit 2.0 functionality. For more information, see About Rich Edit Controls in the Windows SDK.

Call this function to insert the specified file (as a CRichEditCntrItem object) into a rich edit view.

void InsertFileAsObject(LPCTSTR lpszFileName);

Parameters

lpszFileName
String containing the name of the file to be inserted.

Call this function to insert a CRichEditCntrItem object into a rich edit view.

HRESULT InsertItem(CRichEditCntrItem* pItem);

Parameters

pItem
Pointer to the item to be inserted.

Return Value

An HRESULT value indicating the success of the insertion.

Remarks

For more information on HRESULT, see Structure of COM Error Codes in the Windows SDK.

Call this function to determine if cf is a Clipboard format which is text, rich text, or rich text with OLE items.

static BOOL AFX_CDECL IsRichEditFormat(CLIPFORMAT cf);

Parameters

cf
The Clipboard format of interest.

Return Value

Nonzero if cf is a rich edit or text Clipboard format.

Call this function to determine if the specified OLE item is currently selected in this view.

virtual BOOL IsSelected(const CObject* pDocItem) const;

 

Parameters

pDocItem
Pointer to an object in the view.

Return Value

Nonzero if the object is selected; otherwise 0.

Remarks

Override this function if your derived view class has a different method for handling selection of OLE items.

The indentation for bullet items in a list; by default, 720 units, which is 1/2 inch.

int m_nBulletIndent;  

Indicates the type of word wrap for this rich edit view.

int m_nWordWrap;  

Remarks

One of the following values:

  • WrapNone Indicates no automatic word wrapping.

  • WrapToWindow Indicates word wrapping based on the width of the window.

  • WrapToTargetDevice Indicates word wrapping based on the characteristics of the target device.

Example

See the example for CRichEditView::WrapChanged.

Call this function to toggle the character formatting effects for the current selection.

void OnCharEffect(
    DWORD dwMask,  
    DWORD dwEffect);

Parameters

dwMask
The character formatting effects to modify in the current selection.

dwEffect
The desired list of character formatting effects to toggle.

Remarks

Each call to this function toggles the specified formatting effects for the current selection.

For more information on the dwMask and dwEffect parameters and their potential values, see the corresponding data members of CHARFORMAT in the Windows SDK.

Example

void CMyRichEditView::OnItalic()
{
   OnCharEffect( CFM_ITALIC, CFE_ITALIC );
}

Called by the framework when processing commands from the Find/Replace dialog box.

virtual void OnFindNext(
    LPCTSTR lpszFind,  
    BOOL bNext,  
    BOOL bCase,  
    BOOL bWord);

Parameters

lpszFind
The string to find.

bNext
The direction to search: TRUE indicates down; FALSE, up.

bCase
Indicates whether the search is to be case sensitive.

bWord
Indicates whether the search is to match whole words only or not.

Remarks

Call this function to find text within the CRichEditView. Override this function to alter search characteristics for your derived view class.

Called by the framework after the view is first attached to the document, but before the view is initially displayed.

virtual void OnInitialUpdate();

Remarks

The default implementation of this function calls the CView::OnUpdate member function with no hint information (that is, using the default values of 0 for the lHint parameter and NULL for the pHint parameter). Override this function to perform any one-time initialization that requires information about the document. For example, if your application has fixed-sized documents, you can use this function to initialize a view's scrolling limits based on the document size. If your application supports variable-sized documents, use OnUpdate to update the scrolling limits every time the document changes.

Example

See the example for CRichEditView::m_nWordWrap.

Use this function to load native data from an embedded item.

virtual BOOL OnPasteNativeObject(LPSTORAGE lpStg);

Parameters

lpStg
Pointer to an IStorage object.

Return Value

Nonzero if successful; otherwise, 0;

Remarks

Typically, you would do this by creating a COleStreamFile around the IStorage. The COleStreamFile can be attached to an archive and CObject::Serialize called to load the data.

This is an advanced overridable.

For more information, see IStorage in the Windows SDK.

Call this function to change the paragraph alignment for the selected paragraphs.

void OnParaAlign(WORD wAlign);

Parameters

wAlign
Desired paragraph alignment. One of the following values:

  • PFA_LEFT Align the paragraphs with the left margin.

  • PFA_RIGHT Align the paragraphs with the right margin.

  • PFA_CENTER Center the paragraphs between the margins.

Example

void CMyRichEditView::OnParaCenter()
{
   OnParaAlign(PFA_CENTER);
}

Override this function to change characteristics for this rich edit view when the printer changes.

virtual void OnPrinterChanged(const CDC& dcPrinter);

Parameters

dcPrinter
A CDC object for the new printer.

Remarks

The default implementation sets the paper size to the physical height and width for the output device (printer). If there is no device context associated with dcPrinter, the default implementation sets the paper size to 8.5 by 11 inches.

Called by the framework when processing Replace All commands from the Replace dialog box.

virtual void OnReplaceAll(
    LPCTSTR lpszFind,  
    LPCTSTR lpszReplace,  
    BOOL bCase,  
    BOOL bWord);

Parameters

lpszFind
The text to be replaced.

lpszReplace
The replacement text.

bCase
Indicates if the search is case sensitive.

bWord
Indicates if the search must select whole words or not.

Remarks

Call this function to replace all occurrences of some given text with another string. Override this function to alter search characteristics for this view.

Example

See the example for CRichEditView::FindText.

Called by the framework when processing Replace commands from the Replace dialog box.

virtual void OnReplaceSel(
    LPCTSTR lpszFind,  
    BOOL bNext,  
    BOOL bCase,  
    BOOL bWord,  
    LPCTSTR lpszReplace);

Parameters

lpszFind
The text to be replaced.

bNext
Indicates the direction of the search: TRUE is down; FALSE, up.

bCase
Indicates if the search is case sensitive.

bWord
Indicates if the search must select whole words or not.

lpszReplace
The replacement text.

Remarks

Call this function to replace one occurrence of some given text with another string. Override this function to alter search characteristics for this view.

Called by the framework whenever a search fails.

virtual void OnTextNotFound(LPCTSTR lpszFind);

Parameters

lpszFind
The text which was not found.

Remarks

Override this function to change the output notification from a MessageBeep.

For more information, see MessageBeep in the Windows SDK.

Example

void CMyRichEditView::OnTextNotFound(LPCTSTR lpszFind)
{
   // Replace the beep with a message box
   CString str;
   str.Format(_T("'%s' was not found."), lpszFind);
   AfxMessageBox(str);
}

The framework calls this function to update the command UI for character effect commands.

void OnUpdateCharEffect(
    CCmdUI* pCmdUI,  
    DWORD dwMask,  
    DWORD dwEffect);

Parameters

pCmdUI
Pointer to a CCmdUI object.

dwMask
Indicates the character formatting mask.

dwEffect
Indicates the character formatting effect.

Remarks

The mask dwMask specifies which character formatting attributes to check. The flags dwEffect list the character formatting attributes to set/clear.

For more information on the dwMask and dwEffect parameters and their potential values, see the corresponding data members of CHARFORMAT in the Windows SDK.

Example

void CMyRichEditView::OnUpdateCharItalicUI(CCmdUI* pCmdUI)
{
   OnUpdateCharEffect(pCmdUI, CFM_ITALIC, CFE_ITALIC);
}

The framework calls this function to update the command UI for paragraph effect commands.

void OnUpdateParaAlign(
    CCmdUI* pCmdUI,  
    WORD wAlign);

Parameters

pCmdUI
Pointer to a CCmdUI object.

wAlign
The paragraph alignment to check. One of the following values:

  • PFA_LEFT Align the paragraphs with the left margin.

  • PFA_RIGHT Align the paragraphs with the right margin.

  • PFA_CENTER Center the paragraphs between the margins.

Example

void CMyRichEditView::OnUpdateParaCenterUI(CCmdUI* pCmdUI)
{
   OnUpdateParaAlign(pCmdUI, PFA_CENTER);
}

Call this function to format a range of text in a rich edit control to fit within rectLayout for the device specified by pDC.

long PrintInsideRect(
    CDC* pDC,  
    RECT& rectLayout,  
    long nIndexStart,  
    long nIndexStop,  
    BOOL bOutput);

Parameters

pDC
Pointer to a device context for the output area.

rectLayout
RECT or CRect which defines the output area.

nIndexStart
Zero-based index of the first character to be formatted.

nIndexStop
Zero-based index of the last character to be formatted.

bOutput
Indicates if the text should be rendered. If FALSE, the text is just measured.

Return Value

The index of the last character that fits in the output area plus one.

Remarks

Typically, this call is followed by a call to CRichEditCtrl::DisplayBand which generates the output.

Example

See the example for CRichEditView::GetPaperSize.

Call this function to format a range of text in a rich edit control for the output device specified by pDC.

long PrintPage(
    CDC* pDC,  
    long nIndexStart,  
    long nIndexStop);

Parameters

pDC
Pointer to a device context for page output.

nIndexStart
Zero-based index of the first character to be formatted.

nIndexStop
Zero-based index of the last character to be formatted.

Return Value

The index of the last character that fits on the page plus one.

Remarks

The layout of each page is controlled by GetPageRect and GetPrintRect. Typically, this call is followed by a call to CRichEditCtrl::DisplayBand which generates the output.

Note that margins are relative to the physical page, not the logical page. Thus, margins of zero will often clip the text since many printers have unprintable areas on the page. To avoid clipping your text, you should call SetMargins and set reasonable margins before printing.

Called by the framework to paste an object into the rich edit.

virtual HRESULT QueryAcceptData(
    LPDATAOBJECT lpdataobj,  
    CLIPFORMAT* lpcfFormat,  
    DWORD dwReco,  
    BOOL bReally,  
    HGLOBAL hMetaFile);

Parameters

lpdataobj
Pointer to the IDataObject to query.

lpcfFormat
Pointer to the acceptable data format.

dwReco
Not used.

bReally
Indicates if the paste operation should continue or not.

hMetaFile
A handle to the metafile used for drawing the item's icon.

Return Value

An HRESULT value reporting the success of the operation.

Remarks

Override this function to handle different organization of COM items in your derived document class. This is an advanced overridable.

For more information on HRESULT and IDataObject, see Structure of COM Error Codes and IDataObject, respectively, in the Windows SDK.

Example

// This code fragment is taken from the Wordpad sample.
HRESULT CMyRichEditView::QueryAcceptData(LPDATAOBJECT lpdataobj, 
   CLIPFORMAT* lpcfFormat, DWORD dwReco, BOOL bReally, HGLOBAL hMetaFile)
{
   if (bReally && *lpcfFormat == 0 && (m_nPasteType == 0))
   {
      COleDataObject dataobj;
      dataobj.Attach(lpdataobj, FALSE);
      if (!dataobj.IsDataAvailable(cfRTO)) // native avail, let 
                                           // richedit do as it wants
      {
         if (dataobj.IsDataAvailable(cfEmbeddedObject))
         {
            if (PasteNative(lpdataobj)) // See WordPad sample for info
                                        // on PasteNative
               return S_FALSE;
         }
      }
   }
   return CRichEditView::QueryAcceptData(lpdataobj, lpcfFormat, dwReco, 
      bReally, hMetaFile);
}

Call this function to set the character formatting attributes for new text in this CRichEditView object.

void SetCharFormat(CHARFORMAT2 cf);

Parameters

cf
CHARFORMAT2 structure containing the new default character formatting attributes.

Remarks

Only the attributes specified by the dwMask member of cf are changed by this function.

For more information, see EM_SETCHARFORMAT message and CHARFORMAT2 structure in the Windows SDK.

Example

void CMyRichEditView::OnCharUnderline ()
{
    CHARFORMAT2 cf;
    cf = GetCharFormatSelection();

    if (!(cf.dwMask & CFM_UNDERLINE) || !(cf.dwEffects & CFE_UNDERLINE))
        cf.dwEffects = CFE_UNDERLINE;
    else
        cf.dwEffects = 0;

    cf.dwMask = CFM_UNDERLINE;
    SetCharFormat(cf);
}

Call this function to set the printing margins for this rich edit view.

void SetMargins(const CRect& rectMargin);

Parameters

rectMargin
The new margin values for printing, measured in MM_TWIPS.

Remarks

If m_nWordWrap is WrapToTargetDevice, you should call WrapChanged after using this function to adjust printing characteristics.

Note that the margins used by PrintPage are relative to the physical page, not the logical page. Thus, margins of zero will often clip the text since many printers have unprintable areas on the page. To avoid clipping your text, you should call use SetMargins to set reasonable printer margins before printing.

Example

See the example for CRichEditView::GetPaperSize.

Call this function to set the paper size for printing this rich edit view.

void SetPaperSize(CSize sizePaper);

Parameters

sizePaper
The new paper size values for printing, measured in MM_TWIPS.

Remarks

If m_nWordWrap is WrapToTargetDevice, you should call WrapChanged after using this function to adjust printing characteristics.

Example

BOOL CMyRichEditView::OnPreparePrinting(CPrintInfo* pInfo)
{
   // Set the printing margins (720 twips = 1/2 inch).
   SetMargins(CRect(720, 720, 720, 720));

   // Change the paper orientation to landscape mode
   // See the example for CWinApp::GetPrinterDeviceDefaults
   ((CMyWinApp*)AfxGetApp())->SetLandscapeMode();

   // Change the paper size in the CRichEditView to 
   // reflect landscape mode
   CSize csPaper = GetPaperSize();
   int temp;
   temp = csPaper.cx; csPaper.cx = csPaper.cy; csPaper.cy = temp;
   SetPaperSize(csPaper);

   return DoPreparePrinting(pInfo);
}

Call this function to set the paragraph formatting attributes for the current selection in this CRichEditView object.

BOOL SetParaFormat(PARAFORMAT2& pf);

Parameters

pf
PARAFORMAT2 structure containing the new default paragraph formatting attributes.

Return Value

Nonzero if successful; otherwise, 0.

Remarks

Only the attributes specified by the dwMask member of pf are changed by this function.

For more information, see EM_SETPARAFORMAT message and PARAFORMAT2 structure in the Windows SDK.

Example

void CMyRichEditView::AddBullets() 
{
   PARAFORMAT2 pf;

   pf.cbSize = sizeof(PARAFORMAT2);
   pf.dwMask = PFM_NUMBERING | PFM_OFFSET;
   pf.wNumbering = PFN_BULLET;
   pf.dxOffset = 10;

   VERIFY(SetParaFormat(pf));
}

Call this function to reset the internal search state of the CRichEditView control after a failed call to FindText.

void TextNotFound(LPCTSTR lpszFind);

Parameters

lpszFind
Contains the text string that was not found.

Remarks

It is recommended that this method be called immediately after failed calls to FindText so that the internal search state of the control is properly reset.

The lpszFind parameter should include the same content as the string provided to FindText. After resetting the internal search state, this method will call the OnTextNotFound method with the provided search string.

Example

See the example for CRichEditView::FindText.

Call this function when the printing characteristics have changed ( SetMargins or SetPaperSize).

virtual void WrapChanged();

Remarks

Override this function to modify the way the rich edit view responds to changes in m_nWordWrap or the printing characteristics ( OnPrinterChanged).

Example

void CMyRichEditView::OnInitialUpdate()
{
   CRichEditView::OnInitialUpdate();

   // Turn on the horizontal scroll bar
   m_nWordWrap = WrapNone;
   WrapChanged();
}

MFC Sample WORDPAD
CCtrlView Class
Hierarchy Chart
CRichEditDoc Class
CRichEditCntrItem Class

Show: