Home Index  | Back

New Page 1
Lesson 1 -Building Your First Windows Application

1.1 : Creating a Windows Application

1.2 : Creating a Dialog-Based Application

1.3 : Creating DLLs, Console Applications, and More

1.4 : Changing Your AppWizard Decisions
 1.5 : Understanding AppWizard’s Code
 1.6 : Understanding a MDI Application
 1.7 : Understanding the Components of a Dialog-Based Application

Lesson 2 - Dialogs and Controls

2.1 : Understanding Dialog Boxes

2.2 : Creating a Dialog Box Resource

 2.3 : Writing a Dialog Box Class
 2.4 : Using the Dialog Box Class
Lesson 3 - Messages and Commands

3.1 : Understanding Message Routing

3.2 : Understanding Message Loops

 3.3 : Reading Message Maps
 3.4 : Learning How ClassWizard Helps You Catch Messages
 3.5 : Recognizing Messages
 3.6 : Understanding Commands
 3.7 : Understanding Command Updates
 3.8 : Learning How ClassWizard Helps You Catch Commands and Command Updates

Lesson 4 - Documents and Views

4.1 : Understanding the Document Class

4.2 : Understanding the View Class

4.3 : Creating the Rectangles Application

 4.4 : Other View Classes

4.5 : Document Templates, Views, and Frame Windows

Lesson 5 - Drawing on the Screen

5.1 :Understanding Device Contexts

 5.2 : Introducing the Paint1 Application
 5.3 : Building the Paint1 Application
 5.4 : Scrolling Windows
 5.5 : Building the Scroll Application
Lesson 6 - Building a Complete Application: ShowString

6.1 : Building an Application That Displays a String

 6.2 : Building the ShowString Menus
 6.3 : Building the ShowString Dialog Boxes
 6.4 : Making the Menu Work
 6.5 : Making the Dialog Box Work
 6.6 : Adding Appearance Options to the Options Dialog Box
Lesson 7 -  Status Bars and Toolbars

7.1 : Working with Toolbars

 7.2 : Working with Status Bars
Lesson 8 - Common Controls

8.1 : The Progress Bar Control

 8.2 : The Up-Down Control
 8.3 : The Image List Control
 8.4 : The List View Control
 8.5 : The Tree View Control
 8.6 : The Rich Edit Control
 8.7 : The Date Picker Control
 8.8 : Month Calendar Control
 8.9 : Scrolling the View
Lesson 9 - Property Pages and Sheets

9.1 : Introducing Property Sheets

 9.2 : Creating the Property Sheet Demo Application
 9.3 : Running the Property Sheet Demo Application
Lesson 10 - ActiveX Concepts

10.1 : The Purpose of ActiveX

10.2 : Object Linking

10.3 : Object Embedding

 10.4 : Containers and Servers
 10.5 : Toward a More Intuitive User Interface
 10.6 : The Component Object Model  

10.7 : Automation

 10.8 : ActiveX Controls

Lesson 11 -  Building an ActiveX Control

11.1 : Creating a Rolling-Die Control

11.2 : Displaying the Current Value

11.3 : Reacting to a Mouse Click and Rolling the Die 

 11.4 : Creating a Better User Interface
 11.5 : Generating Property Sheets
Lesson 12 - Database Access

12.1 : Understanding Database Concepts

12.2 : Creating an ODBC Database Program

 12.3 : Choosing Between ODBC and DAO
 12.4 : OLE DB

Lesson -8:Common Controls

8.6- The Rich Edit Control

If you took all the energy expended on writing text-editing software and you concentrated that energy on other, less mundane programming problems, computer science would probably be a decade ahead of where it is now. Although that might be an exaggeration, it is true that when it comes to text editors, a huge amount of effort has been dedicated to reinventing the wheel. Wouldn’t it be great to have one piece of text-editing code that all programmers could use as the starting point for their own custom text editors?

With Visual C++’s CRichEditCtrl control, you get a huge jump on any text-editing functionality that you need to install in your applications. The rich edit control is capable of handling fonts, paragraph styles, text color, and other types of tasks that are traditionally found in text editors. In fact, a rich edit control (named for the fact that it handles text in Rich Text Format) provides a solid starting point for any text-editing tasks that your application must handle. Your users can

· Type text.

· Edit text, using cut-and-paste and sophisticated drag-and-drop operations.

· Set text attributes such as font, point size, and color.

