Click here to Skip to main content
11,479,034 members (63,259 online)
Click here to Skip to main content

Simple Wordpad in Managed C++

, 9 Aug 2003 CPOL 96.6K 3.4K 19
Rate this:
Please Sign up or sign in to vote.
Using Managed C++ and the RichTextBox for a simple Wordpad application
<!-- Add the rest of your HTML here -->

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

Share

About the Author

R.selvam
Software Developer (Senior)
India India
Selvam has worked on several technologies like Core Java, VC++, MFC, Windows API and Weblogic server. He takes a lot of interest in reading technical articles and enjoy 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.

Github : https://github.com/selvamselvam
Web site: http://www15.brinkster.com/selvamselvam
Follow on   LinkedIn

Comments and Discussions

 
Generalnice work:) but there is an important question Pin
no_limits16-Aug-04 2:12
memberno_limits16-Aug-04 2:12 
GeneralRe: nice work:) but there is an important question Pin
Alexandru Matei2-Nov-10 22:17
memberAlexandru Matei2-Nov-10 22:17 
GeneralRe: nice work:) but there is an important question Pin
R.selvam9-Nov-10 2:36
memberR.selvam9-Nov-10 2:36 
GeneralGOOD JOB Pin
Sarah_Nina9-Sep-03 16:45
sussSarah_Nina9-Sep-03 16:45 
GeneralRe: GOOD JOB Pin
R.selvam2-Oct-03 19:48
memberR.selvam2-Oct-03 19:48 
GeneralQuestion Pin
ArunVijay14-Nov-03 7:12
memberArunVijay14-Nov-03 7:12 
GeneralRe: Question Pin
R.selvam17-Nov-03 6:31
memberR.selvam17-Nov-03 6:31 
GeneralRe: GOOD JOB Pin
anysiddiqui8-Mar-07 22:03
memberanysiddiqui8-Mar-07 22:03 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 10 Aug 2003
Article Copyright 2003 by R.selvam
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid