Share via


Graphics::MeasureString (constWCHAR*,INT,constFont*,constRectF&,constStringFormat*,RectF*,INT*,INT*) 方法 (gdiplusgraphics.h)

Graphics::MeasureString 方法會測量指定字型、格式和版面配置矩形中的字串範圍。

語法

Status MeasureString(
  const WCHAR        *string,
  INT                length,
  const Font         *font,
  const RectF &      layoutRect,
  const StringFormat *stringFormat,
  RectF              *boundingBox,
  INT                *codepointsFitted,
  INT                *linesFilled
);

參數

string

要測量之寬字元字串的指標。

重要 對於阿拉伯文等雙向語言,字串長度不能超過 2046 個字元。

length

指定 字串 陣列中字元數的整數。 如果字串為 null 終止, 則長度 參數可以設定為 -1。

font

Font 物件的指標,指定要套用至字串之字型的系列名稱、大小和樣式。

layoutRect

系結字串之矩形的參考。

stringFormat

StringFormat 物件的指標,指定版面配置資訊,例如對齊、修剪、製表位等等。

boundingBox

RectF 物件的指標,該物件會接收系結字串的矩形。

codepointsFitted

選擇性。 INT 的指標,接收實際符合版面配置矩形的字元數。 預設值為 NULL 指標。

linesFilled

選擇性。 INT 的指標,接收符合版面配置矩形的行數。 預設值為 NULL 指標。

傳回值

如果方法成功,它會傳回 Ok,這是 Status 列舉的元素。

如果方法失敗,它會傳回 Status 列舉的其中一個其他元素。

備註

範例

下列範例會測量字串的大小,然後繪製代表該大小的矩形。

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, 100.0f, 50.0f);
   StringFormat format;
   format.SetAlignment(StringAlignmentFar);
   RectF boundRect;
   // Measure the string.
   graphics.MeasureString(string, 12, &font, layoutRect, &format, &boundRect);
   // Draw a rectangle that represents the size of the string.
   graphics.DrawRectangle(&Pen(Color(255, 0, 0, 0)), boundRect);
}

規格需求

   
標頭 gdiplusgraphics.h

另請參閱

DrawString 方法

字型

圖形

RectF

StringFormat