< C++ .NET Drawing & Printing 3 | Main | C++ .NET Drawing & Printing 5 >


 

 

Graphical Output: Simple Drawing and Printing 4

 

 

What we have in this page?

 

  1. Using Color

  2. Using Fonts

 

Using Color

 

Colors are represented by the System::Drawing::Color structure and are formed from four components: R, G, B, and A. The first three represent the proportions of red, green, and blue in the color and are specified as an integer with a value from 0 (none) to 255 (maximum). Pure red would, therefore, have a value of (255, 0, 0). The A value represents the transparency, or alpha value, of the color and can vary from 0 (fully transparent) to 255 (fully opaque). Like Graphics, the Color structure doesn’t have a constructor. You create Color instances by using one of the three static factory methods:

  1. FromArgb, which creates a Color from a set of alpha, red, green, and blue values.

  2. FromKnownColor, which creates a Color from a member of the KnownColor enumeration (discussed in a moment)

  3. FromName, which creates a color given a name.

The Color class defines a large number of properties that represent system-defined colors, ranging from AliceBlue to YellowGreen. The KnownColor enumeration defines a list of system-defined colors. These colors include the standard colors defined in Color (such as AliceBlue) as well as values representing colors used in the Windows GUI, such as ActiveCaption (the color of the title bar on an active window) and WindowText (the color of the text in a window). The values of these colors might change if the user alters the desktop color scheme using Control Panel. Here are some examples showing how to create colors:

// Pure red, fully opaque

Color c1 = Color::FromArgb(255, 255, 0, 0);

 

// The 'Cornsilk' standard color

Color c2 = Color::FromKnownColor(KnownColor::Cornsilk);

 

// Another way to get Cornsilk

Color c3 = Color::Cornsilk;

Note that once a Color has been constructed, you can’t change its values. If you need to change a Color, you’ll have to create a new Color based on the existing one.

// Pure red, fully opaque

Color c4 = Color::FromArgb(255, 255, 0, 0);

 

// Pure red, semi-transparent

Color c5 = Color::FromArgb(127, c4.R, c4.G, c4.B);

The A, R, G, and B properties can be used to access the components of the Color.

 

Using Fonts

 

Two classes in System::Drawing are used to represent fonts: Font and FontFamily. A Font object represents a single font and specifies the font name, the size, and the style attributes. A FontFamily defines a set of fonts that share many characteristics but which might differ in font style. The following table lists the major properties of the Font class. Note that they are all read-only; once created, a Font’s properties can’t be changed.

 

Property

Description

Bold

Value is true if the font has bold style.

FontFamily

Gets the font family that this font belongs to.

Height

Gets the height of the font in the current units.

Italic

Value is true if the font has italic style.

Name

Gets the name of the font.

Size

Gets the size of the font in the current units.

SizeInPoints

Gets the size of the font in points. A point equals one seventy- second of an inch.

Strikeout

Value is true if the font has strikeout style.

Style

Gets the style information for the font.

Underline

Value is true if the font has underline style.

Unit

Gets the unit of measure for this font. By default, this will be in pixels.

 

Table 3

 

 

 

 

The Style is composed of one or more values from the FontStyle enumeration, which has the members Bold, Italic, Regular, Strikeout, and Underline. The following short exercise will demonstrate how to create and use a Font to draw text on a form. You’ll enhance the existing application so that using the left button will draw lines and using the right button will draw a text string.

 

23.     Add a Font member to the Form1 class.

private: System::Drawing::Font^ font1;

 

Adding a Font member to the Form1 class

 

24.     You’ll need to use the fully qualified name, or the declaration will conflict with the form’s Font property. Create a Font object in the Form1 constructor.

// Create the Font

font1 = gcnew System::Drawing::Font("Verdana", 8, FontStyle::Regular, GraphicsUnit::Millimeter);

 

Creating a Font object in the Form1 constructor

 

Once again, you need to use the fully qualified name to avoid name clashes. The first argument is the name of the font, followed by the size. What the size represents depends on the last argument, which represents the units to be used. The GraphicsUnit enumeration contains all the valid units, which can be any of the following:

  1. Display, which uses one seventy-fifth of an inch as the unit of measure.

  2. Document, which uses document units (one three-hundredth of an inch).

  3. Inch.

  4. Millimeter.

  5. Pixel.

  6. Point, which uses printer’s points (one seventy-second of an inch).

  7. World, which uses world coordinates.

In this case, we are specifying 8 millimeters, which will be easily visible on the form. The font style can be any of the values from the FontStyle enumeration, combined with the bitwise OR operator (|) if you want to use more than one of them.

 

25.     Edit the Form1_MouseUp function so that it differentiates between the left and right mouse buttons.

 

private: System::Void Form1_MouseUp(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e)

{

      Graphics^ gr = CreateGraphics();

      Pen^ pen1 = gcnew Pen(Color::Black);

 

      if (e->Button == ::CppDraw::MouseButtons::Left)

      {

            // Draw lines

            p2.X = e->X;

            p2.Y = e->Y;

 

            gr->DrawLine(pen1, p1.X,p1.Y, p2.X,p2.Y);

 

            // Add a new line to the list

            Line^ pl = gcnew Line();

            pl->p1 = p1;

            pl->p2 = p2;

            list->Add(pl);

      }

           

      else if (e->Button == ::CppDraw::MouseButtons::Right)

      {

            // Draw text

            gr->DrawString(L"Right click Text", font1, Brushes::Black, (float)e->X, (float)e->Y);

      }

      delete gr;

}

 

 

 

 

Editing the Form1_MouseUp function so that it differentiates between the left and right mouse buttons

 

The function now checks which mouse button gave rise to the event. If it’s the left button, a line gets drawn. If it’s the right button, a string is drawn at the mouse position using the font you just created. The third argument to DrawString is a Brush, which defines the fill color or pattern for the text.

 

26.     Build and run the application. You should be able to draw lines and text using the left and right mouse buttons respectively.

 

Drawing lines and text using the left and right mouse buttons respectively

 

Note that the text isn’t persistent. Unlike the lines, it will disappear when the window gets repainted.

 

 

Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6

 

 


< C++ .NET Drawing & Printing 3 | Main | C++ .NET Drawing & Printing 5 >