gdi、gdi+文本相關函數


GDI:

TextOut()  最低級的文本輸出函數,速度最快,沒有裁剪,不能帶tab(tab鍵被忽略)

TabbedTextOut()  上面函數的帶tab版本

ExtTextOut()  TextOut升級版,可以調整字符間距和裁剪矩形

DrawText()  TextOut升級版,可以是多行文本,指定一個Rect,會自動換行,可以設置對齊方向

DrawTextEx()  drawText升級版,可以指定tab的顯示方式

GetTextExtentPoint32()  測量某文本的顯示區域

GetTabbedTextExtent()  測量帶tab鍵的字符串的顯示區域

GetTextExtentExPoint()  根據某區域測量能顯示下的字符個數

GetTextMetrics()  獲取字體信息

GetTextFace(hdc,100,cc);  獲取設備中字體名

SetTextColor (hdc, rgbColor) 、GetTextColor   設置文本顏色,默認黑色 
 SetBkMode (hdc, iMode) ;   設置背景模式OPAQUE or TRANSPARENT,默認OPAQUE不透明
 SetBkColor (hdc, rgbColor) ;   設置背景顏色,默認白色  WHITE_BRUSH
 GetStockObject(fnObject)  獲得一些默認的font , pen ,brush,所有的索引名字可以直接使用,如WHITE_BRUSH
 GetSysColor(nIndex)  都到windows的系統顏色
SetTextCharacterExtra(hdc,nCharExtra) 設置TextOut等函數畫的字符的橫排間距
SelectObject(hdc,hgdiobj) 將某個屬性選入設備環境
CreatFontIndirect(lplf) 創建字體,參數是個結構體
CreateFont 創建字體,有12個參數,不常用,上面的常用

 畫路徑字體(例子)

BeginPath (hdc) ;    
 BeginPath (hdc) ;   
 StrokePath (hdc) ;   
 FillPath (hdc) ;   
 FillPath (hdc) ;   
 hRgn = PathToRegion (hdc) ;   
 hRgn = PathToRegion (hdc) ;   

 

 

 

 

GDI+(GDI+的文本函數比GDI慢很多):

DrawString()   畫字符串

MeasureString()  獲得字符串的整體顯示區域信息

函數
備注
實例代碼
截圖
1
Status MeasureString(
  [in]       const WCHAR *string,
  [in]       INT length,
  [in]       const Font *font,
  [in, ref]  const PointF &origin,
  [out]      RectF *boundingBox
) const;
用字體和起始點測量字符串的顯示區域
VOID Example_MeasureString4(HDC hdc)
{
   Graphics graphics(hdc);
   // Set up the string.
   WCHAR string[] = L"Measure Text";
   Font font(L"Arial", 16);
   PointF origin(0.0f, 0.0f);
   RectF boundRect;
   // Measure the string.
   graphics.DrawString(string,12,&font,origin,&SolidBrush(Color(255,0,0,0)));
   graphics.MeasureString(string, 12, &font, origin, &boundRect);
   // Draw a rectangle that represents the size of the string.
   graphics.DrawRectangle(&Pen(Color(255, 0, 0, 0)), boundRect);
}

 
2
Status MeasureString(
  [in]       const WCHAR *string,
  [in]       INT length,
  [in]       const Font *font,
  [in, ref]  const RectF &layoutRect,
  [out]      RectF *boundingBox
) const;
用字體和限制矩形擦亮字符串的顯示區域
1的基礎上加了一個矩形區域比較的判斷返回更小的
VOID Example_MeasureString(HDC hdc)
{
   Graphics graphics(hdc);
   // Set up the string.
   WCHAR string[] = L"Measure Text";
   Font font(L"Arial", 16);
   RectF layoutRect(0, 0, 100, 0);
   RectF boundRect;
   // Measure the string.
   graphics.DrawString(string,12,&font,PointF(0,0),&SolidBrush(Color(100,255,0,0)));
   graphics.DrawString(string,12,&font,RectF(0,0,100,500),&StringFormat(),&SolidBrush(Color(100,0,255,0)));
   graphics.MeasureString(string, 12, &font, layoutRect, &boundRect);
   // Draw a rectangle that represents the size of the string.
   graphics.DrawRectangle(&Pen(Color(255, 255, 0, 0)), boundRect);

   //高度為0可以自動測量高度
   layoutRect = RectF(0, 0, 500, 0);
   graphics.MeasureString(string, 12, &font, layoutRect, &boundRect);
   // Draw a rectangle that represents the size of the string.
   graphics.DrawRectangle(&Pen(Color(255, 0, 255, 0)), boundRect);
}
 
3
Status MeasureString(
  [in]       const WCHAR *string,
  [in]       INT length,
  [in]       const Font *font,
  [in, ref]  const PointF &origin,
  [in]       const StringFormat *stringFormat,
  [out]      RectF *boundingBox
) const;
1的帶StringFormat的版本
VOID Example_MeasureString5(HDC hdc)
{
   Graphics graphics(hdc);
   // Set up the string.
   WCHAR string[] = L"Measure Text";
   Font font(L"Arial", 16);
   PointF origin(0.0f, 0.0f);
   StringFormat format;
   format.SetAlignment(StringAlignmentCenter);
   RectF boundRect;
   // Measure the string.
   graphics.DrawString(string,12,&font,PointF(0,0),&format,&SolidBrush(Color(100,255,0,0)));
   graphics.MeasureString(string, 12, &font, origin, &format, &boundRect);
   // Draw a rectangle that represents the size of the string.
   graphics.DrawRectangle(&Pen(Color(255, 0, 0, 0)), boundRect);
}
 
4
Status MeasureString(
  [in]       const WCHAR *string,
  [in]       INT length,
  [in]       const Font *font,
  [in, ref]  const RectF &layoutRect,
  [in]       const StringFormat *stringFormat,
  [out]      RectF *boundingBox,
  [out]      INT *codepointsFitted,
  [out]      INT *linesFilled
) const;
2的加強版,可以測量指定區域顯示下的字符的個數
VOID Example_MeasureString2(HDC hdc)
{
   Graphics graphics(hdc);
   // Set up the string.
   WCHAR string[] = L"Measure Text";
   Font font(L"Arial", 16);
   RectF layoutRect(0.0f, 0.0f, 70, 50.0f);
   StringFormat format;
   format.SetAlignment(StringAlignmentFar);
   RectF boundRect;
   // Measure the string.
   int codepointsFitted;
   int linesFilled;
   graphics.DrawString(string,12,&font,layoutRect,&format,&SolidBrush(Color(255,0,0,0)));
   graphics.MeasureString(string, 12, &font, layoutRect, &format, &boundRect,&codepointsFitted,&linesFilled);
   // Draw a rectangle that represents the size of the string.
   graphics.DrawRectangle(&Pen(Color(255, 0, 0, 0)), boundRect);
   WCHAR *count = new WCHAR[100];
   swprintf(count,100 , L"codepointsFitted:%d   linesFilled:%d",codepointsFitted,linesFilled);
   graphics.DrawString(L"字符串Measure Text,長度12",-1,&font,PointF(0,70),&SolidBrush(Color(255,0,0,0)));
   graphics.DrawString(count,-1,&font,PointF(0,90),&SolidBrush(Color(255,0,0,0)));
}
 
5
Status MeasureString(
  [in]       const WCHAR *string,
  [in]       INT length,
  [in]       const Font *font,
  [in, ref]  const SizeF &layoutRectSize,
  [in]       const StringFormat *stringFormat,
  [out]      SizeF *size,
  [out]      INT *codepointsFitted,
  [out]      INT *linesFilled
) const;
 4的RectF換成了SizeF,其他一樣,不要位置只要大小,其實位置沒有什么用,所以4很少用到  同上  同上

MeasureCharacterRanges()  獲得字符串的特定字符的顯示位置

Status MeasureCharacterRanges(
  [in]       const WCHAR *string,
  [in]       INT length,
  [in]       const Font *font,
  [in, ref]  const Rectf &layoutRect,
  [in]       const StringFormat *stringFormat,
  [in]       INT regionCount,
  [out]      Region *regions
) const;
測量字符的顯示區域 
VOID MeasureCharRanges(HDC hdc)
{
   Graphics graphics(hdc);

   // Brushes and pens used for drawing and painting
   SolidBrush blueBrush(Color(255, 0, 0, 255));
   SolidBrush redBrush(Color(100, 255, 0, 0));
   Pen        blackPen(Color(255, 0, 0, 0));

   // Layout rectangles used for drawing strings
   RectF   layoutRect_A(20.0f, 20.0f, 130.0f, 130.0f);
   RectF   layoutRect_B(160.0f, 20.0f, 165.0f, 130.0f);
   RectF   layoutRect_C(335.0f, 20.0f, 165.0f, 130.0f);

   // Three different ranges of character positions within the string
   CharacterRange charRanges[3] = { CharacterRange(3, 5),
                                    CharacterRange(15, 2),
                                    CharacterRange(30, 15), };

   // Font and string format to apply to string when drawing
   Font         myFont(L"Times New Roman", 16.0f);
   StringFormat strFormat;

    // Other variables
   Region* pCharRangeRegions; // pointer to CharacterRange regions
   short   i;                 // loop counter
   INT     count;             // number of character ranges set
   WCHAR   string[] = L"The quick, brown fox easily jumps over the lazy dog.";


   // Set three ranges of character positions.
   strFormat.SetMeasurableCharacterRanges(3, charRanges);

   // Get the number of ranges that have been set, and allocate memory to 
   // store the regions that correspond to the ranges.
   count = strFormat.GetMeasurableCharacterRangeCount();
   pCharRangeRegions = new Region[count];

   // Get the regions that correspond to the ranges within the string when
   // layout rectangle A is used. Then draw the string, and show the regions.
   graphics.MeasureCharacterRanges(string, -1,
      &myFont, layoutRect_A, &strFormat, count, pCharRangeRegions);
   graphics.DrawString(string, -1,
      &myFont, layoutRect_A, &strFormat, &blueBrush);
   graphics.DrawRectangle(&blackPen, layoutRect_A);
   for ( i = 0; i < count; i++)
   {
      graphics.FillRegion(&redBrush, pCharRangeRegions + i);
   }

   // Get the regions that correspond to the ranges within the string when
   // layout rectangle B is used. Then draw the string, and show the regions.
   graphics.MeasureCharacterRanges(string, -1,
      &myFont, layoutRect_B, &strFormat, count, pCharRangeRegions);
   graphics.DrawString(string, -1,
      &myFont, layoutRect_B, &strFormat, &blueBrush);
   graphics.DrawRectangle(&blackPen, layoutRect_B);
   for ( i = 0; i < count; i++)
   {
      graphics.FillRegion(&redBrush, pCharRangeRegions + i);
   }

   // Get the regions that correspond to the ranges within the string when
   // layout rectangle C is used. Set trailing spaces to be included in the
   // regions. Then draw the string, and show the regions.
   strFormat.SetFormatFlags(StringFormatFlagsMeasureTrailingSpaces);
   graphics.MeasureCharacterRanges(string, -1,
      &myFont, layoutRect_C, &strFormat, count, pCharRangeRegions);
   graphics.DrawString(string, -1,
      &myFont, layoutRect_C, &strFormat, &blueBrush);
   graphics.DrawRectangle(&blackPen, layoutRect_C);
   for ( i = 0; i < count; i++)
   {
      graphics.FillRegion(&redBrush, pCharRangeRegions + i);
   }
   // Delete memory for the range regions.
   delete [] pCharRangeRegions;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM