Flylib.com

Books Software

 
 
 

5.7 A Simple Text Editor

5.7 A Simple Text Editor

Now let's see how to write a simple text editor in just a few minutes, using the functionality we have discussed in this chapter so far.

First we create a Windows application and add some controls to the form. As Figure 5.19 shows, we add two label controls and set their Text properties to Available Fonts and Size , respectively. Then we add a combo box, a NumericUpDown control, and two button controls with the Text properties set to Color and Apply , respectively. We will use the combo box control to display all installed fonts, the NumericUpDown control to set the size of text, and the Color button to set the text color. We also add a RichTextBox control to the form and size it appropriately.

Figure 5.19. A simple text editor application

graphics/05fig19.gif

Now we add the following line to our application:


using System.Drawing.Text;

We also add two private variables of types Color and int , respectively, as follows :


private Color textColor; private int textSize;

Finally, we double-click on the form and insert the code from Listing 5.15 on the form-load event handler, thereby setting the NumericUpDown control's Value property to 10 and adding all installed fonts to the combo box control.

Listing 5.15 The form-load event handler
private void Form1_Load(object sender, System.EventArgs e) { numericUpDown1.Value = 10; // Create InstalledFontCollection object InstalledFontCollection sysFontCollection = new InstalledFontCollection(); // Get the array of FontFamily objects FontFamily[] fontFamilies = sysFontCollection.Families; // Read all font familes and // add to the combo box foreach (FontFamily ff in fontFamilies) { comboBox1.Items.Add(ff.Name); } comboBox1.Text = fontFamilies[0].

Name

; }

The Color button click event handler simply calls ColorDialog , which allows the user to pick the text color (see Listing 5.16).

Listing 5.16 Getting color from ColorDialog
private void button1_Click(object sender, System.EventArgs e) { // Create a color dialog and let // the user select a color. // Save the selected color. ColorDialog colorDlg = new ColorDialog(); if(colorDlg.ShowDialog() == DialogResult.OK) { textColor = colorDlg.Color; } }

The Apply button reads the selected font name from the combo box and the size from the NumericUpDown control. Then it creates a Font object using the font family name and size. Finally, we set the ForeColor and Font properties of the RichTextBox control (see Listing 5.17).

Listing 5.17 Setting the font and foreground color of RichTextBox
private void button2_Click(object sender, System.EventArgs e) { // Get size of text from // the numeric

up-down

control textSize = (int)numericUpDown1.Value; // Get current font name from the list string selFont = comboBox1.Text; // Create new font from the current selection Font textFont = new Font(selFont, textSize); // Set color and font of

rich-text

box richTextBox1.ForeColor = textColor; richTextBox1.Font = textFont; }

By extending this simple application and the RichTextBox features, you can develop a complete text editor with features that include open and save, find, change font styles, and so on. We'll leave this to you as an exercise!

5.8 Transforming Text

Transformation is a process of moving objects from one place to another by applying a series of operations such as scaling, rotation, and translation. In this section we will see how to transform text using the Graphics object.

Transformation using Graphics class methods and properties is pretty simple. The Graphics class provides the methods ScaleTransform , RotateTransform , TranslateTransform and others.

Note

See Chapter 10 for detailed information about transformations and how to use various transformation techniques.


Let's look at a simple yet useful example of text transformation. First we draw some text on a form using the code in Listing 5.18.

Listing 5.18 Drawing text on a form
Graphics g = e.Graphics; string str = "Colors, fonts, and text are common elements "+ "of graphics programming. In this chapter, you learned " + " about the colors, fonts, and text representations in the "+ ".NET Framework class library. You learned how to create "+ "these elements and use them in GDI+."; g.DrawString(str, new Font("Verdana", 10), new SolidBrush(

Color

.Blue), new Rectangle(50,20,200,300) );

Figure 5.20 shows the output of Listing 5.18. The text is drawn normally.

Figure 5.20. Drawing text on a form

graphics/05fig20.jpg

Now let's scale the text using the ScaleTransform method by writing the following line before the DrawString method call. Scaling changes the text size by application of a scaling factor. For example, the following code line doubles the size of text. This code must be added before the DrawString method call:


g.ScaleTransform(2, 1);

Now our text on the form looks like Figure 5.21. It is scaled to twice the regular size.

Figure 5.21. Using ScaleTransform to scale text

graphics/05fig21.jpg

Now let's rotate the text, which we can do by calling the RotateTransform method, which takes a rotation angle. We rotate the text 45 degrees by adding the following line before the DrawString method call:


g.RotateTransform(45.0f, System.Drawing.Drawing2D.MatrixOrder.Prepend);

Now the text on the form looks like Figure 5.22. It is rotated from its previous position.

Figure 5.22. Using RotateTransform to rotate text

graphics/05fig22.jpg

Finally, let's call TranslateTransform , which takes two values related to the x - and y -axes. We add the following line after RotateTransform :


g.TranslateTransform(-20, -70);

and our final form looks like Figure 5.23. The text has been moved (or "translated") from its previous position.

Figure 5.23. Using TranslateTransform to translate text

graphics/05fig23.jpg