首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> VC >> 關於VC++

VC++ 特殊字體效果實作大全:漸變、空心、立體、旋轉

     編輯:關於VC++

透過 GDI 路徑與字體屬性設定,可實作多種視覺衝擊力強的特殊字體效果。本文提供四種經典效果的完整實作代碼。

共同前置操作


CClientDC dc(this);
LOGFONT lf;
dc.GetCurrentFont()->GetLogFont(lf);
CFont font, *pOldFont;
lf.lfCharSet = 134;  // GB2312
lf.lfHeight = -150;
lf.lfWidth = 0;
strcpy(lf.lfFaceName, "隸書");
font.CreateFontIndirect(&lf);
pOldFont = dc.SelectObject(&font);
dc.SetBkMode(TRANSPARENT);

1. 漸變字

原理:建立文字路徑 → 設定裁切區域 → 繪製同心橢圓漸變填充


// 空畫筆、空畫刷
CPen pen(PS_NULL, 1, RGB(255,0,0)), *pOldPen = dc.SelectObject(&pen);
CBrush br, *pOldBrush = dc.SelectObject(&br);

dc.BeginPath();
dc.TextOut(10, 10, "漸變字");
dc.EndPath();
dc.SelectClipPath(RGN_COPY);  // 文字路徑作為裁切區

for (int i=255; i>0; i--) {
    int radius = (600*i)/255;
    dc.SelectObject(pOldBrush); br.DeleteObject();
    br.CreateSolidBrush(RGB(255, i, 0));  // 紅→黃漸變
    dc.SelectObject(&br);
    dc.Ellipse(-radius, -radius/3, radius, radius/3);
}

// 恢復 DC 狀態
dc.SelectObject(pOldFont);
dc.SelectObject(pOldPen);
dc.SelectObject(pOldBrush);

2. 空心字

原理:建立文字路徑 → StrokePath 僅繪製輪廓


CPen pen(PS_SOLID, 1, RGB(255,0,0)), *pOldPen = dc.SelectObject(&pen);

dc.BeginPath();
dc.TextOut(10, 130, "空心字");
dc.EndPath();
dc.StrokePath();  // 僅繪製路徑輪廓

dc.SelectObject(pOldFont);
dc.SelectObject(pOldPen);

3. 立體字

原理:陰影色偏移繪製 + 高光色原位繪製,字體建議粗體、非預設字型


LOGFONT lf;
lf.lfHeight = 50; lf.lfWidth = 0; lf.lfEscapement = 0; lf.lfOrientation = 0;
lf.lfWeight = FW_HEAVY; lf.lfItalic = FALSE; lf.lfUnderline = FALSE; lf.lfStrikeOut = FALSE;
lf.lfCharSet = GB2312_CHARSET; strcpy(lf.lfFaceName, "幼圓");
CFont font; font.CreateFontIndirect(&lf);
CFont* pOldFont = dc.SelectObject(&font);

dc.SetBkMode(TRANSPARENT);
dc.SetTextColor(::GetSysColor(COLOR_3DDKSHADOW));  // 陰影色
dc.TextOut(10, 10, "立體字");
dc.SetTextColor(::GetSysColor(COLOR_3DHILIGHT));   // 高光色
dc.TextOut(13, 7, "立體字");  // 向左上偏移 3 像素

dc.SelectObject(pOldFont);

4. 旋轉字

原理:lf.lfEscapement 設定字體基線與 X 軸夾角(單位:0.1 度)


LOGFONT lf;
lf.lfHeight = 50; lf.lfWidth = 0; lf.lfEscapement = 300;  // 30 度
lf.lfOrientation = 0; lf.lfWeight = FW_HEAVY;
lf.lfItalic = FALSE; lf.lfUnderline = FALSE; lf.lfStrikeOut = FALSE;
lf.lfCharSet = GB2312_CHARSET; strcpy(lf.lfFaceName, "幼圓");
CFont font; font.CreateFontIndirect(&lf);
CFont* pOldFont = dc.SelectObject(&font);

dc.SetBkMode(TRANSPARENT);
dc.SetTextColor(RGB(255,0,0));
dc.TextOut(10, 300, "旋轉的字體");

dc.SelectObject(pOldFont);

關鍵技術點總結

效果核心技術關鍵函式/屬性
漸變字路徑裁切 + 同心圓填充BeginPath/EndPath, SelectClipPath, Ellipse
空心字路徑描邊BeginPath/EndPath, StrokePath
立體字雙層偏移繪製TextOut 兩次, 系統色彩常數
旋轉字字體傾斜角度lfEscapement (0.1度單位)
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved