RICH EDIT

I   -   The beginning app.

  1.    Start Visual C++

    a)  Click File -> New.
        From  the Projects tab, click MFC AppWizard(exe) give your project the
        name: RichText. Specify the directory where you want to create your app.

    b)  Accept all defaults in MFC AppWizard Steps 1, 2, 3, 4, and 5.

    c)  In MFC AppWizard Step 6 of 6, change the name of your view class from 
        CExoView, its header file to ExoView.h, its Implementation file to 
        ExoView.cpp, base it on CRichEditView.
        Then, click Finish.
        Click OK after reading the warning.

    d)  The New Project Information dialog shows up. Read it, then click OK.

    e)  From the Visual Studio main menu.
        Click Build -> Set Active Configuration...
        Double-click RichText - Win32 Release

  2.    This exo is strictly about text formatting, so we will spare any 
        of the usual fantasies.

    a)  But to make it less boring, let's change the default font.
        From the WorkSpace, click the FileView tab. Expand the RichText files.
        Expand the Source Files, and double-click ExoView.cpp to open the 
        implementation of the view class. Change it as follows:

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

	CRichEditCtrl& rCtrl = GetRichEditCtrl();
	CHARFORMAT	cfm;

	cfm.cbSize = sizeof(cfm);
	cfm.dwMask = CFM_SIZE | CFM_ITALIC | CFM_BOLD | CFM_UNDERLINE 
		| CFM_STRIKEOUT | CFM_PROTECTED | CFM_FACE;
	cfm.dwEffects = 0;
	cfm.yHeight = 240;

	strcpy (cfm.szFaceName, "Times New Roman");
	rCtrl.SetDefaultCharFormat(cfm);

	// Set the printing margins (720 twips = 1/2 inch).
	SetMargins(CRect(720, 720, 720, 720));
}

    b)  Also, open the IDR_RICHTETYPE menu. Inclucde a menu called F&ormat
        between the View and the Window menus. Then, under the new Format
        menu, create a submenu captioned &Font...

II  -   We will use only the existing toolbar (feel free to create another 
        toolbar if you want).

  1.    From the WorkSpace, click the ResourceView tab, make sure that the 
        RichText resources folder. Expand the Toolbar folder and 
        double-click the IDR_MAINFRAME toolbar.
        Create/design the six new buttons that we will use on the toolbar.
        Identify these buttons as:
        ID_CHAR_BOLD, ID_CHAR_ITALIC, ID_CHAR_UNDERLINE, ID_CHAR_STRIKEOUT,
        separator, ID_PARA_LEFT, ID_PARA_CENTER, ID_PARA_RIGHT:

2. Now, we will programmatically format character, paragraph, and text. a) The easiest way to use these buttons would be to use their features only from the menu, and they would produce the exact same result. But they would not be easier for the user since we know that a menu is farther than the toolbar. So, we will use the buttons from the toolbar. Unfortunately, we need to give them a visual effect to help the user identify their effect. If you don't care about the visual/boolean behavior of the buttons, then igonre everything on this section. i)In your view header, declare four boolean elements, one for each button. Also, create an enumeration that will be used to control to three paragraph formatting buttons so that they can behave like radio buttons:
protected: BOOL bBold; BOOL bItalic; BOOL bUnderline; BOOL bStrikeout; enum JUST { LEFT = 0, CENTER, RIGHT } m_Justify; ii)In the constructor of the view class, specify the paragraph alignment to be on left at start up, and initialize the boolean buttons as false because we don't want them to look/appear pressed at start up. CExoView::CExoView() { m_Justify = LEFT; bBold = FALSE; bItalic = FALSE; bUnderline = FALSE; bStrikeout = FALSE; } b) From the Visual Studio main menu, click View -> ClassWizard. We will use the View class as the basis of our implementation. So choose the CExoView as the Class name in the combo box. In the Object IDs listbox, choose ID_CHAR_BOLD. In the Messages listbox double-click COMMAND and accept the suggested name for the function by clicking OK. Then, double-click UPDATE_COMMAN_UI and accept the suggested function name. Repeat the same thing for ID_CHAR_ITALIC, ID_CHAR_UNDERLINE, and ID_CHAR_STRIKEOUT. c) Do the same for the paragraph formatting buttons: d) For each character formatting button, we change the state of the button from whatever state it is when the user clicks it. Then we call the CRichEditView::OnCharEffect() function that has all the functionality we need for the particular button. The paragraph formatting buttons behave like radio buttons in a groub, that means only one in the group can be pressed at a particular time. But one of their differences from the other first buttons is that their state doesn't change when they are pressed, only when another button in the group is clicked. So, when the user clicks the button, you don't change its state, you keep it the way it is. Implement all these functions as follows: void CExoView::OnCharBold() { bBold = !bBold; OnCharEffect(CFM_BOLD, CFE_BOLD); } void CExoView::OnUpdateCharBold(CCmdUI* pCmdUI) { pCmdUI->SetCheck(bBold); } void CExoView::OnCharItalic() { bItalic = !bItalic; OnCharEffect(CFM_ITALIC, CFM_ITALIC); } void CExoView::OnUpdateCharItalic(CCmdUI* pCmdUI) { pCmdUI->SetCheck(bItalic); } void CExoView::OnCharUnderline() { bUnderline = !bUnderline; OnCharEffect(CFM_UNDERLINE, CFE_UNDERLINE); } void CExoView::OnUpdateCharUnderline(CCmdUI* pCmdUI) { pCmdUI->SetCheck(bUnderline); } void CExoView::OnCharStrikeout() { bStrikeout = !bStrikeout; OnCharEffect(CFM_STRIKEOUT, CFE_STRIKEOUT); } void CExoView::OnUpdateCharStrikeout(CCmdUI* pCmdUI) { pCmdUI->SetCheck(bStrikeout); } void CExoView::OnParaLeft() { m_Justify = LEFT; OnParaAlign(PFA_LEFT); } void CExoView::OnUpdateParaLeft(CCmdUI* pCmdUI) { pCmdUI->SetRadio(m_Justify == LEFT); } void CExoView::OnParaCenter() { m_Justify = CENTER; OnParaAlign(PFA_CENTER); } void CExoView::OnUpdateParaCenter(CCmdUI* pCmdUI) { pCmdUI->SetRadio(m_Justify == CENTER); } void CExoView::OnParaRight() { m_Justify = RIGHT; OnParaAlign(PFA_RIGHT); } void CExoView::OnUpdateParaRight(CCmdUI* pCmdUI) { pCmdUI->SetRadio(m_Justify == RIGHT); }

Copyright © 1998 FunctionX