Graphics.MeasureString 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
使用指定的 Font繪製時,測量指定的字串。
多載
MeasureString(ReadOnlySpan<Char>, Font, Int32, StringFormat)
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
使用指定的 Font 繪製,並以指定的 StringFormat格式化時,測量指定的字串。
public:
System::Drawing::SizeF MeasureString(ReadOnlySpan<char> text, System::Drawing::Font ^ font, int width, System::Drawing::StringFormat ^ format);
public System.Drawing.SizeF MeasureString (ReadOnlySpan<char> text, System.Drawing.Font font, int width, System.Drawing.StringFormat? format);
member this.MeasureString : ReadOnlySpan<char> * System.Drawing.Font * int * System.Drawing.StringFormat -> System.Drawing.SizeF
Public Function MeasureString (text As ReadOnlySpan(Of Char), font As Font, width As Integer, format As StringFormat) As SizeF
參數
- text
- ReadOnlySpan<Char>
要測量的字串。
- width
- Int32
字串的最大寬度。
- format
- StringFormat
StringFormat,代表字串的格式資訊,例如行距。
傳回
這個方法會傳回 SizeF 結構,代表 PageUnit 屬性所指定之字元串的大小,如使用 font
參數和 stringFormat
參數所繪製的 text
參數所指定的字元串。
適用於
MeasureString(String, Font, SizeF, StringFormat, Int32, Int32)
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
使用指定的 Font 繪製,並以指定的 StringFormat格式化時,測量指定的字串。
public:
System::Drawing::SizeF MeasureString(System::String ^ text, System::Drawing::Font ^ font, System::Drawing::SizeF layoutArea, System::Drawing::StringFormat ^ stringFormat, [Runtime::InteropServices::Out] int % charactersFitted, [Runtime::InteropServices::Out] int % linesFilled);
public System.Drawing.SizeF MeasureString (string? text, System.Drawing.Font font, System.Drawing.SizeF layoutArea, System.Drawing.StringFormat? stringFormat, out int charactersFitted, out int linesFilled);
public System.Drawing.SizeF MeasureString (string text, System.Drawing.Font font, System.Drawing.SizeF layoutArea, System.Drawing.StringFormat stringFormat, out int charactersFitted, out int linesFilled);
member this.MeasureString : string * System.Drawing.Font * System.Drawing.SizeF * System.Drawing.StringFormat * int * int -> System.Drawing.SizeF
Public Function MeasureString (text As String, font As Font, layoutArea As SizeF, stringFormat As StringFormat, ByRef charactersFitted As Integer, ByRef linesFilled As Integer) As SizeF
參數
- text
- String
要測量的字串。
- stringFormat
- StringFormat
StringFormat,代表字串的格式資訊,例如行距。
- charactersFitted
- Int32
字串中的字元數。
- linesFilled
- Int32
字串中的文字行數。
傳回
這個方法會傳回 SizeF 結構,此結構代表使用 font
參數和 stringFormat
參數所繪製之 PageUnit 屬性所指定之 text
參數的大小。
例外狀況
font
null
。
範例
下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse
,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:
建立要測量的字串,並將字型物件設定為 Arial (16 點)
設定字串的版面配置大小上限。
建立字串格式物件,並將其格式旗標設定為 DirectionVertical。
建立整數變數
charactersFitted
和linesFilled
和大小對象來測量字元串。測量字串的大小,並決定使用字串、字型物件、配置大小上限和字串格式填滿的字元數和行數。
使用字串的測量大小繪製紅色矩形。
繪製矩形內的字串。
繪製安裝字元數和填滿行數的值。
結果是以垂直字串括住的垂直矩形。
public:
void MeasureStringSizeFFormatInts( PaintEventArgs^ e )
{
// Set up string.
String^ measureString = "Measure String";
System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Arial",16 );
// Set maximum layout size.
SizeF layoutSize = SizeF(100.0F,200.0F);
// Set string format.
StringFormat^ newStringFormat = gcnew StringFormat;
newStringFormat->FormatFlags = StringFormatFlags::DirectionVertical;
// Measure string.
int charactersFitted;
int linesFilled;
SizeF stringSize = e->Graphics->MeasureString( measureString, stringFont, layoutSize, newStringFormat, charactersFitted, linesFilled );
// Draw rectangle representing size of string.
e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), 0.0F, 0.0F, stringSize.Width, stringSize.Height );
// Draw string to screen.
e->Graphics->DrawString( measureString, stringFont, Brushes::Black, PointF(0,0), newStringFormat );
// Draw output parameters to screen.
String^ outString = String::Format( "chars {0}, lines {1}", charactersFitted, linesFilled );
e->Graphics->DrawString( outString, stringFont, Brushes::Black, PointF(100,0) );
}
private void MeasureStringSizeFFormatInts(PaintEventArgs e)
{
// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);
// Set maximum layout size.
SizeF layoutSize = new SizeF(100.0F, 200.0F);
// Set string format.
StringFormat newStringFormat = new StringFormat();
newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical;
// Measure string.
int charactersFitted;
int linesFilled;
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont, layoutSize, newStringFormat, out charactersFitted, out linesFilled);
// Draw rectangle representing size of string.
e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height);
// Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, new PointF(0, 0), newStringFormat);
// Draw output parameters to screen.
string outString = "chars " + charactersFitted + ", lines " + linesFilled;
e.Graphics.DrawString(outString, stringFont, Brushes.Black, new PointF(100, 0));
}
Private Sub MeasureStringSizeFFormatInts(ByVal e As PaintEventArgs)
' Set up string.
Dim measureString As String = "Measure String"
Dim stringFont As New Font("Arial", 16)
' Set maximum layout size.
Dim layoutSize As New SizeF(100.0F, 200.0F)
' Set string format.
Dim newStringFormat As New StringFormat
newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical
' Measure string.
Dim charactersFitted As Integer
Dim linesFilled As Integer
Dim stringSize As New SizeF
stringSize = e.Graphics.MeasureString(measureString, stringFont, _
layoutSize, newStringFormat, charactersFitted, linesFilled)
' Draw rectangle representing size of string.
e.Graphics.DrawRectangle(New Pen(Color.Red, 1), 0.0F, 0.0F, _
stringSize.Width, stringSize.Height)
' Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
New PointF(0, 0), newStringFormat)
' Draw output parameters to screen.
Dim outString As String = "chars " & charactersFitted & _
", lines " & linesFilled
e.Graphics.DrawString(outString, stringFont, Brushes.Black, _
New PointF(100, 0))
End Sub
備註
MeasureString 方法的設計目的是要與個別字串搭配使用,並在字串前後包含少量的額外空間,以允許懸置字元。 此外,DrawString 方法會調整圖像點,以優化顯示品質,而且可能會顯示比 MeasureString所報告窄的字串。 若要取得適用於版面配置中相鄰字串的計量(例如,實作格式化文本時),請使用 MeasureCharacterRanges 方法或採用 StringFormat 並傳遞 GenericTypographic的其中一個 MeasureString 方法。 也請確定 Graphics 的 TextRenderingHintAntiAlias。
另請參閱
- MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)
- MeasureCharacterRanges(String, Font, RectangleF, StringFormat)
- 使用字型和文字
適用於
MeasureString(ReadOnlySpan<Char>, Font, SizeF, StringFormat, Int32, Int32)
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
使用指定的 Font 繪製,並以指定的 StringFormat格式化時,測量指定的字串。
public:
System::Drawing::SizeF MeasureString(ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::SizeF layoutArea, System::Drawing::StringFormat ^ stringFormat, [Runtime::InteropServices::Out] int % charactersFitted, [Runtime::InteropServices::Out] int % linesFilled);
public System.Drawing.SizeF MeasureString (ReadOnlySpan<char> text, System.Drawing.Font font, System.Drawing.SizeF layoutArea, System.Drawing.StringFormat? stringFormat, out int charactersFitted, out int linesFilled);
member this.MeasureString : ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.SizeF * System.Drawing.StringFormat * int * int -> System.Drawing.SizeF
Public Function MeasureString (text As ReadOnlySpan(Of Char), font As Font, layoutArea As SizeF, stringFormat As StringFormat, ByRef charactersFitted As Integer, ByRef linesFilled As Integer) As SizeF
參數
- text
- ReadOnlySpan<Char>
要測量的字串。
- stringFormat
- StringFormat
StringFormat,代表字串的格式資訊,例如行距。
- charactersFitted
- Int32
字串中的字元數。
- linesFilled
- Int32
字串中的文字行數。
傳回
這個方法會傳回 SizeF 結構,此結構代表使用 font
參數和 stringFormat
參數所繪製之 PageUnit 屬性所指定之 text
參數的大小。
適用於
MeasureString(String, Font, Int32, StringFormat)
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
使用指定的 Font 繪製,並以指定的 StringFormat格式化時,測量指定的字串。
public:
System::Drawing::SizeF MeasureString(System::String ^ text, System::Drawing::Font ^ font, int width, System::Drawing::StringFormat ^ format);
public System.Drawing.SizeF MeasureString (string? text, System.Drawing.Font font, int width, System.Drawing.StringFormat? format);
public System.Drawing.SizeF MeasureString (string text, System.Drawing.Font font, int width, System.Drawing.StringFormat format);
member this.MeasureString : string * System.Drawing.Font * int * System.Drawing.StringFormat -> System.Drawing.SizeF
Public Function MeasureString (text As String, font As Font, width As Integer, format As StringFormat) As SizeF
參數
- text
- String
要測量的字串。
- width
- Int32
字串的最大寬度。
- format
- StringFormat
StringFormat,代表字串的格式資訊,例如行距。
傳回
這個方法會傳回 SizeF 結構,代表 PageUnit 屬性所指定之字元串的大小,如使用 font
參數和 stringFormat
參數所繪製的 text
參數所指定的字元串。
例外狀況
font
null
。
範例
下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse
,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:
建立要測量的字串,並將字型物件設定為 Arial (16 點)。
設定字串的最大寬度。
建立字串格式物件,並將其格式旗標設定為 DirectionVertical。
建立大小對象來測量字串。
使用字串、字型物件、最大寬度和字串格式來測量字串的大小。
使用字串的測量大小繪製紅色矩形。
繪製矩形內的字串。
結果是以垂直字串括住的垂直矩形。
public:
void MeasureStringWidthFormat( PaintEventArgs^ e )
{
// Set up string.
String^ measureString = "Measure String";
System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Arial",16 );
// Set maximum width of string.
int stringWidth = 100;
// Set string format.
StringFormat^ newStringFormat = gcnew StringFormat;
newStringFormat->FormatFlags = StringFormatFlags::DirectionVertical;
// Measure string.
SizeF stringSize = e->Graphics->MeasureString( measureString, stringFont, stringWidth, newStringFormat );
// Draw rectangle representing size of string.
e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), 0.0F, 0.0F, stringSize.Width, stringSize.Height );
// Draw string to screen.
e->Graphics->DrawString( measureString, stringFont, Brushes::Black, PointF(0,0), newStringFormat );
}
private void MeasureStringWidthFormat(PaintEventArgs e)
{
// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);
// Set maximum width of string.
int stringWidth = 100;
// Set string format.
StringFormat newStringFormat = new StringFormat();
newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical;
// Measure string.
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont, stringWidth, newStringFormat);
// Draw rectangle representing size of string.
e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height);
// Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, new PointF(0, 0), newStringFormat);
}
Private Sub MeasureStringWidthFormat(ByVal e As PaintEventArgs)
' Set up string.
Dim measureString As String = "Measure String"
Dim stringFont As New Font("Arial", 16)
' Set maximum width of string.
Dim stringWidth As Integer = 100
' Set string format.
Dim newStringFormat As New StringFormat
newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical
' Measure string.
Dim stringSize As New SizeF
stringSize = e.Graphics.MeasureString(measureString, stringFont, _
stringWidth, newStringFormat)
' Draw rectangle representing size of string.
e.Graphics.DrawRectangle(New Pen(Color.Red, 1), 0.0F, 0.0F, _
stringSize.Width, stringSize.Height)
' Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
New PointF(0, 0), newStringFormat)
End Sub
備註
MeasureString 方法的設計目的是要與個別字串搭配使用,並在字串前後包含少量的額外空間,以允許懸置字元。 此外,DrawString 方法會調整圖像點,以優化顯示品質,而且可能會顯示比 MeasureString所報告窄的字串。 若要取得適用於版面設定中相鄰字串的計量(例如,實作格式化文本時),請使用 MeasureCharacterRanges 方法或採用 StringFormat的其中一個 MeasureString 方法,並傳遞 GenericTypographic。 此外,請確定 Graphics 的 TextRenderingHintAntiAlias。
另請參閱
- MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)
- MeasureCharacterRanges(String, Font, RectangleF, StringFormat)
- 使用字型和文字
適用於
MeasureString(String, Font, SizeF, StringFormat)
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
使用指定的 Font 繪製,並以指定的 StringFormat格式化時,測量指定的字串。
public:
System::Drawing::SizeF MeasureString(System::String ^ text, System::Drawing::Font ^ font, System::Drawing::SizeF layoutArea, System::Drawing::StringFormat ^ stringFormat);
public System.Drawing.SizeF MeasureString (string? text, System.Drawing.Font font, System.Drawing.SizeF layoutArea, System.Drawing.StringFormat? stringFormat);
public System.Drawing.SizeF MeasureString (string text, System.Drawing.Font font, System.Drawing.SizeF layoutArea, System.Drawing.StringFormat stringFormat);
member this.MeasureString : string * System.Drawing.Font * System.Drawing.SizeF * System.Drawing.StringFormat -> System.Drawing.SizeF
Public Function MeasureString (text As String, font As Font, layoutArea As SizeF, stringFormat As StringFormat) As SizeF
參數
- text
- String
要測量的字串。
- stringFormat
- StringFormat
StringFormat,代表字串的格式資訊,例如行距。
傳回
這個方法會傳回 SizeF 結構,代表 PageUnit 屬性所指定之字元串的大小,如使用 font
參數和 stringFormat
參數所繪製的 text
參數所指定的字元串。
例外狀況
font
null
。
範例
下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse
,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:
建立要測量的字串,並將字型物件設定為 Arial (16 點)。
設定字串的版面配置大小上限,建立大小對象來測量字串。
建立字串格式物件,並將其格式旗標設定為 DirectionVertical。
使用字串、字型物件、配置大小上限和字串格式來測量字串的大小。
使用字串的測量大小繪製紅色矩形。
繪製矩形內的字串。
結果是以垂直字串括住的垂直矩形。
public:
void MeasureStringSizeFFormat( PaintEventArgs^ e )
{
// Set up string.
String^ measureString = "Measure String";
System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Arial",16 );
// Set maximum layout size.
SizeF layoutSize = SizeF(100.0F,200.0F);
// Set string format.
StringFormat^ newStringFormat = gcnew StringFormat;
newStringFormat->FormatFlags = StringFormatFlags::DirectionVertical;
// Measure string.
SizeF stringSize = e->Graphics->MeasureString( measureString, stringFont, layoutSize, newStringFormat );
// Draw rectangle representing size of string.
e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), 0.0F, 0.0F, stringSize.Width, stringSize.Height );
// Draw string to screen.
e->Graphics->DrawString( measureString, stringFont, Brushes::Black, PointF(0,0), newStringFormat );
}
private void MeasureStringSizeFFormat(PaintEventArgs e)
{
// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);
// Set maximum layout size.
SizeF layoutSize = new SizeF(100.0F, 200.0F);
// Set string format.
StringFormat newStringFormat = new StringFormat();
newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical;
// Measure string.
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont, layoutSize, newStringFormat);
// Draw rectangle representing size of string.
e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height);
// Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, new PointF(0, 0), newStringFormat);
}
Private Sub MeasureStringSizeFFormat(ByVal e As PaintEventArgs)
' Set up string.
Dim measureString As String = "Measure String"
Dim stringFont As New Font("Arial", 16)
' Set maximum layout size.
Dim layoutSize As New SizeF(100.0F, 200.0F)
' Set string format.
Dim newStringFormat As New StringFormat
newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical
' Measure string.
Dim stringSize As New SizeF
stringSize = e.Graphics.MeasureString(measureString, stringFont, _
layoutSize, newStringFormat)
' Draw rectangle representing size of string.
e.Graphics.DrawRectangle(New Pen(Color.Red, 1), 0.0F, 0.0F, _
stringSize.Width, stringSize.Height)
' Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
New PointF(0, 0), newStringFormat)
End Sub
備註
MeasureString 方法的設計目的是要與個別字串搭配使用,並在字串前後包含少量的額外空間,以允許懸置字元。 此外,DrawString 方法會調整圖像點,以優化顯示品質,而且可能會顯示比 MeasureString所報告窄的字串。 若要取得適用於版面設定中相鄰字串的計量(例如,實作格式化文本時),請使用 MeasureCharacterRanges 方法或採用 StringFormat的其中一個 MeasureString 方法,並傳遞 GenericTypographic。 此外,請確定 Graphics 的 TextRenderingHintAntiAlias。
另請參閱
- MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)
- MeasureCharacterRanges(String, Font, RectangleF, StringFormat)
- 使用字型和文字
適用於
MeasureString(String, Font, PointF, StringFormat)
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
使用指定的 Font 繪製,並以指定的 StringFormat格式化時,測量指定的字串。
public:
System::Drawing::SizeF MeasureString(System::String ^ text, System::Drawing::Font ^ font, System::Drawing::PointF origin, System::Drawing::StringFormat ^ stringFormat);
public System.Drawing.SizeF MeasureString (string? text, System.Drawing.Font font, System.Drawing.PointF origin, System.Drawing.StringFormat? stringFormat);
public System.Drawing.SizeF MeasureString (string text, System.Drawing.Font font, System.Drawing.PointF origin, System.Drawing.StringFormat stringFormat);
member this.MeasureString : string * System.Drawing.Font * System.Drawing.PointF * System.Drawing.StringFormat -> System.Drawing.SizeF
Public Function MeasureString (text As String, font As Font, origin As PointF, stringFormat As StringFormat) As SizeF
參數
- text
- String
要測量的字串。
- stringFormat
- StringFormat
StringFormat,代表字串的格式資訊,例如行距。
傳回
這個方法會傳回 SizeF 結構,代表 PageUnit 屬性所指定之字串的大小,如使用 font
參數和 stringFormat
參數繪製的 text
參數所指定的字串。
例外狀況
font
null
。
範例
下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse
,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:
建立要測量的字串,並將字型物件設定為 Arial (16 點)
建立一個點,以找出字串的左上角。
建立字串格式物件,並將其格式旗標設定為 DirectionVertical。
建立大小對象來測量字串。
使用字串、字型物件、尋找點和字串格式來測量字串的大小。
使用尋找點和字串的測量大小繪製紅色矩形。
繪製矩形內的字串。
結果是以垂直字串括住的垂直矩形。
public:
void MeasureStringPointFFormat( PaintEventArgs^ e )
{
// Set up string.
String^ measureString = "Measure String";
System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Arial",16 );
// Set point for upper-left corner of string.
float x = 50.0F;
float y = 50.0F;
PointF ulCorner = PointF(x,y);
// Set string format.
StringFormat^ newStringFormat = gcnew StringFormat;
newStringFormat->FormatFlags = StringFormatFlags::DirectionVertical;
// Measure string.
SizeF stringSize = e->Graphics->MeasureString( measureString, stringFont, ulCorner, newStringFormat );
// Draw rectangle representing size of string.
e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), x, y, stringSize.Width, stringSize.Height );
// Draw string to screen.
e->Graphics->DrawString( measureString, stringFont, Brushes::Black, ulCorner, newStringFormat );
}
private void MeasureStringPointFFormat(PaintEventArgs e)
{
// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);
// Set point for upper-left corner of string.
float x = 50.0F;
float y = 50.0F;
PointF ulCorner = new PointF(x, y);
// Set string format.
StringFormat newStringFormat = new StringFormat();
newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical;
// Measure string.
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont, ulCorner, newStringFormat);
// Draw rectangle representing size of string.
e.Graphics.DrawRectangle(new Pen(Color.Red, 1), x, y, stringSize.Width, stringSize.Height);
// Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, ulCorner, newStringFormat);
}
Private Sub MeasureStringPointFFormat(ByVal e As PaintEventArgs)
' Set up string.
Dim measureString As String = "Measure String"
Dim stringFont As New Font("Arial", 16)
' Set point for upper-left corner of string.
Dim x As Single = 50.0F
Dim y As Single = 50.0F
Dim ulCorner As New PointF(x, y)
' Set string format.
Dim newStringFormat As New StringFormat
newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical
' Measure string.
Dim stringSize As New SizeF
stringSize = e.Graphics.MeasureString(measureString, stringFont, _
ulCorner, newStringFormat)
' Draw rectangle representing size of string.
e.Graphics.DrawRectangle(New Pen(Color.Red, 1), x, y, _
stringSize.Width, stringSize.Height)
' Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
ulCorner, newStringFormat)
End Sub
備註
MeasureString 方法的設計目的是要與個別字串搭配使用,並在字串前後包含少量的額外空間,以允許懸置字元。 此外,DrawString 方法會調整圖像點,以優化顯示品質,而且可能會顯示比 MeasureString所報告窄的字串。 若要取得適用於版面設定中相鄰字串的計量(例如,實作格式化文本時),請使用 MeasureCharacterRanges 方法或採用 StringFormat的其中一個 MeasureString 方法,並傳遞 GenericTypographic。 此外,請確定 Graphics 的 TextRenderingHintAntiAlias。
另請參閱
- MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)
- MeasureCharacterRanges(String, Font, RectangleF, StringFormat)
- 使用字型和文字
適用於
MeasureString(ReadOnlySpan<Char>, Font, SizeF, StringFormat)
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
使用指定的 Font 繪製,並以指定的 StringFormat格式化時,測量指定的字串。
public:
System::Drawing::SizeF MeasureString(ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::SizeF layoutArea, System::Drawing::StringFormat ^ stringFormat);
public System.Drawing.SizeF MeasureString (ReadOnlySpan<char> text, System.Drawing.Font font, System.Drawing.SizeF layoutArea, System.Drawing.StringFormat? stringFormat);
member this.MeasureString : ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.SizeF * System.Drawing.StringFormat -> System.Drawing.SizeF
Public Function MeasureString (text As ReadOnlySpan(Of Char), font As Font, layoutArea As SizeF, stringFormat As StringFormat) As SizeF
參數
- text
- ReadOnlySpan<Char>
要測量的字串。
- stringFormat
- StringFormat
StringFormat,代表字串的格式資訊,例如行距。
傳回
這個方法會傳回 SizeF 結構,代表 PageUnit 屬性所指定之字元串的大小,如使用 font
參數和 stringFormat
參數所繪製的 text
參數所指定的字元串。
適用於
MeasureString(ReadOnlySpan<Char>, Font)
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
使用指定的 Font繪製時,測量指定的字串。
public:
System::Drawing::SizeF MeasureString(ReadOnlySpan<char> text, System::Drawing::Font ^ font);
public System.Drawing.SizeF MeasureString (ReadOnlySpan<char> text, System.Drawing.Font font);
member this.MeasureString : ReadOnlySpan<char> * System.Drawing.Font -> System.Drawing.SizeF
Public Function MeasureString (text As ReadOnlySpan(Of Char), font As Font) As SizeF
參數
- text
- ReadOnlySpan<Char>
要測量的字串。
傳回
這個方法會傳回 SizeF 結構,此結構代表使用 font
參數所繪製之 text
參數所指定之 PageUnit 屬性所指定之單位的大小。
適用於
MeasureString(String, Font, Int32)
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
使用指定的 Font繪製時,測量指定的字串。
public:
System::Drawing::SizeF MeasureString(System::String ^ text, System::Drawing::Font ^ font, int width);
public System.Drawing.SizeF MeasureString (string? text, System.Drawing.Font font, int width);
public System.Drawing.SizeF MeasureString (string text, System.Drawing.Font font, int width);
member this.MeasureString : string * System.Drawing.Font * int -> System.Drawing.SizeF
Public Function MeasureString (text As String, font As Font, width As Integer) As SizeF
參數
- text
- String
要測量的字串。
- width
- Int32
以像素為單位的字串寬度上限。
傳回
這個方法會傳回 SizeF 結構,此結構代表使用 font
參數繪製之 text
參數中所指定之字元串的大小,以 PageUnit 屬性所指定的單位。
例外狀況
font
null
。
範例
下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse
,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:
建立要測量的字串,並將字型物件設定為 Arial (16 點)。
設定字串的最大寬度。
建立 size 物件,並使用它、字型物件和最大字串寬度來測量字串的大小。
使用字串的測量大小繪製紅色矩形。
繪製矩形內的字串。
public:
void MeasureStringWidth( PaintEventArgs^ e )
{
// Set up string.
String^ measureString = "Measure String";
System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Arial",16 );
// Set maximum width of string.
int stringWidth = 200;
// Measure string.
SizeF stringSize = e->Graphics->MeasureString( measureString, stringFont, stringWidth );
// Draw rectangle representing size of string.
e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), 0.0F, 0.0F, stringSize.Width, stringSize.Height );
// Draw string to screen.
e->Graphics->DrawString( measureString, stringFont, Brushes::Black, PointF(0,0) );
}
private void MeasureStringWidth(PaintEventArgs e)
{
// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);
// Set maximum width of string.
int stringWidth = 200;
// Measure string.
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont, stringWidth);
// Draw rectangle representing size of string.
e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height);
// Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, new PointF(0, 0));
}
Private Sub MeasureStringWidth(ByVal e As PaintEventArgs)
' Set up string.
Dim measureString As String = "Measure String"
Dim stringFont As New Font("Arial", 16)
' Set maximum width of string.
Dim stringWidth As Integer = 200
' Measure string.
Dim stringSize As New SizeF
stringSize = e.Graphics.MeasureString(measureString, _
stringFont, stringWidth)
' Draw rectangle representing size of string.
e.Graphics.DrawRectangle(New Pen(Color.Red, 1), 0.0F, 0.0F, _
stringSize.Width, stringSize.Height)
' Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
New PointF(0, 0))
End Sub
備註
width
參數會指定所傳回之 SizeF 結構的寬度元件最大值(Width)。 如果 width
參數小於字串的實際寬度,則傳回的 Width 元件會截斷為值,代表符合指定寬度內的最大字元數。 為了容納整個字串,傳回的 Height 元件會調整為允許使用字元換行顯示字串的值。
MeasureString 方法的設計目的是要與個別字串搭配使用,並在字串前後包含少量的額外空間,以允許懸置字元。 此外,DrawString 方法會調整圖像點,以優化顯示品質,而且可能會顯示比 MeasureString所報告窄的字串。 若要取得適用於版面設定中相鄰字串的計量(例如,實作格式化文本時),請使用 MeasureCharacterRanges 方法或採用 StringFormat的其中一個 MeasureString 方法,並傳遞 GenericTypographic。 此外,請確定 Graphics 的 TextRenderingHintAntiAlias。
另請參閱
- MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)
- MeasureCharacterRanges(String, Font, RectangleF, StringFormat)
- 使用字型和文字
適用於
MeasureString(String, Font, SizeF)
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
使用指定配置區域內的指定 Font 繪製時,測量指定的字串。
public:
System::Drawing::SizeF MeasureString(System::String ^ text, System::Drawing::Font ^ font, System::Drawing::SizeF layoutArea);
public System.Drawing.SizeF MeasureString (string? text, System.Drawing.Font font, System.Drawing.SizeF layoutArea);
public System.Drawing.SizeF MeasureString (string text, System.Drawing.Font font, System.Drawing.SizeF layoutArea);
member this.MeasureString : string * System.Drawing.Font * System.Drawing.SizeF -> System.Drawing.SizeF
Public Function MeasureString (text As String, font As Font, layoutArea As SizeF) As SizeF
參數
- text
- String
要測量的字串。
傳回
這個方法會傳回 SizeF 結構,此結構代表使用 font
參數所繪製之 text
參數所指定之 PageUnit 屬性所指定之單位的大小。
例外狀況
font
null
。
範例
下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse
,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:
建立要測量的字串,並將字型物件設定為 Arial (16 點)。
設定字串的版面配置大小上限。
建立 size 物件,並使用它、字型物件,以及測量字串大小的版面配置大小上限。
使用字串的測量大小繪製紅色矩形。
繪製矩形內的字串。
public:
void MeasureStringSizeF( PaintEventArgs^ e )
{
// Set up string.
String^ measureString = "Measure String";
System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Arial",16 );
// Set maximum layout size.
SizeF layoutSize = SizeF(200.0F,50.0F);
// Measure string.
SizeF stringSize = e->Graphics->MeasureString( measureString, stringFont, layoutSize );
// Draw rectangle representing size of string.
e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), 0.0F, 0.0F, stringSize.Width, stringSize.Height );
// Draw string to screen.
e->Graphics->DrawString( measureString, stringFont, Brushes::Black, PointF(0,0) );
}
private void MeasureStringSizeF(PaintEventArgs e)
{
// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);
// Set maximum layout size.
SizeF layoutSize = new SizeF(200.0F, 50.0F);
// Measure string.
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont, layoutSize);
// Draw rectangle representing size of string.
e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height);
// Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, new PointF(0, 0));
}
Private Sub MeasureStringSizeF(ByVal e As PaintEventArgs)
' Set up string.
Dim measureString As String = "Measure String"
Dim stringFont As New Font("Arial", 16)
' Set maximum layout size.
Dim layoutSize As New SizeF(200.0F, 50.0F)
' Measure string.
Dim stringSize As New SizeF
stringSize = e.Graphics.MeasureString(measureString, stringFont, _
layoutSize)
' Draw rectangle representing size of string.
e.Graphics.DrawRectangle(New Pen(Color.Red, 1), 0.0F, 0.0F, _
stringSize.Width, stringSize.Height)
' Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
New PointF(0, 0))
End Sub
備註
MeasureString 方法的設計目的是要與個別字串搭配使用,並在字串前後包含少量的額外空間,以允許懸置字元。 此外,DrawString 方法會調整圖像點,以優化顯示品質,而且可能會顯示比 MeasureString所報告窄的字串。 若要取得適用於版面設定中相鄰字串的計量(例如,實作格式化文本時),請使用 MeasureCharacterRanges 方法或採用 StringFormat的其中一個 MeasureString 方法,並傳遞 GenericTypographic。 此外,請確定 Graphics 的 TextRenderingHintAntiAlias。
另請參閱
- MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)
- MeasureCharacterRanges(String, Font, RectangleF, StringFormat)
- 使用字型和文字
適用於
MeasureString(ReadOnlySpan<Char>, Font, Int32)
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
使用指定的 Font繪製時,測量指定的字串。
public:
System::Drawing::SizeF MeasureString(ReadOnlySpan<char> text, System::Drawing::Font ^ font, int width);
public System.Drawing.SizeF MeasureString (ReadOnlySpan<char> text, System.Drawing.Font font, int width);
member this.MeasureString : ReadOnlySpan<char> * System.Drawing.Font * int -> System.Drawing.SizeF
Public Function MeasureString (text As ReadOnlySpan(Of Char), font As Font, width As Integer) As SizeF
參數
- text
- ReadOnlySpan<Char>
要測量的字串。
- width
- Int32
以像素為單位的字串寬度上限。
傳回
這個方法會傳回 SizeF 結構,此結構代表使用 font
參數繪製之 text
參數中所指定之字元串的大小,以 PageUnit 屬性所指定的單位。
適用於
MeasureString(ReadOnlySpan<Char>, Font, SizeF)
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
使用指定配置區域內的指定 Font 繪製時,測量指定的字串。
public:
System::Drawing::SizeF MeasureString(ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::SizeF layoutArea);
public System.Drawing.SizeF MeasureString (ReadOnlySpan<char> text, System.Drawing.Font font, System.Drawing.SizeF layoutArea);
member this.MeasureString : ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.SizeF -> System.Drawing.SizeF
Public Function MeasureString (text As ReadOnlySpan(Of Char), font As Font, layoutArea As SizeF) As SizeF
參數
- text
- ReadOnlySpan<Char>
要測量的字串。
傳回
這個方法會傳回 SizeF 結構,此結構代表使用 font
參數所繪製之 text
參數所指定之 PageUnit 屬性所指定之單位的大小。
適用於
MeasureString(String, Font)
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
使用指定的 Font繪製時,測量指定的字串。
public:
System::Drawing::SizeF MeasureString(System::String ^ text, System::Drawing::Font ^ font);
public System.Drawing.SizeF MeasureString (string? text, System.Drawing.Font font);
public System.Drawing.SizeF MeasureString (string text, System.Drawing.Font font);
member this.MeasureString : string * System.Drawing.Font -> System.Drawing.SizeF
Public Function MeasureString (text As String, font As Font) As SizeF
參數
- text
- String
要測量的字串。
傳回
這個方法會傳回 SizeF 結構,此結構代表使用 font
參數所繪製之 text
參數所指定之 PageUnit 屬性所指定之單位的大小。
例外狀況
font
null
。
font
null
。
範例
下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse
,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:
建立要測量的字串。
建立字型物件,並將其設定為 Arial (16 點)。
建立 size 物件,並使用它和字型對象來測量字串的大小。
使用字串的測量大小繪製紅色矩形。
繪製矩形內的字串。
public:
void MeasureStringMin( PaintEventArgs^ e )
{
// Set up string.
String^ measureString = "Measure String";
System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Arial",16 );
// Measure string.
SizeF stringSize = e->Graphics->MeasureString( measureString, stringFont );
// Draw rectangle representing size of string.
e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), 0.0F, 0.0F, stringSize.Width, stringSize.Height );
// Draw string to screen.
e->Graphics->DrawString( measureString, stringFont, Brushes::Black, PointF(0,0) );
}
private void MeasureStringMin(PaintEventArgs e)
{
// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);
// Measure string.
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont);
// Draw rectangle representing size of string.
e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height);
// Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, new PointF(0, 0));
}
Private Sub MeasureStringMin(ByVal e As PaintEventArgs)
' Set up string.
Dim measureString As String = "Measure String"
Dim stringFont As New Font("Arial", 16)
' Measure string.
Dim stringSize As New SizeF
stringSize = e.Graphics.MeasureString(measureString, stringFont)
' Draw rectangle representing size of string.
e.Graphics.DrawRectangle(New Pen(Color.Red, 1), 0.0F, 0.0F, _
stringSize.Width, stringSize.Height)
' Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
New PointF(0, 0))
End Sub
備註
MeasureString 方法的設計目的是要與個別字串搭配使用,並在字串前後包含少量的額外空間,以允許懸置字元。 此外,DrawString 方法會調整圖像點,以優化顯示品質,而且可能會顯示比 MeasureString所報告窄的字串。 若要取得適用於版面設定中相鄰字串的計量(例如,實作格式化文本時),請使用 MeasureCharacterRanges 方法或採用 StringFormat的其中一個 MeasureString 方法,並傳遞 GenericTypographic。 此外,請確定 Graphics 的 TextRenderingHintAntiAlias。
另請參閱
- MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)
- MeasureCharacterRanges(String, Font, RectangleF, StringFormat)
- 使用字型和文字
適用於
MeasureString(ReadOnlySpan<Char>, Font, PointF, StringFormat)
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
使用指定的 Font 繪製,並以指定的 StringFormat格式化時,測量指定的字串。
public:
System::Drawing::SizeF MeasureString(ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::PointF origin, System::Drawing::StringFormat ^ stringFormat);
public System.Drawing.SizeF MeasureString (ReadOnlySpan<char> text, System.Drawing.Font font, System.Drawing.PointF origin, System.Drawing.StringFormat? stringFormat);
member this.MeasureString : ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.PointF * System.Drawing.StringFormat -> System.Drawing.SizeF
Public Function MeasureString (text As ReadOnlySpan(Of Char), font As Font, origin As PointF, stringFormat As StringFormat) As SizeF
參數
- text
- ReadOnlySpan<Char>
要測量的字串。
- stringFormat
- StringFormat
StringFormat,代表字串的格式資訊,例如行距。
傳回
這個方法會傳回 SizeF 結構,代表 PageUnit 屬性所指定之字串的大小,如使用 font
參數和 stringFormat
參數繪製的 text
參數所指定的字串。