GDI+學習及代碼總結之------文本與字體

轉自:http://blog.csdn.net/harvic880925/article/details/9097319


體、字體系列基本概念與構造

字體系列

GDI+中將具有相同字樣、包括不同風格的字體稱爲字體系列。字體從其系列中獲得名稱、重量(如粗體、規則、加亮)以及風格。例如Arial字體系列包含了下列4種字體:

Arial Regular(規則)、Arial Bold(黑體)、Arial Italic(斜體)、Arial Bold Italic(粗斜體);

在GDI+中輸出文本之前,需要構造一個FontFamily對象和一個Font對象。FontFamily類的構造函數如下:

[cpp] view plain copy
  1. FontFamily()   
  2. FontFamily(name, fontCollection)   
name:字體系列名。
fontCollection:指向一個FontCollection對象(字體集),表明字體系列所屬的私有字體集,默認爲NULL

示例(定義一個Arial Regular字體對象font)

[cpp] view plain copy
  1. FontFamily fontFamily(L"Arial");  
  2. Font font(&fontFamily,16,FontStyleRegular,UnitPixel);  

看幾個結構類型

1、LOGFONTA和LOGFONTW

兩個都是一樣的,只是一個用於ASCII編碼,一個用於UNICODE編碼


字體大小單位枚舉(Unit)

[cpp] view plain copy
  1. enum Unit{  
  2.   UnitWorld,//世界座標,非物理上的單位  
  3.   UnitDisplay,//使用和輸出設備相同的單位,如輸出設備爲顯示器,其單位爲像素  
  4.   UnitPixel,//像素  
  5.   UnitPoint,//點(磅),1點等於1/72英寸  
  6.   UnitInch,//英寸  
  7.   UnitDocument,//1/300英寸  
  8.   UnitMillimeter//毫米  
  9. };  
字體風格枚舉(FontStyle)
[cpp] view plain copy
  1. enum FontStyle{  
  2.   FontStyleRegular    = 0,//常規  
  3.   FontStyleBold       = 1,//加粗  
  4.   FontStyleItalic     = 2,//斜體  
  5.   FontStyleBoldItalic = 3,//粗斜  
  6.   FontStyleUnderline  = 4,//下劃線  
  7.   FontStyleStrikeout  = 8//強調線,即在字體中部劃線  
  8. };  
注意:在這幾個樣式中,常規、加粗、傾斜、粗斜不能同時使用。下劃線和強調線風格可以同時使用,也就是說,字體風格由兩部分組成,一部分與字體外形有關,另一部分是附加的線條風格(下劃線和強調線),這兩部分可以配合使用組成字體的整體風格。

例如,要描述一個帶下劃線和強調線的粗斜體風格的字體,可以使用下面的組合來完成:

[cpp] view plain copy
  1. FontStyleBoldItalic |FontStyleUnderline |FontStyleStrikeout   
列舉系統目前安裝的字體信息

我們可以通過InstalledFontCollection類的成員函數GetFamilies枚舉安裝在計算機上的所有字體。GetFamilies函數返回的是一個FontFamily對象的數組。所以,在使用GetFamilies前,應該爲FontFamily數組分配足夠的內存空間。GetFamilies函數:

