This is what I'm trying, and vertical centering is failing:
void CGDIOverlays::drawText(RGB_COLOR color, const string &text, INT x, INT y, INT w, INT h, HFONT hFont, BOOL keepSize, BOOL staticText, UINT options ){
HDC hdc = m_pGraphics->GetHDC();
Gdiplus::Font graphFont( hdc, hFont );
m_pGraphics->ReleaseHDC( hdc );
if( staticText || keepSize )
SelectViewport2TargetTransform();
setAlignment( options );
m_StringFormat.SetFormatFlags( StringFormatFlagsNoFitBlackBox );
RectF mrect( static_cast<float>( x ), static_cast<float>( y ), static_cast<float>( w ), static_cast<float>( h ) );
GraphicsPath path( FillModeWinding );
FontFamily fontFamily;
graphFont.GetFamily( &fontFamily );
path.AddString( ( BasicTypesTools::toWString(text) ).c_str(), text.length(), &fontFamily, graphFont.GetStyle(), graphFont.GetSize(), mrect, &m_StringFormat );
DrawRectangle( RGB_COLORS::Red, x, y, w, h, false ); //TODO <DBG>
m_pGraphics->DrawPath( &m_ShadowPen, &path );
m_pGraphics->FillPath( &SolidBrush( Color( color.a, color.r, color.g, color.b ) ), &path );
}
Ah, this is how I set the string format:
void CGDIOverlays::setAlignment( UINT options ) {
m_StringFormat.SetAlignment( StringAlignmentNear );
m_StringFormat.SetLineAlignment( StringAlignmentNear );
if( options & DT_CENTER )
m_StringFormat.SetAlignment( StringAlignmentCenter );
if( options & DT_RIGHT )
m_StringFormat.SetAlignment( StringAlignmentFar );
if( options & DT_VCENTER )
m_StringFormat.SetLineAlignment( StringAlignmentCenter );
if( options & DT_BOTTOM )
m_StringFormat.SetLineAlignment( StringAlignmentFar );
}
I forgot to tell you, this is how I set the drawing options:
m_Text.setDrawingOptions( DT_CENTER | DT_VCENTER | DT_SINGLELINE );
EDIT: This center the string correctly around the baseline, but in this particular case, that's not what I want. My string consists of only one character, and I want to center it. So now I'm checking out GDI's GetGlyphOutline function in order to measure the character and adjust its position accordingly.