Click here to Skip to main content
15,867,835 members
Articles / Desktop Programming / MFC
Article

Simple Wordpad in Managed C++

Rate me:
Please Sign up or sign in to vote.
4.14/5 (9 votes)
9 Aug 2003CPOL4 min read 115.3K   3.9K   19   8
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.

Image 1

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.
C++
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.

C++
//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,

C++
// 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.
C++
// 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
Selvam has worked on several technologies like Java, Python, Big data, VC++, MFC, Windows API and Weblogic server. He takes a lot of interest in reading technical articles and enjoys writing them too. He has been awarded as a Microsoft Community Star in 2004, MVP in 2005-06, SCJP 5.0 in 2009, Microsoft Community Contributor(MCC) 2011.

Big Data
o Google Professional Data Engineer 2021
o Confluent Certified Developer for Apache Kafka 2020
o Datastax Apache Cassandra 3.x Developer Associate Certification 2020
✓ Cloud
o Google Professional Cloud Architect 2021
o Microsoft Certified: Azure Solutions Architect Expert 2020
o AWS Certified Solutions Architect - Associate 2020
✓ Oracle Certified Master, Java EE 6 Enterprise Architect (OCMEA) 2018

Github : https://github.com/selvamselvam
Web site: http://www.careerdrill.com
Linkedin: https://www.linkedin.com/in/selvamselvam/

Comments and Discussions

 
Generalnice work:) but there is an important question Pin
no_limits16-Aug-04 1:12
no_limits16-Aug-04 1:12 
GeneralRe: nice work:) but there is an important question Pin
Alexandru Matei2-Nov-10 21:17
Alexandru Matei2-Nov-10 21:17 
Hi,

I am also interested.

Have you found the answer to this question ?

How to paste simple tables (e.g. two rows, two columns) from Microsoft Word into this editor?
GeneralRe: nice work:) but there is an important question Pin
Selvam R9-Nov-10 1:36
professionalSelvam R9-Nov-10 1:36 
GeneralGOOD JOB Pin
Sarah_Nina9-Sep-03 15:45
sussSarah_Nina9-Sep-03 15:45 
GeneralRe: GOOD JOB Pin
Selvam R2-Oct-03 18:48
professionalSelvam R2-Oct-03 18:48 
GeneralQuestion Pin
ArunVijay14-Nov-03 6:12
ArunVijay14-Nov-03 6:12 
GeneralRe: Question Pin
Selvam R17-Nov-03 5:31
professionalSelvam R17-Nov-03 5:31 
GeneralRe: GOOD JOB Pin
anysiddiqui8-Mar-07 21:03
anysiddiqui8-Mar-07 21:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.