· Apply underline, bold, italic, strikethrough, superscript, and subscript properties to text.

· Format text, using various alignments and bulleted lists.

· Lock text from further editing.

· Save and load files.

As you can see, a rich edit control is powerful. It is, in fact, almost a complete word-processor-in-a-box that you can plug into your program and use immediately. Of course, because a rich edit control offers so many features, there’s a lot to learn. This section gives you a quick introduction to creating and manipulating a rich edit control.

8.6.1 Creating the Rich Edit Control

Add a call to CreateRichEdit() to the view class’s OnCreate() function and then add the function to the class. Listing 8.8 shows the code you should add to the function. Add resource IDs for IDC_RICHEDIT, IDC_RICHEDIT_ULINE, IDC_RICHEDIT_LEFT, IDC_RICHEDIT_CENTER, and IDC_RICHEDIT_RIGHT.

Listing 8.8 CommonView.cpp—CCommonView::CreateRichEdit()

void CCommonView::CreateRichEdit()

{

m_richEdit.Create(WS_CHILD | WS_VISIBLE | WS_BORDER |

ES_AUTOVSCROLL | ES_MULTILINE,

CRect(180, 260, 393, 360), this, IDC_RICHEDIT);

m_boldButton.Create("ULine", WS_VISIBLE | WS_CHILD | WS_BORDER,

CRect(400, 260, 450, 280), this, IDC_RICHEDIT_ULINE);

m_leftButton.Create("Left", WS_VISIBLE | WS_CHILD | WS_BORDER,

CRect(400, 285, 450, 305), this, IDC_RICHEDIT_LEFT);

m_centerButton.Create("Center", WS_VISIBLE | WS_CHILD | WS_BORDER,

CRect(400, 310, 450, 330), this, IDC_RICHEDIT_CENTER);

m_rightButton.Create("Right", WS_VISIBLE | WS_CHILD | WS_BORDER,

CRect(400, 335, 450, 355), this, IDC_RICHEDIT_RIGHT);

}

As usual, things start with a call to the control’s Create() member function. The style constants include the same constants that you would use for creating any type of window, with the addition of special styles used with rich edit controls. Table 8.4 lists these special styles.

Table 8.4 Rich Edit Styles

Style Description
ES_AUTOHSCROLL Automatically scrolls horizontally
ES_AUTOVSCROLL Automatically scrolls vertically
ES_CENTER Centers text
ES_LEFT Left-aligns text
ES_LOWERCASE Lowercases all text
ES_MULTILINE Enables multiple lines
ES_NOHIDESEL Doesn’t hide selected text when losing the focus
ES_OEMCONVERT Converts from ANSI characters to OEM characters and back to ANSI
ES_PASSWORD Displays characters as asterisks
ES_READONLY Disables editing in the control
ES_RIGHT Right-aligns text
ES_UPPERCASE Uppercases all text
ES_WANTRETURN Inserts return characters into text when Enter is pressed

8.6.2 Initializing the Rich Edit Control

The rich edit control is perfectly usable as soon as it is created. Member functions manipulate the control extensively, formatting and selecting text, enabling and disabling many control features, and more.

8.6.3 Manipulating the Rich Edit Control

This sample application shows you the basics of using the rich edit control by setting character attributes and paragraph formats. When you include a rich edit control in an application, you will probably want to give the user some control over its contents. For this reason, you usually create menu and toolbar commands for selecting the various options that you want to support in the application. In Common, the user can click four buttons to control the rich edit control.

You’ve already added the code to create these buttons. Add lines to the message map in the header file to declare the handlers:

afx_msg void OnULine();

afx_msg void OnLeft();

afx_msg void OnCenter();

afx_msg void OnRight();

Similarly, add these lines to the message map in the source file:

ON_COMMAND(IDC_RICHEDIT_ULINE, OnULine)

ON_COMMAND(IDC_RICHEDIT_LEFT, OnLeft)

ON_COMMAND(IDC_RICHEDIT_CENTER, OnCenter)

ON_COMMAND(IDC_RICHEDIT_RIGHT, OnRight)

Each of these functions is simple. Add them each to CommonView.cpp. OnULine() looks like this:

void CCommonView::OnULine()

