5.7 A Simple Text EditorNow 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
Figure 5.19. A simple text editor application
Now we add the following line to our application: using System.Drawing.Text;
We also add two private
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].
The
Color
button click event handler simply calls
ColorDialog
, which allows the
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
By extending this simple application and the
RichTextBox
features, you can develop a complete text editor with features that include
|
5.8 Transforming TextTransformation 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
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(
Figure 5.20 shows the output of Listing 5.18. The text is drawn normally. Figure 5.20. Drawing text on a form
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
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
Figure 5.22. Using RotateTransform to rotate text
Finally, let's call
TranslateTransform
, which takes two values
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
|