Vc. NET to create a special effect font with GDI +

Source: Internet
Author: User

from Microsoft. NET technology's Managed Extensions for C + + contains a powerful GDI + technology, and this article describes how to use GDI + 's brush to draw text.

Basic techniques for drawing text using brushes

The example program in this article allows the user to define the text to display, the font size, the brush used to display the text (a mesh brush or gradient brush), and the color of the drawing text. Example code download: Gdiplustextwithbrushes.zip

The following are the basic steps in GDI + to draw text using a gradient brush or a grid painter:

1, add an event handler function to the control's drawing (Paint) event.

The processing of drawing text in this handler function so that the control can be redrawn correctly.

2. Get a graphics (Graphics) object.

As we are familiar with the device context, a drawing object is a drawing plane of the net package, for example, when drawing on a PictureBox control, you can call the Picturebox::creategraphics method to get a graphics object. and use this graphic (Graphics) object when drawing on the control. There are many examples on the Internet that do this, but one problem is that the resulting graphics object is not a permanent object and the control will not be redrawn correctly if the user moves from the current program to another application and returns again. So, to be appropriate, the drawing object should use the Graphics object in the PaintEventArgs object passed to the control paint method, as shown in the following code:

private: System::Void picText_Paint(System::Object * sender,
System::Windows::Forms::PaintEventArgs * e)
{
...
Graphics* g = e->Graphics;

3. Instantiate a Font object

In the 13 constructors of a font class, the most basic constructor requires only that you provide the font name and font size. In the following example, you create a 20-point, regular font for the Times New Roman class:

using namespace System::Drawing;
...
Font* font = new Font(S"Times new Roman", 20, FontStyle::Regular);

4. Measure the size of the text to be drawn

In order to draw the text, you need to use the Graphics::measurestring method to measure the text size. You can use the Graphics::measurestring method to accomplish this task. This method needs to provide the text and font object being measured, and returns the SizeF structure object, which contains the dimensions that will be drawn to the text.

SizeF textsize = g->measurestring (S "My Sample Text", font);

5, the instance brushes the object

You can use a variety of brushes to draw text, including mesh brush, linear gradient brush, path gradient brush, solid brush and texture brush, and so on, only in the creation of different instances of the brush when the parameters passed some small differences. The discussion of various brushes is not the content of this article, only two kinds of brushes (mesh brush and linear gradient brush) are used in the example of this article.

// 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);

6. (option) Fill background

To make your application unique, you can fill the background with color before you draw the text, which has two standard methods. The simpler approach is to call the Graphics::clear method and define the colors that will be used, but sometimes more advanced control is required, and the Graphics::fillrectange method is required.

The Graphics::fillrectange method allows the developer to specify the selected brush object and define the exact rectangular coordinate position. With regard to brush objects, you can use an instantiated custom brush or a system brush systembrushes, which defines a number of attribute members, which are solid brushes, which are used to represent different elements of the window, including the active border and title bar, and so on.

// 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);

7. Draw Text

Once all GDI + objects are instantiated, the next thing you need to do is call Graphics::D rawstring method. The following example uses this method, in which you specify the text, brush, and font that you want to display, and where to display the text.

// Center the text on the drawing surface
g->DrawString(txtToDisplay->Text, font, brush,
(picText->Width - textSize.Width) / 2,
(picText->Height - textSize.Height) / 2);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.