3.3 EMF+ String Drawing Example

This section provides an example of EMF+ string drawing as generated by GDI+ functions.

The following GDI+ example generates an EmfPlusDrawString record (section 2.3.4.14) and EmfPlusFont and EmfPlusStringFormatData objects (sections 2.2.1.3 and 2.2.2.44).

 GdiplusStartupInput gdiplusStartupInput;
 GdiplusStartupOutput gdiplusStartupOutput;
 ULONG_PTR gdiplusToken = 0;
  
 int CALLBACK WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
 {
     // InitializeGdiPlus
  
     GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, &gdiplusStartupOutput);
  
     HDC DeviceContext = GetDC( nullptr );
  
     Metafile File( L"DrawString.emf", DeviceContext, EmfTypeEmfPlusOnly, L"DrawString Demo" );
     Graphics RenderTarget( &File );
     FontFamily Family( L"Arial" );
     Font EffectiveFont( &Family, 40.0f, FontStyle::FontStyleUnderline, UnitPixel );
     RectF LayoutRect( 0, 0, 100, 100 );
  
     Gdiplus::StringFormat Format( 0, 0 );
     Format.SetAlignment( Gdiplus::StringAlignment::StringAlignmentFar );
  
     Gdiplus::Rect GradientRect( 0, 0, 100, 100 );
  
     Gdiplus::LinearGradientBrush
         TestBrush( GradientRect,
                    Gdiplus::Color( 0xff, 0x00, 0x00 ),
                    Gdiplus::Color( 0x00, 0x00, 0xff ),
                    0.0f );
  
     const wchar_t HelloWorld[] = L"Hello World 1 2 3 4!";
  
     RenderTarget.DrawString( HelloWorld,
                              ARRAYSIZE(HelloWorld) - 1,
                              &EffectiveFont,
                              LayoutRect,
                              &Format,
                              &TestBrush );
  
     ReleaseDC( nullptr, DeviceContext );
  
     return 1;
 }
  

The EMF+ metafile generated by the preceding GDI+ example renders the following image:

pictc53ff350-832a-9ef7-6a8c-f7a5354b5bcd

Figure 6: EMF+ string drawing example