[cpp] view plain copy
  1. Status GetFamilies(  
  2.   INT numSought,  
  3.   FontFamily* gpfamilies,  
  4.   INT* numFound  
  5. const;  
numSought:[in]字體集中的字體系列總數,通過InstalledFontCollection::GetFamilyCount()獲取
gpfamilies:[out]存儲返回的已安裝的字體系列的數組;
numFound:[out]存儲返回的字體系列總數,數值與numSought一樣;

應用:列舉出系統中的所有字體:

[cpp] view plain copy
  1. SolidBrush solidbrush(Color::Black);  
  2. FontFamily fontfamily(L"Arial");  
  3. Font font(&fontfamily,8,FontStyleRegular,UnitPoint);  
  4.   
  5. int count=0;  
  6. int found=0;  
  7.   
  8. WCHAR familyName[100];//這裏爲了簡化程序,分配足夠大的空間  
  9. WCHAR *familyList=NULL;  
  10. FontFamily pFontFamily[500];//同樣,分配足夠大的空間  
  11.   
  12. CRect rect;  
  13. this->GetClientRect(&rect);  
  14. RectF r(0,0,rect.Width(),rect.Height());  
  15.   
  16. InstalledFontCollection installedFontCollection;  
  17. count=installedFontCollection.GetFamilyCount();  
  18. installedFontCollection.GetFamilies(count,pFontFamily,&found);  
  19.   
  20. familyList=new WCHAR[count*sizeof(familyName)];  
  21. wcscpy(familyList,L"");//先清空familyList,wcscpy實現對寬字節的複製操作  
  22. for(int j=0;j<count;j++){  
  23.     pFontFamily[j].GetFamilyName(familyName);  
  24.     wcscat(familyList,familyName);//把familyName添加到familyList的最後  
  25.     wcscat(familyList,L",");//wcscat實現對寬字節字符的添加操作  
  26. }  
  27. graphics.DrawString(familyList,-1,&font,r,NULL,&solidbrush);  
  28.   
  29. delete[] familyList;  

注意:wcscpy\wcscat函數分別實現了對雙字節字符(寬字符)的複製、添加操作,在ASCII碼中對應strcpy、strcat;

字體邊沿平滑處理

大家都知道,當字體過大時,就會出現失真,邊緣會出現鋸齒,GDI+對字體邊緣有一套自己的處理方式,是通過SetTextRenderingHint來實現的;

[cpp] view plain copy
  1. Status SetTextRenderingHint(  
  2.   TextRenderingHint newMode  
  3. );  
其中TextRenderingHint是個枚舉類,枚舉了幾種處理邊緣的方式

[cpp] view plain copy
  1. enum TextRenderingHint{  
  2.   TextRenderingHintSystemDefault = 0,//使用與系統相同的處理方式  
  3.   TextRenderingHintSingleBitPerPixelGridFit,//不消除鋸齒,使用網格匹配  
  4.   TextRenderingHintSingleBitPerPixel,//不消除鋸齒,不使用網格匹配  
  5.   TextRenderingHintAntiAliasGridFit,//消除鋸齒,使用網格匹配  
  6.   TextRenderingHintAntiAlias,//消除鋸齒,不使用網格匹配  
  7.   TextRenderingHintClearTypeGridFit //在液晶顯示器上使用ClearType技術增強字體清晰度  
  8. };  
看示例:

[cpp] view plain copy
  1. FontFamily fontfamily(L"宋體");  
  2. Font font(&fontfamily,60,FontStyleRegular,UnitPixel);  
  3.   
  4. //使用與系統相同的處理方式  
  5. graphics.SetTextRenderingHint(TextRenderingHintSystemDefault);  
  6. graphics.DrawString(L"什麼玩意",-1,&font,PointF(0,0),&SolidBrush(Color::Green));  
  7.   
  8. //不消除鋸齒,使用網格匹配  
  9. graphics.TranslateTransform(0,font.GetHeight(0.0f));  
  10. graphics.SetTextRenderingHint(TextRenderingHintSingleBitPerPixelGridFit );  
  11. graphics.DrawString(L"什麼玩意",-1,&font,PointF(0,0),&SolidBrush(Color::Green));  
  12.   
  13. //不消除鋸齒,不使用網格匹配  
  14. graphics.TranslateTransform(0,font.GetHeight(0.0f));  
  15. graphics.SetTextRenderingHint(TextRenderingHintSingleBitPerPixel );  
  16. graphics.DrawString(L"什麼玩意",-1,&font,PointF(0,0),&SolidBrush(Color::Green));  
  17.   
  18. //消除鋸齒,使用網格匹配  
  19. graphics.TranslateTransform(0,font.GetHeight(0.0f));  
  20. graphics.SetTextRenderingHint(TextRenderingHintAntiAliasGridFit );  
  21. graphics.DrawString(L"什麼玩意",-1,&font,PointF(0,0),&SolidBrush(Color::Green));  
  22.   
  23. //消除鋸齒,不使用網格匹配  
  24. graphics.TranslateTransform(0,font.GetHeight(0.0f));  
  25. graphics.SetTextRenderingHint(TextRenderingHintAntiAlias );  
  26. graphics.DrawString(L"什麼玩意",-1,&font,PointF(0,0),&SolidBrush(Color::Green));  
  27.   
  28. //在液晶顯示器上使用ClearType技術增強字體清晰度  
  29. graphics.TranslateTransform(0,font.GetHeight(0.0f));  
  30. graphics.SetTextRenderingHint(TextRenderingHintClearTypeGridFit );  
  31. graphics.DrawString(L"什麼玩意",-1,&font,PointF(0,0),&SolidBrush(Color::Green));  

從上面可以看出,列出的這六種邊緣處理方式中,最後一種枚舉成員TextRenderingHintClearTypeGridFit的處理效果最好。這種處理方式使用了Microsoft特有的ClearType技術。是專在液晶顯示器上使用的一種增強字體清晰度的技術。但這種技術有時會出現問題,用投影儀投射到白色牆壁上,會出出字體顯示不正常的情況,而且對於老式的CRT顯示器是根本不支持的。操作系統也必須是XP及以上的系統才行。所以一般用第一個參數,與系統處理方式相同就好。讓系統決定我們適合哪種技術。

FontFamily和Font

在GDI+中輸出文本之前,需要構造一個FontFamily對象和一個Font對象。FontFamily類的構造函數如下:

[cpp] view plain copy
  1. FontFamily()   
  2. FontFamily(name, fontCollection)   
name:字體系列名。
fontCollection:指向一個FontCollection對象(字體集),表明字體系列所屬的字體集,默認爲NULL

再看看Font類的構造函數(先看這四個):

[cpp] view plain copy
  1. Font(hdc)   
  2. Font(HDC hdc, LOGFONTA* logfont)   
  3. Font(HDC hdc, LOGFONTW* logfont)   
  4. Font(hdc, hfont)   
這四個都是GDI中的構造函數,這裏就全不講了,只說說最後兩個純正的GDI+中的Font構造函數

[cpp] view plain copy
  1. Font(family, emSize, style, unit) ;//一般用這個  
  2. Font(familyName, emSize, style, unit, fontCollection) ;//當用到私有字體集時,纔會用這個構造  

family、familyName:fontFamily對象
style:字體樣式,是規則、粗體、斜體、粗斜體、下劃線、強調線的枚舉類,我們上面說過了
unit:字體大小
fontCollection:字體所屬的字體集,這個是用來添加私有字體集的,下面我們將講私有字體集。

構造私有字體集(PrivateFontCollection)

我們平時使用的字體集,只有在系統中註冊過才能直接調用,但當我們使用一個別人系統中可能不存在的字體集時,我們可以從我們的字體文件中加載這個字體集,然後給用戶使用,從文件中加載系統中沒有註冊的字體集的過程,就是創建私有字體集的過程。創建私有字體集用PrivateFontCollection類來實現,這裏就不講了,想了解的話,看《精通GDI+編程》P139。

Font的構造函數中的最後一個參數fontCollection就是從文件中加載的私有字體集;

字體尺寸、字體系列尺寸

字體尺寸和字體系統尺寸不是同一個概念,我們先看一下他們的共同部分,字體在幾個距離上的概念:

幾個概念:

基線:在英文中,會有基線的概念,這是個抽象的概念,所以GDI+並不支持對基線的設置,是指在四線格中的第三條線,這條線把字母分爲上部和下部,如圖中所示。

上部:是指從基線到上行字母頂部之間的距離。

下部:是從基線到下行字母底部的距離。

行距:是指上下兩行基線之間的距離,包括基線之上的上部值、基線之下的下部值以及在上部之外的一些額外空間(通常用於形成行間距,對於這部分空間,GDI+也不提供函數直接訪問操作。)

字體系列的尺寸(FontFamily)

字體系列尺寸,與實際顯示的字體大小無關,它只與設計時的尺寸有關,獲取函數有:

[cpp] view plain copy
  1. FontFamily::GetEmHeight(style)//獲取字體高度的設計單位;  
  2. FontFamily::GetCellAscent(style) //獲取字體上部距離的設計單位數;  
  3. FontFamily::GetCellDescent(style)//獲取字體下部距離的設計單位數;  
  4. FontFamily::GetLineSpacing(style)//獲取行距的設計單位數;  
它們返回的並不是一個具體的物理尺寸,這些返回值並不被轉換成具體的長度值。這些函數返回的只是一個抽像的大小,在GDI+中被稱爲設計單位(Desin Units)。對於一個設計單位,它的大小是固定的,它不會隨着字體大小的改變而改變。從名稱上就可以看出,設計單位是字體設計人員在設計字體時所使用的一種相對的座標計量單位。
字體尺寸(Font)

字體尺寸是指在顯示器上實際的尺寸(顯示尺寸),它的操作函數的返回值與字體的大小成正比;獲取函數有:

[cpp] view plain copy
  1. Font::GetHeight(dpi)//獲取字體高度,返回值以像素爲單位;  
  2. Font::GetSize();//獲取字體尺寸,返回值是em,1em=12dpi;1em稱爲1倍卡,對於倍卡的來義,參看《精通GDI+編程》P145  
雖然這裏的API只提供了對字體高度值的獲取,但我們可以通過計算得出上部顯示高度,下部顯示高度、行距;

看獲取上部顯示高度公式,其它都類似;

看代碼示例:

[cpp] view plain copy
  1. SolidBrush solidBrush(Color::Green);  
  2. PointF pointf(0.0f,0.0f);  
  3.   
  4. WCHAR infoString[100]=L"";  
  5. REAL FontHeightPixel;  
  6. UINT ascent;  
  7. REAL ascentPixel;  
  8. UINT desent;  
  9. REAL descentPixel;  
  10. UINT lineSpacing;  
  11. REAL lineSpacingPixel;  
  12.   
  13. FontFamily fontFamily(L"Arial");  
  14. Font font(&fontFamily,16,FontStyleRegular,UnitPixel);  
  15.   
  16. //顯示字體顯示大小  
  17. swprintf(infoString,L"使用font.GetSize()的返回值爲:%.2fem;",font.GetSize());  
  18. graphics.DrawString(infoString,wcslen(infoString),&font,pointf,&solidBrush);  
  19. //獲取字體顯示高度  
  20. pointf.Y+=font.GetHeight(0.0);  
  21. swprintf(infoString,L"使用font.GetHeight()的返回值爲:%.2f個像素",font.GetHeight(0.0));  
  22. graphics.DrawString(infoString,wcslen(infoString),&font,pointf,&solidBrush);  
  23. //獲取字體設計高度  
  24. pointf.Y+=font.GetHeight(0.0);  
  25. swprintf(infoString,L"使用fontFamily.GetEmHeight()返回的字體高度爲:%d個設計單位。",fontFamily.GetEmHeight(FontStyleRegular));  
  26. graphics.DrawString(infoString,-1,&font,pointf,&solidBrush);  
  27. //獲取字體上部顯示高度,----in 像素  
  28. pointf.Y+=2.0f*font.GetHeight(0.0f);  
  29. ascent=fontFamily.GetCellAscent(FontStyleRegular);  
  30. ascentPixel=font.GetSize()*ascent/fontFamily.GetEmHeight(FontStyleRegular);  
  31. swprintf(infoString,L"上部距離爲%d個設計單位,%.2f像素。",ascent,ascentPixel);  
  32. graphics.DrawString(infoString,-1,&font,pointf,&solidBrush);  
  33. //獲取字體下部顯示高度---in 像素  
  34. pointf.Y+=font.GetHeight(0.0f);  
  35. desent=fontFamily.GetCellDescent(FontStyleRegular);  
  36. descentPixel=font.GetSize()*desent/fontFamily.GetEmHeight(FontStyleRegular);  
  37. swprintf(infoString,L"下部距離爲%d個設計單位,%.2f像素。",desent,descentPixel);  
  38. graphics.DrawString(infoString,-1,&font,pointf,&solidBrush);  
  39. //獲取顯示行距,---in 像素  
  40. pointf.Y+=font.GetHeight(0.0f);  
  41. lineSpacing=fontFamily.GetLineSpacing(FontStyleRegular);  
  42. lineSpacingPixel=font.GetSize()*lineSpacing/fontFamily.GetEmHeight(FontStyleRegular);  
  43. swprintf(infoString,L"行距爲%d個設計單位,%.2f像素。",lineSpacing,lineSpacingPixel);  
  44. graphics.DrawString(infoString,-1,&font,pointf,&solidBrush);  

注意:由於在中文中沒有基線的概念,所以本節中提到的文本尺寸不一定適合中文。

寫字

一、測量字符串顯示寬度

在實際繪圖中,有一個問題很值得我們考慮,我們怎麼知道我們指定的RECT區域能否容得下我們要繪製的文字?如何確保我們的文字在所設置的區域內能夠完整的顯示?當然GDI+爲我們提供了測量函數MeasureString,我們看下它的調用格式:

[cpp] view plain copy
  1. MeasureString(string, length, font, layoutRect, boundingBox)   
  2. MeasureString(string, length, font, layoutRect, stringFormat, boundingBox, codepointsFitted, linesFilled)   
  3. MeasureString(string, length, font, layoutRectSize, stringFormat, size, codepointsFitted, linesFilled)   
  4. MeasureString(string, length, font, origin, boundingBox)   
  5. MeasureString(string, length, font, origin, stringFormat, boundingBox)   
參數說明:

string:[in]所在繪製的字符串
length:[in]
字符串裏字符個數,-1表示字符串以空白結尾。
font:[in]
字符串輸出時使用的字體;
origin:[in]
輸出原點
stringFormat: [in]
輸出所用格式;
layoutRect、layoutRectSize:[in]
指定的文本輸出區域
boundingBox、size:[out]
計算後的文本輸出的限制矩形(注意中詳細講解)
linesFilled:[out]
計算出的在指定區域中能夠顯示的字符行數;
codepointsFitted:[out]
計算出的在指定區域中能夠顯示的字符個數;

注意:boundingBox的計算法則:

1、當layoutRect的width 、height都不爲0時;

boundingBox的值是輸出文本所需要的區域與layoutRect的區域的交集,也就是說,在這種情況下,boundingBox的值始終小於等於layoutRect的值;

2、當layoutRect的width爲0時,height!=0;

boundingBox.height=layoutRect.height;

boundingBox.width的值爲在layoutRect.height;這個高度上,要顯示所有文字所需要的寬度;

3、當layoutRect的width!=0,height==0;

boundingBox.width=layoutRect.width;
boundingBox.height的值爲在layoutRect.width;這個寬度上,要顯示所有文字所需要的高度;

示例:

[cpp] view plain copy
  1. WCHAR string[256]={0};  
  2. wcscpy(string,L"123456789abcdefg");  
  3. FontFamily fontfamily(L"Arial");  
  4.   
  5. Font font1(&fontfamily,30,FontStyleRegular,UnitPixel);  
  6. Font font2(&fontfamily,18,FontStyleRegular,UnitPixel);  
  7.   
  8. RectF layoutRect(10,10,130,0);//高度爲0,讓其自動計算高度  
  9. SolidBrush brush(Color::Green);  
  10. RectF boundRect;  
  11. INT codePointsFitted=0;  
  12. INT linesFitted=0;  
  13.   
  14. int strlen=wcslen(string);  
  15. graphics.MeasureString(string,strlen,&font1,layoutRect,NULL,&boundRect,&codePointsFitted,&linesFitted);  
  16. CString tmp;  
  17. CString s(string);  
  18. tmp.Format(L"欲輸出的字符串爲\n\"%s\"\n共%d個字符,\n其中,在規定的輸出矩形中,\n只輸出了%d行,共%d個字符",s,s.GetLength(),linesFitted,codePointsFitted);  
  19.   
  20. graphics.DrawRectangle(&Pen(Color::Red,2),layoutRect);  
  21. graphics.DrawRectangle(&Pen(Color::Red,2),boundRect);  
  22.   
  23. graphics.DrawString(string,-1,&font1,boundRect,NULL,&brush);  
  24. graphics.DrawString(tmp,-1,&font2,PointF(150,10),NULL,&brush);  

二、字符串去尾

字符串去尾,是指,當字符串在指定的RECT中無法全部顯示時,如何去文本進行截取,以適應目標區域大小。我們暫時把這種截取操作稱爲“去尾”,去尾方式,是通過StringFormat類中的SetTrimming函數來設置的,其函數格式如下:

[cpp] view plain copy
  1. Status SetTrimming(  
  2.   StringTrimming trimming  
  3. );  
其中StringTrimming是個枚舉類,枚舉了GDI+中所支持的去尾方式;

[cpp] view plain copy
  1. enum StringTrimming{  
  2.   StringTrimmingNone      = 0,//不使用去尾  
  3.   StringTrimmingCharacter = 1,//以字符爲單位去尾  
  4.   StringTrimmingWord      = 2,//以單詞爲單位去尾  
  5.   StringTrimmingEllipsisCharacter = 3,//以字符爲單位去尾,略去部分使用省略號表示  
  6.   StringTrimmingEllipsisWord      = 4,//以單詞爲單位去尾,略去部分使用省略號表示  
  7.   StringTrimmingEllipsisPath      = 5//略去字符串中間部分,保證字符的首尾都能夠顯示  
  8. };  
看下他們的去尾效果(爲保證效果,把字符串中的six去掉了):


使用方法,代碼示例:

[cpp] view plain copy
  1. SolidBrush solidBrush(Color::Green);  
  2. FontFamily fontFamily(L"宋體");  
  3. Font font(&fontFamily,25,FontStyleRegular,UnitPixel);  
  4.   
  5. Pen pen(Color::YellowGreen,2);  
  6. //爲了看到效果,故意把six去掉了;  
  7. WCHAR text[]=L"one two three four five seven eight nine ten";  
  8.   
  9. StringFormat stringformat;  
  10. stringformat.SetTrimming(StringTrimmingCharacter);  
  11. graphics.DrawRectangle(&pen,10,30,200,60);  
  12. graphics.DrawString(text,-1,&font,RectF(10,30,200,60),&stringformat,&solidBrush);  

三、格式化文本輸出(StringFormat類)

1、StringFormat::SetFormatFlags()

在使用DrawString函數輸出文本時,可以控制文本輸出的格式。這些輸出格式包括文本對齊方式、文本的輸出方向、文本是否自動換行、使用製表符定製文本列及文本是否剪裁等。這些操作都是使用StringFormat類實現的,先看它其中的一個比較重要的設置文本格式的函數:SetFormatFlags

[cpp] view plain copy
  1. Status SetFormatFlags(  
  2.   INT flags  
  3. );  
其中flags是個枚舉類(可以混合使用),枚舉了幾個格式用於設置,這裏只講解幾個比較常用的,其它就不講解了,如果需要可以查查相關資料;

[cpp] view plain copy
  1. enum StringFormatFlags{  
  2.     StringFormatFlagsDirectionRightToLeft = 0x00000001,//從右至左輸出文本  
  3.     StringFormatFlagsDirectionVertical = 0x00000002,//垂直方向輸出文本  
  4.     StringFormatFlagsNoFitBlackBox = 0x00000004,  
  5.     StringFormatFlagsDisplayFormatControl = 0x00000020,  
  6.     StringFormatFlagsNoFontFallback = 0x00000400,  
  7.     StringFormatFlagsMeasureTrailingSpaces = 0x00000800,  
  8.     StringFormatFlagsNoWrap = 0x00001000,//輸出時不使用自動換行功能  
  9.     StringFormatFlagsLineLimit = 0x00002000,  
  10.     StringFormatFlagsNoClip = 0x00004000//不使用裁剪  
  11. };  
看StringFormatFlagsDirectionRightToLeft和StringFormatFlagsDirectionVertical使用示例:

[cpp] view plain copy
  1. CStringW temp(L"人生得意須盡歡,莫使金樽空對月!天生我材必有用,千金散盡還復來。");  
  2. FontFamily fontfamily(L"幼圓");  
  3. Font font(&fontfamily,20,FontStyleBold,UnitPixel);  
  4. SolidBrush brush(Color::Red);  
  5.   
  6. RectF f(10,10,120,font.GetHeight(0.0f)*8);  
  7. StringFormat strformat;  
  8. strformat.SetFormatFlags(StringFormatFlagsDirectionRightToLeft |StringFormatFlagsDirectionVertical );  
  9. strformat.SetAlignment(StringAlignmentCenter);  
  10. strformat.SetLineAlignment(StringAlignmentCenter);  
  11.   
  12. graphics.DrawRectangle(&Pen(Color::Green,2),f);  
  13. graphics.DrawString(temp,-1,&font,f,&strformat,&brush);  

StringFormatFlagsNoClip示例:

[cpp] view plain copy
  1. CStringW temp(L"小白兔,白又白");  
  2. FontFamily fontfamily(L"幼圓");  
  3. Font font(&fontfamily,20,FontStyleBold,UnitPixel);  
  4. SolidBrush brush(Color::Red);  
  5. StringFormat strformat;  
  6. //使用默認,剪裁  
  7. graphics.DrawRectangle(&Pen(Color::Green,2),RectF(10,10,100,30));  
  8. graphics.DrawString(temp,-1,&font,RectF(10,10,100,30),&strformat,&brush);  
  9. //使用StringFormatFlagsNoClip標識,不剪裁  
  10. strformat.SetFormatFlags(StringFormatFlagsNoClip );  
  11. graphics.DrawRectangle(&Pen(Color::Green,2),RectF(120,10,100,30));  
  12. graphics.DrawString(temp,-1,&font,RectF(120,10,100,30),&strformat,&brush);  

2、設置文本對齊方式(StringFormat::SetAlignment(align))

提起“對齊”,大家可能會馬上聯想到“左對齊”、“右對齊”、“居中”之類的常用對齊方法。GDI+中通過stringFormat類下的下列兩個成員函數來設置文本在水平和垂直方向上的對齊方式:

[cpp] view plain copy
  1. StringFormat::SetAlignment(align)//設置水平對齊方式  
  2. StringFormat::SetLineAlignment(align)//設置垂直方向上的對齊方式  
其中align是StringAlignment枚舉類中的成員:

[cpp] view plain copy
  1. enum StringAlignment{  
  2.   StringAlignmentNear   = 0,//靠近,對水平對齊來言是居左對齊,對垂直而言是居上  
  3.   StringAlignmentCenter = 1,//靠中,對水平和垂直而言都是居中  
  4.   StringAlignmentFar    = 2//靠遠,對水平而言是居右,對垂直而言是居下  
  5. };  
簡單示例(水平居左,垂直居中):
[cpp] view plain copy
  1. CStringW temp(L"小白兔");  
  2. FontFamily fontfamily(L"幼圓");  
  3. Font font(&fontfamily,20,FontStyleBold,UnitPixel);  
  4. SolidBrush brush(Color::Red);  
  5. StringFormat strformat;  
  6. strformat.SetAlignment(StringAlignmentNear);//水平居左  
  7. strformat.SetLineAlignment(StringAlignmentCenter );//垂直居中  
  8. graphics.DrawRectangle(&Pen(Color::Green,2),RectF(10,10,200,130));  
  9. graphics.DrawString(temp,-1,&font,RectF(10,10,200,130),&strformat,&brush);  


最終

由於構建DrawString時用的是brush,這就出現了有趣的地方,由於brush分爲純色畫刷、影線畫刷、紋理畫刷、漸變畫刷,所以我們可以用這幾個畫刷來寫字,寫出的字也具有了畫刷所示的效果,這裏就不再一一列舉了,在《精通GDI+編程》中P178-P182演示了這幾種畫刷寫字的示例,有興趣的可以看看。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章