Implementation of the Shadow text function in C ++ GDI +

Source: Internet
Author: User

Implementation of the Shadow text function in C ++ GDI +
Using the excellent graphic output function of GDI +, you can easily implement text effects. One of the Shadow texts is an example.

Many simple text effects simply output the text in different colors and positions once or multiple times, the shadow effect discussed in this article uses the reverse sample capability of GDI + to generate transparent shadows and semi-shadows. The method described hereFirst draw a text smaller than expected on the drawing plane, and then enlarge it.

Because the code comments are very detailed, go directly to the Code:

 

Authorization = NULL; // GDI + initialization void authorization: using () {using namespace Gdiplus; if (NULL = g_gdiPlusToken) {Gdiplus: GdiplusStartupInput gdiplusStartupInput; Gdiplus :: gdiplusStartup (& g_gdiPlusToken, & gdiplusStartupInput, NULL);} CRect ClientRC; CStringW strTxt ("19:20:30"); reverse (& ClientRC); RectF desRC (ClientRC. left, ClientRC. top, ClientRC. width (), ClientRC. height (); PointF txtPos (0, 0); FontFamily fontFamily (L "Times New Roman"); Gdiplus: Font font (& fontFamily, 100, FontStyleBold, UnitPixel ); graphics g (m_picBox.GetDC ()-> m_hDC); // 1.0 fill in the background color g. fillRectangle (& Gdiplus: SolidBrush (Color: LightSlateGray), desRC); // 2.0 create a small memory bitmap, set its length and width to 1/4 Bitmap bmp (ClientRC. width ()/4, ClientRC. height ()/4, & g); // 2.1 set the drawing mode to the anti-sample mode Graphics * pTempG = G Raphics: FromImage (& bmp); pTempG-> SetTextRenderingHint (TextRenderingHintAntiAlias); // 2.2 create a matrix so that the font is 1/4 of the original one, the Shadow distance also sets about 1/4 of the text Matrix mx (0.25f, 0, 0, 0.25f, 3, 3); pTempG-> SetTransform (& mx ); // 2.3 draw text on the in-place graph and use a transparent paint brush (such as 50% transparent) pTempG-> DrawString (strTxt,-1, & font, txtPos, NULL, & SolidBrush (Color (128, 0, 0, 0); // The 3.1 interpolation mode is a high quality double-cubic Interpolation method, which is very important, because the sides of the text are blurred by double interpolation, the shadow and shadow effect g appears. setInterpolationMode (Interpol AtionModeHighQualityBicubic); // 3.2 set the draw mode to the reverse sample mode to ensure the correct range g. setTextRenderingHint (TextRenderingHintAntiAlias); // 3.3 display the bitmap on the screen, which is 4 times larger in both directions. drawImage (& bmp, desRC, 0, 0, bmp. getWidth (), bmp. getHeight (), UnitPixel); // 3.4 draw the text on the drawing plane and use the white font g. drawString (strTxt,-1, & font, txtPos, NULL, & SolidBrush (Color: White); // 4.0 release the memory if (NULL! = PTempG) {delete pTempG; pTempG = NULL ;}}


 

 

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.