{

CHARFORMAT charFormat;

charFormat.cbSize = sizeof(CHARFORMAT);

charFormat.dwMask = CFM_UNDERLINE;

m_richEdit.GetSelectionCharFormat(charFormat);

if (charFormat.dwEffects & CFM_UNDERLINE)

charFormat.dwEffects = 0;

else

charFormat.dwEffects = CFE_UNDERLINE;

m_richEdit.SetSelectionCharFormat(charFormat);

m_richEdit.SetFocus();

}

OnULine() creates and initializes a CHARFORMAT structure, which holds information about character formatting and is declared in Listing 8.9.

Listing 8.9 The CHARFORMAT Structure, Defined by MFC

typedef struct _charformat

{

UINT cbSize;

_WPAD _wPad1;

DWORD dwMask;

DWORD dwEffects;

LONG yHeight;

LONG yOffset;

COLORREF crTextColor;

BYTE bCharSet;

BYTE bPitchAndFamily;

TCHAR szFaceName[LF_FACESIZE];

_WPAD _wPad2;

} CHARFORMAT;

In a CHARFORMAT structure, cbSize is the size of the structure. dwMask indicates which members of the structure are valid (can be a combination of CFM_BOLD, CFM_CHARSET, CFM_COLOR, CFM_FACE, CFM_ITALIC, CFM_OFFSET, CFM_PROTECTED, CFM_SIZE, CFM_STRIKEOUT, and CFM_UNDERLINE). dwEffects is the character effects (can be a combination of CFE_AUTOCOLOR, CFE_BOLD, CFE_ITALIC, CFE_STRIKEOUT, CFE_UNDERLINE, and CFE_PROTECTED). yHeight is the character height, and yOffset is the character baseline offset (for super- and subscript characters). crTextColor is the text color. bCharSet is the character set value (see the ifCharSet member of the LOGFONT structure). bPitchAndFamily is the font pitch and family, and szFaceName is the font name.

After initializing the CHARFORMAT structure, as needed, to toggle underlining, OnULine() calls the control’s GetSelectionCharFormat() member function. This function, whose single argument is a reference to the CHARFORMAT structure, fills the character format structure. OnULine() checks the dwEffects member of the structure to determine whether to turn underlining on or off. The bitwise and operator, &, is used to test a single bit of the variable.

Finally, after setting the character format, OnULine() returns the focus to the rich edit control. By clicking a button, the user has removed the focus from the rich edit control. You don’t want to force the user to keep switching back manually to the control after every button click, so you do it by calling the control’s SetFocus() member function.

Common also enables the user to switch between the three types of paragraph alignment. This is accomplished similarly to toggling character formats. Listing 8.10 shows the three functions—OnLeft(), OnRight(), and OnCenter()—that handle the alignment commands. Add the code for these functions to CommonView.cpp. As you can see, the main difference is the use of the PARAFORMAT structure instead of CHARFORMAT and the call to SetParaFormat() instead of SetSelectionCharFormat().

Listing 8.10 CommonView.cpp—Changing Paragraph Formats

void CCommonView::OnLeft()

{

PARAFORMAT paraFormat;

paraFormat.cbSize = sizeof(PARAFORMAT);

paraFormat.dwMask = PFM_ALIGNMENT;

paraFormat.wAlignment = PFA_LEFT;

m_richEdit.SetParaFormat(paraFormat);

m_richEdit.SetFocus();

}

void CCommonView::OnCenter()

{

PARAFORMAT paraFormat;

paraFormat.cbSize = sizeof(PARAFORMAT);

paraFormat.dwMask = PFM_ALIGNMENT;

paraFormat.wAlignment = PFA_CENTER;

m_richEdit.SetParaFormat(paraFormat);

m_richEdit.SetFocus();

}

void CCommonView::OnRight()

{

PARAFORMAT paraFormat;

paraFormat.cbSize = sizeof(PARAFORMAT);

paraFormat.dwMask = PFM_ALIGNMENT;

paraFormat.wAlignment = PFA_RIGHT;

m_richEdit.SetParaFormat(paraFormat);

m_richEdit.SetFocus();

}

After adding all that code, it’s time to build and test again. First, click in the text box to give it the focus. Then, start typing. Want to try out character attributes? Click the ULine button to add underlining to either selected text or the next text you type. To try out paragraph formatting, click the Left, Center, or Right button to specify paragraph alignment. (Again, if you’re using large text, adjust the button size if the labels don’t fit.) Figure 8.8 shows the rich edit control with some different character and paragraph styles used.


FIG. 8.8 A rich edit control is almost a complete word processor.

 

Next>>
 
© Dewsoft Overseas