What we have in this page?
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:
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:
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.
The A, R, G, and B properties can be used to access the components of the Color.
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.
Value is true if the font has bold style.
Gets the font family that this font belongs to.
Gets the height of the font in the current units.
Value is true if the font has italic style.
Gets the name of the font.
Gets the size of the font in the current units.
Gets the size of the font in points. A point equals one seventy- second of an inch.
Value is true if the font has strikeout style.
Gets the style information for the font.
Value is true if the font has underline style.
Gets the unit of measure for this font. By default, this will be in pixels.
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;
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);
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:
Display, which uses one seventy-fifth of an inch as the unit of measure.
Document, which uses document units (one three-hundredth of an inch).
Point, which uses printer’s points (one seventy-second of an inch).
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;
else if (e->Button == ::CppDraw::MouseButtons::Right)
// Draw text
gr->DrawString(L"Right click Text", font1, Brushes::Black, (float)e->X, (float)e->Y);
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.
Note that the text isn’t persistent. Unlike the lines, it will disappear when the window gets repainted.