I   -   Let's create the starting app first.

  1.    Start Visual C++.

    a)  From the main menu, click File -> New.
        In the New dialog, choose the Projects tab.
        In the Project name box, type ExoRich.
        Specify a directory in the Location box; then, click OK.

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

    c)  In MFC AppWizard - Step 6 of 6, Change the Class name of the view to be 
        CRichView, the Header file will be called RichView.h, and the
        implementation file will be RichView.cpp. Base the view on the 
        CRichEditView class. Click Finish.
        You receive a warning or message telling that your app needs 
        OLE support...After you have read it, click OK.

    d)  Examine the New Project Information dialog to see the files and 
        resources that AppWizard is about to create for you. 
        If everything is fine, click OK.

    e)  In the Visual Studio main menu, 
        click Build -> Set Active Configuration...
        Click ExoRich - Win32 Release and click OK.

  2.    We will create the additional resources for our app. We mainly need a 
        toolbar to hold our formatting buttons.

    a)  From the WorkSpace, click the ResourceView tab.
        Expand the ExoRich resources. Right-click the Toolbar folder and choose 
        Insert Toolbar from the popup menu.
        Right-click the new IDR_TOOLBAR1 and choose Properties from the menu.
        Click the Pushpin on the Properties dialog because we will use it a lot.
        Change the identification of the IDR_TOOLBAR1 to IDR_FORMATBAR. 
        Press Enter.

    b)  Design the buttons identified as: 

c) Modify the IDR_EXORICTYPE to put a menu between View and Window. Call it F&ormat. Include a submenu under Format captioned as &Font... Under the same menu, include other items on the format toolbar.
Also, since we will use a second toolbar, include a call to it under the View menu, captioned as &Format Bar.
d) Let's change the default font (the System font). Right-click in the Editor window and choose ClassWizard. From the MFC ClassWizard, choose the Message Maps tab. Specify the Class name as CRichView; in the Object IDs box, choose CRichView In the Messages box, find and click WM_CREATE; then click the Add Function button. Click the Edit Code button and implement it: int CRichView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CRichEditView::OnCreate(lpCreateStruct) == -1) return -1; 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; lstrcpy (cfm.szFaceName, "Times New Roman"); rCtrl.SetDefaultCharFormat(cfm); return 0; } e) From the WorkSpace, click the ClassView tab and double-click CMainframe to access its header file. Declare a new toolbar for the formatbar, call it m_wndFormatBar; protected: // control bar embedded members CStatusBar m_wndStatusBar; CToolBar m_wndToolBar; CToolBar m_wndFormatBar; f) In the CMainFrame::OnCreate() function, create/implement the formatbar, simply imitating the m_wndToolBar creation: g) Get back to the ClassWizard. From the Message Maps tab, specify the Class name as CMainFrame. In the Object IDs: box, find and click ID_VIEW_FORMATBAR. In the Messages box, click COMMAND and then click the Add Function button. Change the name of the suggested function to OnViewFormatBar. Click Edit Code. We will handle the toolbar menu by checking its state to verify whether it is displayed or not. To do that, we will dynamically create a menu and use the CMenu::GetMenuState function. Once the GetMenuState function is called, we decide whether to show the format bar or not. void CMainFrame::OnViewFormatBar() { // Declare a pointer to the menu class CMenu* m_pMenu; // Get the menu m_pMenu = GetMenu(); // If the FormatBar is checked, hide it; then uncheck it if( m_pMenu->GetMenuState(ID_VIEW_FORMATBAR, MF_CHECKED) ) { ShowControlBar(&m_wndFormatBar, FALSE, FALSE); m_pMenu->CheckMenuItem(ID_VIEW_FORMATBAR, MF_UNCHECKED); } // Otherwise, show it else { ShowControlBar(&m_wndFormatBar, TRUE, FALSE); m_pMenu->CheckMenuItem(ID_VIEW_FORMATBAR, MF_CHECKED); } } h) The only thing left now is to use the formatting features of the CRichEditView class. As the following magic will show, we will not write a single line of code. In the WorkSpace, click the FileView tab. Expand the ExoRich files. Expand the Source Files folder, and double-click RichView.cpp to access its implementation. In its Message Map section, use a command call to tell CRichEditView that you have a button(for example ID_CHAR_BOLD) that you want to associate with a corresponding function(for example OnCharBold) for appropriate formatting. Simply type the following: BEGIN_MESSAGE_MAP(CRichView, CRichEditView) //{{AFX_MSG_MAP(CRichView) ON_WM_DESTROY() ON_WM_CREATE() ON_COMMAND(ID_CHAR_BOLD, OnCharBold) ON_UPDATE_COMMAND_UI(ID_CHAR_BOLD, OnUpdateCharBold) ON_COMMAND(ID_CHAR_ITALIC, OnCharItalic) ON_UPDATE_COMMAND_UI(ID_CHAR_ITALIC, OnUpdateCharItalic) ON_COMMAND(ID_CHAR_UNDERLINE, OnCharUnderline) ON_UPDATE_COMMAND_UI(ID_CHAR_UNDERLINE, OnUpdateCharUnderline) ON_COMMAND(ID_INSERT_BULLET, OnBullet) ON_COMMAND(ID_PARA_LEFT, OnParaLeft) ON_UPDATE_COMMAND_UI(ID_PARA_LEFT, OnUpdateParaLeft) ON_COMMAND(ID_PARA_CENTER, OnParaCenter) ON_UPDATE_COMMAND_UI(ID_PARA_CENTER, OnUpdateParaCenter) ON_COMMAND(ID_PARA_RIGHT, OnParaRight) ON_UPDATE_COMMAND_UI(ID_PARA_RIGHT, OnUpdateParaRight) //}}AFX_MSG_MAP


Copyright © 1998 FunctionX, Inc.