2017-02-20 593 views
1

有一個GDI的StrokePath API可以允許使用this method模擬「撫摸」文本。我使用的是DrawString功能從GDI +繪製文本如何在使用GDI +中的DrawString函數繪製文本時描邊文本?

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

在我的情況:我會在這裏複製。

有沒有人知道是否有替代BeginPath,EndPathStrokePath在GDI +模擬文本撫摸?

編輯:繼以下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

任何想法如何使它與抗鋸齒渲染?

回答

0

我相信我們的代碼會創建一個GraphicsPath對象,請致電GraphicsPath::AddString()獲取文本的路徑,然後使用Graphics::DrawPath()繪製路徑。

更新:

基於塊狀文字,我嘗試和認爲這看起來有點順暢。

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

enter image description here

+0

嗯。有趣。謝謝。只是好奇,如果有一種方法來添加消除鋸齒畫筆。它看起來很矮胖。 – c00000fd

+0

看看我們的代碼,我們似乎使用SetInterpolationMode(InterpolationModeHighQualityBicubic)在Graphics對象上設置了它,並且在我們調用graphics-> SetTextRenderingHint(TextRenderingHintAntiAlias)之前,有一條關於尋址'blocky text'的註釋:然後SetPixelOffsetMode(PixelOffsetModeNone) – jschroedl

+0

你知道它沒有改變任何東西。查看我更新的原始帖子w/code。 – c00000fd