In a previous article - Managed Extensions - Using GDI+ Brushes to Draw Text - I presented step-by-step instructions as well as a demo application illustrating how to draw 2D text using GDI+ objects. This week, I'll take that a step a further and show you how to render 3D text in order to accomlish the following effects:
Note: In order to test these code snippets, first simply drag a PictureBox from the Toolbox onto a Form in a Managed Extensions Windows Forms application and name the PictureBox variable picText. Then, copy and paste the desired snippet into your application to have the text rendered onto the PictureBox
// Assumes a PictureBox on the form named picText // with this code being the picText object's // Paint method private: System::Void picText_Paint( System::Object * sender, System::Windows::Forms::PaintEventArgs * e) { // Test string String* textToDisplay = S"Test string"; // Obtain Graphics object Graphics* g = e->Graphics; // Create a Font object, Times New Roman, 25pt System::Drawing::Font* font = new System::Drawing::Font("Times New Roman", Convert::ToSingle(25), FontStyle::Regular); // Obtain the size of the text to be rendered SizeF textSize = g->MeasureString(textToDisplay, font); // Text will be centered on PictureBox control Single x = (picText->Width - textSize.Width) / 2; Single y = (picText->Height - textSize.Height) / 2; // Clear background g->Clear(Color::White); // Draw the shadow text g->DrawString(textToDisplay, font, SystemBrushes::ControlLight, x + 5, y + 5); // Draw the foreground text g->DrawString(textToDisplay, font, SystemBrushes::ControlText, x, y); }
// Assumes a PictureBox on the form named picText // with this code being the picText object's // Paint method private: System::Void picText_Paint( System::Object * sender, System::Windows::Forms::PaintEventArgs * e) { // Test string String* textToDisplay = S"Test string"; // Get drawing surface for PictureBox and clear background Graphics* g = e->Graphics; // Create a Font object System::Drawing::Font* font = new System::Drawing::Font("Times New Roman", Convert::ToSingle(25), FontStyle::Regular); // Obtain the size of the text to be rendered SizeF textSize = g->MeasureString(textToDisplay, font); // Text will be centered on Picture Box control Single x = (picText->Width - textSize.Width) / 2; Single y = (picText->Height - textSize.Height) / 2; // Clear background g->Clear(Color::White); // Print the background text multiple times starting // at the furthest point in the background up to // the foreground text for (int i = Convert::ToInt32(5); i >= 0; i--) { g->DrawString(textToDisplay, font, SystemBrushes::ControlLight, x - i, y + i); } // Draw the foreground text g->DrawString(textToDisplay, font, SystemBrushes::ControlText, x, y); }
// Assumes a PictureBox on the form named picText // with this code being the picText object's // Paint method private: System::Void picText_Paint( System::Object * sender, System::Windows::Forms::PaintEventArgs * e) { // Test string String* textToDisplay = S"Test string"; // Get drawing surface for PictureBox and clear background Graphics* g = e->Graphics; // Create a Font object System::Drawing::Font* font = new System::Drawing::Font("Times New Roman", Convert::ToSingle(25), FontStyle::Regular); // Obtain the size of the text to be rendered SizeF textSize = g->MeasureString(textToDisplay, font); // Text will be centered on Picture Box control Single x = (picText->Width - textSize.Width) / 2; Single y = (picText->Height - textSize.Height) / 2; // Clear background g->Clear(Color::White); // Change the isEmossed value to switch // between embossed and engraved bool isEmbossed = false; g->DrawString(textToDisplay, font, SystemBrushes::ControlText, x + Convert::ToSingle( (isEmbossed? 1 : -1)), y + Convert::ToSingle( (isEmbossed ? 1 : -1))); // Draw the foreground text g->DrawString(textToDisplay, font, new SolidBrush(Color::White), x, y); }
I've attached a demo application with this article that will allow you to play around with the various effects you can accomplish with GDI+ and 3D text. The following image is a screen capture of that demo application