Drawing strings almost doesn't require a section of its own. All it really is, is a single call to the DrawString() method found in the Graphics class. The more difficult part of drawing strings is setting up the font and color you want to print with. (I cover both topics later.)
Now you'll take a quick peek at the DrawString() method. If you were to look at the .NET Framework documentation, you'd find a plethora of overloads. When you examine them more closely, you'll discover that they all start with the parameters String, Font, and Brush. From there, it gets a little tricky because you have to decide if you just want to specify the starting upper-left corner of where you want the string displayed, using either (x, y) coordinates or a Point, or specify the entire rectangle that you want to restrict the string to.
g.DrawString(string, font, brush, xF, yF); g.DrawString(string, font, brush, pointF); g.DrawString(string, font, brush, rectangleF);
When you restrict the string to a rectangle, the text automatically word wraps, as Listing 11-9 shows. It unfortunately will also show half of a line of text if the vertical height is not enough.
Listing 11-9: Drawing a String to a Rectangle
namespace StringRect { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; public __gc class Form1 : public System::Windows::Forms::Form { public: Form1(void) //... protected: void Dispose(Boolean disposing) //... private: System::ComponentModel::Container * components; void InitializeComponent(void) { this->AutoScaleBaseSize = System::Drawing::Size(6, 15); this->ClientSize = System::Drawing::Size(292, 265); this->Name = S"Form1"; this->Text = S"String in a Rectangle"; this->Paint += new System::Windows::Forms::PaintEventHandler(this, Form1_Paint); } private: System::Void Form1_Paint(System::Object * sender, System::Windows::Forms::PaintEventArgs * e) { Graphics *g = e->Graphics; // Draw the string g->DrawString(S"Let's draw string to a rectangle and go a little " S"overboard on the size of the string that we place " S"inside of it", new Drawing::Font(new FontFamily(S"Arial"), 10), Brushes::Black, Drawing::RectangleF(20.0, 40.0, 260.0, 50.0)); } }; }
Figure 11-9 shows that StringRect.exe draws a string to a rectangle that is too small.
Figure 11-9: A string restricted to a too-small rectangle
In reality, each of the overloads for the DrawString() method listed previously has one more parameter of type StringFormat, which has been defaulted to GenericDefault.
g.DrawString(string, font, brush, xF, yF, stringformat); g.DrawString(string, font, brush, pointF, stringformat); g.DrawString(string, font, brush, rectangleF, stringformat);
StringFormat is a class containing several properties (see Table 11-9) that allow the DrawString() method to do things such as draw the text vertically and left, right, or center align it.
PROPERTY | DESCRIPTION |
---|---|
Alignment | Specifies alignment of the text |
FormatFlags | Specifies StringFormatFlags such as DirectionVertical and NoWrap |
GenericDefault | A static method that gets the generic default StringFormat object |
GenericTypographic | A static method that gets the generic typographic StringFormat object |
LineAlignment | Specifies line alignment |
Trimming | Specifies how to trim a string that doesn't fit completely within a display area |
Listing 11-10 shows the same text as shown previously, but this time it is written in a downward direction and centered on each line.
Listing 11-10: Drawing Strings Downward in a Rectangle
namespace DownwardStringRect { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; public __gc class Form1 : public System::Windows::Forms::Form { public: Form1(void) //... protected: void Dispose(Boolean disposing) //... private: System::ComponentModel::Container * components; void InitializeComponent(void) { this->AutoScaleBaseSize = System::Drawing::Size(6, 15); this->ClientSize = System::Drawing::Size(292, 265); this->Name = S"Form1"; this->Text = S"Downward String in a Rectangle"; this->Paint += new System::Windows::Forms::PaintEventHandler(this,Form1_Paint); } private: System::Void Form1_Paint(System::Object * sender, System::Windows::Forms::PaintEventArgs * e) { Graphics *g = e->Graphics; // create and configure the StringFormat object StringFormat *stringformat = new StringFormat(); stringformat->FormatFlags = StringFormatFlags::DirectionVertical; stringformat->Alignment = StringAlignment::Center; // Draw the string g->DrawString(S"Let's draw a string to a rectangle and go a little " S"overboard on the size of the string that we place " S"inside of it", new Drawing::Font(new FontFamily(S"Arial"), 10), Brushes::Black, Drawing::RectangleF(20.0, 40.0, 242.0, 80.0), stringformat); } }; }
Figure 11-10 shows that DownwardStringRect.exe draws a string in a downward direction and centers it in a rectangle that is too small. This causes the string to be clipped on the final line.
Figure 11-10: A string drawn downward and restricted to a too-small rectangle