Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Simple Wordpad in Managed C++

0.00/5 (No votes)
9 Aug 2003 1  
Using Managed C++ and the RichTextBox for a simple Wordpad application

Introduction

Microsoft Visual C++ .NET is a powerful language compared to other .NET enabled languages like C#, VB .NET etc. It is the only language that supports both managed and unmanaged C++.

Simple WordPad is an application that uses the RichTextBox control in Windows Forms. The RichTextBox control can be used to create, edit, save and open different file formats such as Document files, RichText format files, Plain text files and DOS based text files, including those with embedded images.

The main use of RichTextBox is formatting text, changing color, and all the operations support by TextBox. This project uses CommonDialog controls also. The TextBox displays Horizontal and Vertical Scrollbars by default. But the RichTextBox by default displays a vertical scrollbar only.

Note:

This project was developed with Microsoft (R) 32-bit C/C++ Standard Compiler Version 13.00.9466 for 80x86 and compiled with the /CLR switch on the command line. This project did not use the Microsoft .NET IDE.

RichTextBox Control

The RichTextBox supports all the features of the TextBox Control with some new features added. We can change the font type or font colors independently like Microsoft Word format embedded with images. WordWrap properties are also supported in the RichTextBox control. The default WordWrap property in the RichTextBox Control is true. If the WordWrap Property is true, the text automatically adjusts with the Horizontal scrollbar. If the property is set to false, the WordWrap property is disabled.

The class hierarchy for the RichTextBox Class is

System.Object
    System.MarshalByRefObject
        System.ComponentModel.Component
            System.Windows.Forms.Control
                System.Windows.Forms.TextBoxBase
                    System.Windows.Forms.RichTextBox
The RichTextBox is derived from the TextBox base class thus inheriting all the functionality of the TextBox Class.
RichTextBox *m_pRichText = new RichTextBox();
m_pRichText-> Dock = DockStyle::Fill;
m_pRichText ->Font = new System::Drawing::Font(
                             new FontFamily(S"Times New Roman"), 12.0f );
m_pRichText ->Size = System::Drawing::Size(600, 380);
m_pRichText ->Dock = DockStyle::Fill;
m_pRichText ->TabIndex = 1;
m_pRichText ->Multiline = true;
m_pRichText ->WordWrap =false;
m_pRichText ->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
The above code creates a RichTextBox object. We then set and get the properties to the object through the m_pRichText pointer. The Font property is used to initialize the font and the Size property is used to set the size of the RichTextBox control. In our example, the Border style is Fixed3D Style in the object.

For further reading about RichTextBox,

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWindowsFormsRichTextBoxClassTopic.asp

Setting Bullet, Indent and Alignment

The RichTextBox Control has a Bullet property like Microsoft Word. When the SelectionBullet property is true, the bullet is inserted in the selected text. If it is false, the bullet is removed from the selected text.

The Indent property usage procedure is the same as for the Bullet property.

//Set selected text as bullet format

m_pRichText->SelectionBullet  =  true;

// Set selected text as Indent

m_pRichText->SelectionIndent = 20;

// Alignment of RichTextBox

m_pRichText->SelectionAlignment  = HorizontalAlignment::Right;
In our example, if the SelectionIndent property is set positive the selected whole text will move towards the right and vice versa.

For example,

// Text moves to left

m_pRichText->SelectionIndent =  - 20;
The SelectionAlignment property aligns the text or image in the RichTextBox control. The alignment supports three types - Left, Right or Center. These Bullet, Indent and Alignment properties are applicable only for selected (or cursor position) text.

CommonDialog Classes

The CommonDialog Class provides a standard set of dialog boxes for operations such as opening and saving files, setting print options and selecting colors and fonts. In our application we use four CommonDialog Classes.
// Opens the common colordialog

ColorDialog *m_pColorDialog = new ColorDialog();
    
// Opens the common Font Dialog

FontDialog *m_pFontDialog = new FontDialog();

// Opens the Commom OpenFileDialog

OpenFileDialog *m_pFileOpenDialog = new OpenFileDialog();

//Opens the Common FileSaveDialog

SaveFileDialog *m_pFileSaveDialog = new SaveDialog();
Each CommonDialog has it�s own methods and properties but all the CommonDialog controls support ShowDialog property. The ShowDialog property is used to show the particular Common Dialog control.

The ColorDialog Commondialog provides service to the user, to select the Color from the color palette and can select any Custom Color. The System.Windows.Forms.ColorDialog is derived from the CommonDialog Base Class System.Windows.Forms.CommonDialog.

The FontDialog CommonDialog allows the user to select from the list of fonts installed in the Windows operating System. It is also possible to choose the font color. When font size and font color of the selected text is changed, the text is changed to the respective color and size. Strikeout and underline are also used in the same manner.

The OpenDialog CommonDialog provides services to open the Document file, Rich Text File, Text file and DOS based text Files. The LoadFile property in the OpenDialog CommonDialog method loads the file to the RichTextBox. The SaveDialog CommonDialog SaveFile property saves document format, text formats, and RichText format files in the RichTextBox.

Note: For further reading about, CommonDialog Class,

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsfontdialogclasstopic.asp

About the Sample code:

With a simple WordPad application we can create, edit, open and save the RichTextBox enabled formats (* .doc, *.txt, * .rtf). The edit menu is used for normal clipboard operations such as Cut, Copy, Paste, Select All and Clear operations with special RichTextBox Redo, Undo operations.

The Options menu is used to change the ForegroundColor, BackgroundColor and Font Style. The RichTextBox menu has bullet, indent and the alignments for selected text.

Summary

The Managed C++ language is a C++ language to access the .NET framework directly with /CLR compiler option for C++ programmers. The Managed C++ is the only .NET-enabled language which support both managed code and unmanaged code. The disadvantage of C++ is complexity compared to other .NET enabled languages like C# and VB .NET.

For further reference about Managed C++

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmex/html/vcconMCOverview.asp

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here