private: System::Void picText_Paint(System::Object * sender, System::Windows::Forms::PaintEventArgs * e) { ... Graphics* g = e->Graphics; |
using namespace System::Drawing; ... Font* font = new Font(S"Times new Roman", 20, FontStyle::Regular); |
SizeF textSize = g->MeasureString(S"My Sample Text", font); |
// HatchBrush example Brush* brush = new HatchBrush(HatchStyle::Cross, Color::Black, Color::Blue); // LinearGradientBrush example RectangleF* rect = __nogc new RectangleF(PointF(0, 0), textSize); brush= new LinearGradientBrush(*rect, Color::Black, Color::Blue, LinearGradientMode::ForwardDiagonal); |
// Use the Windows-defined color for controls // and explicitly state the rectangle coordinates g->FillRectangle(SystemBrushes::Control, picText->Left, picText->Top, picText->Right - picText->Left, picText->Bottom - picText->Top); // Color the entire drawing surface using White g->Clear(Color::White); |
// Center the text on the drawing surface g->DrawString(txtToDisplay->Text, font, brush, (picText->Width - textSize.Width) / 2, (picText->Height - textSize.Height) / 2); |
图二、阴影文本效果 |
// 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); // 注意:使用系统"光照"画刷绘制阴影文本 g->DrawString(textToDisplay, font, SystemBrushes::ControlLight, x + 5, y + 5); // 使用系统默认的文本画刷绘制前端文本。 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); // 从背景处开始反复绘制阴影文本 for (int i = Convert::ToInt32(5); i >= 0; i--) { g->DrawString(textToDisplay, font, SystemBrushes::ControlLight, x - i, y + i); } // 绘制前端文本 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); // isEmbossed变量用来决定浮雕或雕刻效果 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); |
图:显示倾斜文本的例子程序界面 |
public __gc class Form1 : public System::Windows::Forms::Form { ... protected: String* textToDisplay; Decimal fontSize; Decimal shearSize; ... private: System::Void btnDisplayText_Click(System::Object * sender, System::EventArgs * e) { // Set up internal display values textToDisplay = txtToDisplay->Text; fontSize = spnFontSize->Value; shearSize = spnShear->Value; // Invalidate the control picText->Invalidate(); } |
private: System::Void picText_Paint(System::Object * sender, System::Windows::Forms::PaintEventArgs * e) { if (textToDisplay) { } } |
Graphics* g = e->Graphics; |
System::Drawing::Font* font = new System::Drawing::Font("Times New Roman", Convert::ToSingle(fontSize), FontStyle::Regular); |
SizeF textSize = g->MeasureString(textToDisplay, font); |
g->Clear(SystemColors::Control); |
Single x = (picText->Width - textSize.Width) / 2; Single y = (picText->Height - textSize.Height) / 2; |
g->TranslateTransform(x, y); |
Matrix* transform = g->Transform; |
transform->Shear(Convert::ToSingle(shearSize), 0); |
g->Transform = transform; |
g->DrawString(textToDisplay, font, Brushes::Black, 0, 0); |
g->TranslateTransform(x, y); |
int lineAscent = font->FontFamily->GetCellAscent(font->Style); int lineSpacing = font->FontFamily->GetLineSpacing(font->Style); Single lineHeight = font->GetHeight(g); Single cy = lineHeight * lineAscent / lineSpacing; |
g->DrawString(textToDisplay, font, Brushes::Black, 0, 0); |
g->ScaleTransform(1, -1.0F); |
g->DrawString(textToDisplay, font, Brushes::Gray, 0, -(cy*2)); |