在GDI +中使用DrawString函数绘制文本时如何描边?

时间:2017-02-20 07:51:30

标签: c++ windows winapi gdi+ gdi

GDI的StrokePath API可以让您模拟"抚摸"使用this method的文本。我将它复制到这里:

void CCanvas::DrawOutlineText( CDC& dc, const CString& Text )
{
  const int RestorePoint = dc.SaveDC();

  // Create new font
  CFont NewFont;
  NewFont.CreatePointFont( 700, TEXT( "Verdana" ), &dc );

  // Use this font
  dc.SelectObject( &NewFont );

  // Brush for pen
  LOGBRUSH lBrushForPen = { 0 };
  lBrushForPen.lbColor = RGB( 200, 150, 100 );
  lBrushForPen.lbHatch = HS_CROSS;
  lBrushForPen.lbStyle = BS_SOLID;

  // New pen for drawing outline text
  CPen OutlinePen;
  OutlinePen.CreatePen( PS_GEOMETRIC | PS_SOLID, 2, &lBrushForPen, 0, 0 );

  // Use this pen
  dc.SelectObject( &OutlinePen );

  dc.SetBkMode( TRANSPARENT );

  dc.BeginPath();
  // This text is not drawn on screen, but instead each action is being
  // recorded and stored internally as a path, since we called BeginPath
  dc.TextOut( 20, 20, Text );
  // Stop path
  dc.EndPath();

  // Now draw outline text
  dc.StrokePath();

  dc.RestoreDC( RestorePoint );
}

就我而言,我使用GDI +中的DrawString函数来绘制文字。

是否有人知道GDI +中是否有替代BeginPathEndPathStrokePath来模拟文字抚摸?

编辑: 根据以下jschroedl的建议,我尝试了以下内容:

CString str = L"Text";

Graphics grpx(dc.GetSafeHdc());

SolidBrush gdiBrush(Color(0xFF, 0xFF, 0, 0));

StringFormat gdiSF;
gdiSF.SetAlignment(StringAlignmentNear);
gdiSF.SetFormatFlags(StringFormatFlagsNoWrap | StringFormatFlagsNoFitBlackBox | 
    StringFormatFlagsNoFontFallback | StringFormatFlagsNoClip);
gdiSF.SetHotkeyPrefix(HotkeyPrefixNone);
gdiSF.SetTrimming(StringTrimmingNone);

grpx.SetTextRenderingHint(TextRenderingHintAntiAlias);
grpx.SetPixelOffsetMode(PixelOffsetModeNone);
grpx.SetInterpolationMode(InterpolationModeHighQualityBicubic);

GraphicsPath dd;
FontFamily gdiFF(L"Segoe UI");
const PointF pntF(0, 0);
dd.AddString(str, str.GetLength(), &gdiFF, FontStyleRegular, 58.0f, pntF, &gdiSF);

Pen penFF(Color(0xFF, 0xFF, 0, 0), 1.0f);
grpx.DrawPath(&penFF, &dd);

产生了一个锯齿状的轮廓(放大的截图):

enter image description here

知道如何使用抗锯齿进行渲染吗?

1 个答案:

答案 0 :(得分:0)

我相信我们的代码会创建一个GraphicsPath对象,调用GraphicsPath::AddString()来获取文本的路径,然后使用Graphics::DrawPath()绘制路径。

更新:

基于块状文本,我进行了实验并认为这看起来更平滑。

grpx.SetTextRenderingHint(TextRenderingHintClearTypeGridFit);
grpx.SetSmoothingMode(SmoothingModeHighQuality);
grpx.SetPixelOffsetMode(PixelOffsetModeHalf);

enter image description here