技术开发 频道

VC++.NET中使用GDI+创建特效字体

  3、浮雕及雕刻文本

  由于浮雕及雕刻是一种相反的效果,所以本文将这两种效果放在一节中进行解释。浮雕效果常常通过阴影文本技术来实现,深度设置为1个像素,前景文本的颜色设置为背景颜色。阴影的文本的颜色选择黑色或灰色。雕刻效果的步骤相反,阴影文本相对于前面的文本向左上方偏移一个像素。

// 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);
  三、倾斜文本
 
  1、存储倾斜参数
 
  这里存在最终用户定义文本倾斜参数的问题,在这种情况下,定义一个变量存储这个值,然后调用PictureBox 控件的Paint 方法(例子代码中,是通过"显示文本"按钮的单击响应函数btnDisplayText_Click来调用这个方法)。有时你需要使用用户键入的其他值,如字体大小等。在例子程序中,代码如下:
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();  }
  2、实现PictureBox控件的Paint方法
 
  文本将在PictureBox控件上进行绘制,所以实现Paint方法可以确保在需要的时候控件进行重绘。下面代码的中的条件语句确保用户已经输入了需要显示的文本。
private: System::Void picText_Paint(System::Object * sender, System::Windows::Forms::PaintEventArgs * e) {  if (textToDisplay)  {  } }
  下面步骤中的代码全部放入到PictureBox对象的Paint方法中。
 
  3、获取picture控件的Graphics对象
 
  可以通过PictureBox控件的CreateGraphics方法获取Graphics对象。然而,当Graphics对象超出作用域范围时,垃圾收集器将回收它。这时候,这个对象是不稳定的,所以,你需要的Graphics对象要从Paint 方法得到(经由PaintEventArgs::Graphics方法)。
Graphics* g = e->Graphics;
  4、在用户提供的字体大小基础上实例化字体
 
  这个程序使用默认的"Times New Roman"字体,所以下面的代码使用用户规定的字体大小创建Times New Roman"字体。
System::Drawing::Font* font = new System::Drawing::Font("Times New Roman", Convert::ToSingle(fontSize), FontStyle::Regular);
  5、获取所要显示的文本的尺寸
 
  Graphics::MeasureString方法通常来测量字符串的尺寸,它将返回所要按某种字体显示的文本的尺寸。
SizeF textSize = g->MeasureString(textToDisplay, font);
  6、对PictureBox控件进行清除操作
 
  通过PictureBox::Clear方法来初始化PictureBox控件并定义所需要的颜色,下面的代码使用了用户为控件定义的值。
g->Clear(SystemColors::Control);
  7、计算文本在PictureBox控件上显示的位置
 
  下面的代码决定了居中显示文本坐标位置(X,Y)。
Single x = (picText->Width - textSize.Width) / 2; Single y = (picText->Height - textSize.Height) / 2;
  8、对PictureBox控件的平移矩阵进行平移
 
  为了正确衡量文本,你必须重新衡量整个graphics对象,所以,首先必须重新定位最初的Graphics对象到(X,Y)处,你通过Graphics::TranslateTransform方法来完成上述任务。
g->TranslateTransform(x, y);

 

 

0
相关文章