本文整理汇总了C#中IGraphics.MeasureString方法的典型用法代码示例。如果您正苦于以下问题:C# IGraphics.MeasureString方法的具体用法?C# IGraphics.MeasureString怎么用?C# IGraphics.MeasureString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IGraphics
的用法示例。
在下文中一共展示了IGraphics.MeasureString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PointInBox
/// <summary>
/// Determines if the specified screen point lies within the bounding box of
/// the text, taking into account alignment and rotation parameters.
/// </summary>
/// <param name="pt">The screen point, in pixel units</param>
/// <param name="g">
/// A graphic device object to be drawn into. This is normally e.Graphics from the
/// PaintEventArgs argument to the Paint() method.
/// </param>
/// <param name="text">A string value containing the text to be
/// displayed. This can be multiple lines, separated by newline ('\n')
/// characters</param>
/// <param name="x">The X location to display the text, in screen
/// coordinates, relative to the horizontal (<see cref="AlignH"/>)
/// alignment parameter <paramref name="alignH"/></param>
/// <param name="y">The Y location to display the text, in screen
/// coordinates, relative to the vertical (<see cref="AlignV"/>
/// alignment parameter <paramref name="alignV"/></param>
/// <param name="alignH">A horizontal alignment parameter specified
/// using the <see cref="AlignH"/> enum type</param>
/// <param name="alignV">A vertical alignment parameter specified
/// using the <see cref="AlignV"/> enum type</param>
/// <param name="scaleFactor">
/// The scaling factor to be used for rendering objects. This is calculated and
/// passed down by the parent <see cref="GraphPane"/> object using the
/// <see cref="PaneBase.CalcScaleFactor"/> method, and is used to proportionally adjust
/// font sizes, etc. according to the actual size of the graph.
/// </param>
/// <param name="layoutArea">The limiting area (<see cref="SizeF"/>) into which the text
/// must fit. The actual rectangle may be smaller than this, but the text will be wrapped
/// to accomodate the area.</param>
/// <returns>true if the point lies within the bounding box, false otherwise</returns>
public bool PointInBox( PointF pt, IGraphics g, string text, float x,
float y, AlignH alignH, AlignV alignV,
float scaleFactor, SizeF layoutArea )
{
// make sure the font size is properly scaled
Remake( scaleFactor, this.Size, ref _scaledSize, ref _font );
// Get the width and height of the text
SizeF sizeF;
if ( layoutArea.IsEmpty )
sizeF = g.MeasureString( text, _font );
else
sizeF = g.MeasureString( text, _font, layoutArea );
// Create a bounding box rectangle for the text
RectangleF rect = new RectangleF( new PointF( -sizeF.Width / 2.0F, 0.0F ), sizeF );
// Build a transform matrix that inverts that drawing transform
// in this manner, the point is brought back to the box, rather
// than vice-versa. This allows the container check to be a simple
// RectangleF.Contains, since the rectangle won't be rotated.
Matrix matrix = GetMatrix( x, y, sizeF, alignH, alignV, _angle );
PointF[] pts = new PointF[1];
pts[0] = pt;
matrix.TransformPoints( pts );
return rect.Contains( pts[0] );
}
示例2: GetWidth
/// <summary>
/// Get the total width of the specified text string
/// </summary>
/// <param name="g">
/// A graphic device object to be drawn into. This is normally e.Graphics from the
/// PaintEventArgs argument to the Paint() method.
/// </param>
/// <param name="text">The text string for which the width is to be calculated
/// </param>
/// <param name="scaleFactor">
/// The scaling factor to be used for rendering objects. This is calculated and
/// passed down by the parent <see cref="GraphPane"/> object using the
/// <see cref="PaneBase.CalcScaleFactor"/> method, and is used to proportionally adjust
/// font sizes, etc. according to the actual size of the graph.
/// </param>
/// <returns>The scaled text width, in pixels</returns>
public float GetWidth( IGraphics g, string text, float scaleFactor )
{
Remake( scaleFactor, this.Size, ref _scaledSize, ref _font );
float width = g.MeasureString( text, _font ).Width;
if ( _isDropShadow )
width += (float)( Math.Cos( _dropShadowAngle ) * _dropShadowOffset * _font.Height );
return width;
}
示例3: MeasureString
/// <summary>
/// Get a <see cref="SizeF"/> struct representing the width and height
/// of the specified text string, based on the scaled font size, and using
/// the specified <see cref="SizeF"/> as an outer limit.
/// </summary>
/// <remarks>
/// This method will allow the text to wrap as necessary to fit the
/// <see paramref="layoutArea"/>.
/// </remarks>
/// <param name="g">
/// A graphic device object to be drawn into. This is normally e.Graphics from the
/// PaintEventArgs argument to the Paint() method.
/// </param>
/// <param name="text">The text string for which the width is to be calculated
/// </param>
/// <param name="scaleFactor">
/// The scaling factor to be used for rendering objects. This is calculated and
/// passed down by the parent <see cref="GraphPane"/> object using the
/// <see cref="PaneBase.CalcScaleFactor"/> method, and is used to proportionally adjust
/// font sizes, etc. according to the actual size of the graph.
/// </param>
/// <param name="layoutArea">The limiting area (<see cref="SizeF"/>) into which the text
/// must fit. The actual rectangle may be smaller than this, but the text will be wrapped
/// to accomodate the area.</param>
/// <returns>The scaled text dimensions, in pixels, in the form of
/// a <see cref="SizeF"/> struct</returns>
public SizeF MeasureString( IGraphics g, string text, float scaleFactor, SizeF layoutArea )
{
Remake( scaleFactor, this.Size, ref _scaledSize, ref _font );
SizeF size = g.MeasureString( text, _font, layoutArea );
if ( _isDropShadow )
{
size.Width += (float)( Math.Cos( _dropShadowAngle ) *
_dropShadowOffset * _font.Height );
size.Height += (float)( Math.Sin( _dropShadowAngle ) *
_dropShadowOffset * _font.Height );
}
return size;
}
示例4: MeasureTextBox
protected NodeMeasurement MeasureTextBox(IGraphics g, float textHeight, string teamName, string score)
{
float minWidth = TextXOffset * 2;
if (!string.IsNullOrEmpty(score))
{
minWidth += g.MeasureString(score, UserboxFont).Width + TextXOffset;
}
int nameLength = teamName.Length;
string shortName = teamName;
if (!string.IsNullOrEmpty(teamName))
{
while(nameLength > 1 && g.MeasureString(shortName, UserboxFont).Width + minWidth > MinBracketWidth)
{
nameLength--;
shortName = teamName.Substring(0, nameLength) + "...";
}
minWidth += g.MeasureString(shortName, UserboxFont).Width;
}
float width = Math.Max(MinBracketWidth, minWidth);
return new NodeMeasurement(
width,
textHeight,
textHeight / 2);
}
示例5: InternalSize
/// <summary>
/// Function to compute the required size for rendering the map decoration object
/// <para>This is just the size of the decoration object, border settings are excluded</para>
/// </summary>
/// <param name="g"></param>
/// <param name="map"></param>
/// <returns>The</returns>
protected override Size InternalSize(IGraphics g, Map map)
{
RequestDisclaimer(map);
var s = g.MeasureString(_disclaimerText, _font);
return new Size((int)Math.Ceiling(s.Width), (int)Math.Ceiling(s.Height));
}
示例6: DrawGroupBox
private void DrawGroupBox(IGraphics g)
{
// Get windows to draw the GroupBox
Rectangle bounds = new Rectangle(ClientRectangle.X + Margin.Left, ClientRectangle.Y + Margin.Top + Padding.Top,
ClientRectangle.Width - Margin.Left - Margin.Right,
ClientRectangle.Height - Margin.Top - Margin.Bottom - Padding.Top);
g.DrawRectangle(BackColor != SystemColors.Control ? SystemPens.Control : new Pen(Color.Black), bounds.X, bounds.Y,
bounds.Width, bounds.Height, 5, RectangleCorners.All);
// Text Formating positioning & Size
int iTextPos = (bounds.X + 8) + mToggleRect.Width + 2;
int iTextSize = (int) g.MeasureString(Text, Font).Width;
iTextSize = iTextSize < 1 ? 1 : iTextSize;
int iEndPos = iTextPos + iTextSize + 1;
// Draw a line to cover the GroupBox border where the text will sit
if (g is CGraphics){
g.DrawLine(SystemPens.Control, iTextPos, bounds.Y, iEndPos, bounds.Y);
}
// Draw the GroupBox text
using (SolidBrush drawBrush = new SolidBrush(Color.FromArgb(0, 70, 213))){
g.DrawString(Text, Font, drawBrush, iTextPos, bounds.Y - (Font.Height*0.5f));
}
}
示例7: RenderTextWithFormat
/// <summary>
/// Renders the text
/// </summary>
/// <param name="g">The graphics object</param>
/// <param name="text">The text to render</param>
/// <param name="x">The x-ordinate of the reference point</param>
/// <param name="y">The y-ordinate of the reference point</param>
/// <param name="format">The string format, mainly vertical an horizontal alignment are of interest.</param>
/// <param name="lastX">The last horizontal position, to ensure that texts are not overlapping</param>
private void RenderTextWithFormat(IGraphics g, string text, int x, int y, StringFormat format, ref int lastX)
{
//Get the size of the string
var size = g.MeasureString(text, _font);
switch (format.Alignment)
{
case StringAlignment.Far:
if (x - size.Width < lastX) return;
break;
case StringAlignment.Center:
if (x - size.Width / 2 < lastX) return;
break;
case StringAlignment.Near:
if (x < lastX) return;
break;
}
//Output the text.
g.DrawString(text, _font, new SolidBrush(OpacityColor(_foreColor)), x, y, format);
//Keep track of latest x position.
switch (format.Alignment)
{
case StringAlignment.Far:
lastX = x + (int) size.Width;
break;
case StringAlignment.Center:
lastX = x + (int) (size.Width/2);
break;
case StringAlignment.Near:
lastX = x;
break;
}
}
示例8: DrawControlPoint
public static void DrawControlPoint(IGraphics g, Coordinates loc, Color color, string label, ContentAlignment align, ControlPointStyle style, bool drawTextBox, WorldTransform wt)
{
// figure out the size the control box needs to be in world coordinates to make it
// show up as appropriate in view coordinates
// invert the scale
float scaled_size = 0;
if (style == ControlPointStyle.LargeBox || style == ControlPointStyle.LargeCircle || style == ControlPointStyle.LargeX) {
scaled_size = cp_large_size / wt.Scale;
}
else {
scaled_size = cp_small_size / wt.Scale;
}
float scaled_offset = 1 / wt.Scale;
// assume that the world transform is currently applied correctly to the graphics
RectangleF rect = new RectangleF(-scaled_size / 2, -scaled_size / 2, scaled_size, scaled_size);
rect.Offset(Utility.ToPointF(loc));
if (style == ControlPointStyle.LargeBox) {
g.FillRectangle(Color.White, rect);
// shrink the rect down a little (nominally 1 pixel)
rect.Inflate(-scaled_offset, -scaled_offset);
g.FillRectangle(color, rect);
}
else if (style == ControlPointStyle.LargeCircle) {
g.FillEllipse(Color.White, rect);
// shrink the rect down a little (nominally 1 pixel)
rect.Inflate(-scaled_offset, -scaled_offset);
g.FillEllipse(color, rect);
}
else if (style == ControlPointStyle.LargeX) {
using (IPen p = g.CreatePen()) {
p.Width = 3/wt.Scale;
p.Color = Color.White;
g.DrawLine(p, new PointF(rect.Left, rect.Top), new PointF(rect.Right, rect.Bottom));
g.DrawLine(p, new PointF(rect.Left, rect.Bottom), new PointF(rect.Right, rect.Top));
p.Width = scaled_offset;
p.Color = color;
g.DrawLine(p, new PointF(rect.Left, rect.Top), new PointF(rect.Right, rect.Bottom));
g.DrawLine(p, new PointF(rect.Left, rect.Bottom), new PointF(rect.Right, rect.Top));
}
}
else if (style == ControlPointStyle.SmallBox) {
g.FillRectangle(color, rect);
}
else if (style == ControlPointStyle.SmallCircle) {
g.FillEllipse(color, rect);
}
else if (style == ControlPointStyle.SmallX) {
using (IPen p = g.CreatePen()) {
p.Width = 3/wt.Scale;
p.Color = color;
g.DrawLine(p, new PointF(rect.Left, rect.Top), new PointF(rect.Right, rect.Bottom));
g.DrawLine(p, new PointF(rect.Left, rect.Bottom), new PointF(rect.Right, rect.Top));
}
}
if (!string.IsNullOrEmpty(label)) {
SizeF strSize = g.MeasureString(label, label_font);
float x = 0, y = 0;
if (align == ContentAlignment.BottomRight || align == ContentAlignment.MiddleRight || align == ContentAlignment.TopRight) {
x = (float)loc.X + cp_label_space / wt.Scale;
}
else if (align == ContentAlignment.BottomCenter || align == ContentAlignment.MiddleCenter || align == ContentAlignment.TopCenter) {
x = (float)loc.X - strSize.Width / (2 * wt.Scale);
}
else if (align == ContentAlignment.BottomLeft || align == ContentAlignment.MiddleLeft || align == ContentAlignment.TopLeft) {
x = (float)loc.X - (strSize.Width + cp_label_space) / wt.Scale;
}
if (align == ContentAlignment.BottomCenter || align == ContentAlignment.BottomLeft || align == ContentAlignment.BottomRight) {
y = (float)loc.Y - cp_label_space / wt.Scale;
}
else if (align == ContentAlignment.MiddleCenter || align == ContentAlignment.MiddleLeft || align == ContentAlignment.MiddleRight) {
y = (float)loc.Y + strSize.Height / (2 * wt.Scale);
}
else if (align == ContentAlignment.TopCenter || align == ContentAlignment.TopLeft || align == ContentAlignment.TopRight) {
y = (float)loc.Y + (strSize.Height + cp_label_space) / wt.Scale;
}
PointF text_loc = new PointF(x, y);
if (drawTextBox) {
RectangleF text_rect = new RectangleF(text_loc.X - 4/wt.Scale, text_loc.Y - 4/wt.Scale, strSize.Width/wt.Scale, strSize.Height/wt.Scale);
g.FillRectangle(Color.FromArgb(127, Color.White), text_rect);
}
g.DrawString(label, label_font, color, text_loc);
}
}
示例9: MeasureStringWidth
/// <summary>
///
/// </summary>
/// <param name="g"></param>
/// <param name="str"></param>
/// <param name="font"></param>
/// <returns></returns>
public static float MeasureStringWidth(IGraphics g, string str, Font font)
{
return g.MeasureString(str, font).Width;
}
示例10: SizeOfStringCeiling
/// <summary>
/// Function to get the <see cref="SizeF"/> of a string when rendered with the given font.
/// </summary>
/// <param name="g"><see cref="IGraphics"/> object</param>
/// <param name="text">the text to render</param>
/// <param name="font">the font to use</param>
/// <returns>the size</returns>
public SizeF SizeOfStringCeiling(IGraphics g, string text, Font font)
{
SizeF f = g.MeasureString(text, font);
return new SizeF((float)Math.Ceiling(f.Width), (float)Math.Ceiling(f.Height));
}
示例11: StringRegionValue
private RectangleF StringRegionValue(IGraphics g, int textpos)
{
var measureString = Text.Substring(textpos, 1);
var numChars = measureString.Length;
var characterRanges = new CharacterRange[numChars + 1];
var stringFormat = new StringFormat
{
Trimming = StringTrimming.None,
FormatFlags =
StringFormatFlags.NoClip | StringFormatFlags.NoWrap |
StringFormatFlags.LineLimit
};
var size = g.MeasureString(Text, _font, LetterSpacePercentage);
var layoutRect = new RectangleF(0f, 0f, size.Width, size.Height);
characterRanges[0] = new CharacterRange(0, 1);
stringFormat.FormatFlags = StringFormatFlags.NoClip;
stringFormat.SetMeasurableCharacterRanges(characterRanges);
stringFormat.Alignment = StringAlignment.Center;
return g.MeasureCharacterRanges(Text.Substring(textpos), _font, layoutRect, stringFormat);
}
示例12: SizeOfStringBase
/// <summary>
/// Function to get the <see cref="SizeF"/> of a string when rendered with the given font.
/// </summary>
/// <param name="g"><see cref="IGraphics"/> object</param>
/// <param name="text">the text to render</param>
/// <param name="font">the font to use</param>
/// <returns>the size</returns>
public SizeF SizeOfStringBase(IGraphics g, string text, Font font)
{
return g.MeasureString(text, font);
}
示例13: WrapString
public static string[] WrapString(IGraphics g, string s, int width, Font2 font)
{
if (width < 20){
return new[]{s};
}
if (g.MeasureString(s, font).Width < width - 7){
return new[]{s};
}
s = StringUtils.ReduceWhitespace(s);
string[] q = s.Split(' ');
List<string> result = new List<string>();
string current = q[0];
for (int i = 1; i < q.Length; i++){
string next = current + " " + q[i];
if (g.MeasureString(next, font).Width > width - 7){
result.Add(current);
current = q[i];
} else{
current += " " + q[i];
}
}
result.Add(current);
return result.ToArray();
}
示例14: GetStringValue
public static string GetStringValue(IGraphics g, string s, int width, Font2 font)
{
if (width < 20){
return "";
}
if (g.MeasureString(s, font).Width < width - 7){
return s;
}
StringBuilder sb = new StringBuilder();
foreach (char t in s){
if (g.MeasureString(sb.ToString(), font).Width < width - 21){
sb.Append(t);
} else{
break;
}
}
return sb + "...";
}
示例15: BoundingBoxTenPower
/// <summary>
/// Get a <see cref="SizeF"/> struct representing the width and height
/// of the bounding box for the specified text string, based on the scaled font size.
/// </summary>
/// <remarks>
/// This special case method will show the specified string as a power of 10,
/// superscripted and downsized according to the
/// <see cref="Default.SuperSize"/> and <see cref="Default.SuperShift"/>.
/// This routine differs from <see cref="MeasureString(Graphics,string,float)"/> in that it takes into
/// account the rotation angle of the font, and gives the dimensions of the
/// bounding box that encloses the text at the specified angle.
/// </remarks>
/// <param name="g">
/// A graphic device object to be drawn into. This is normally e.Graphics from the
/// PaintEventArgs argument to the Paint() method.
/// </param>
/// <param name="text">The text string for which the width is to be calculated
/// </param>
/// <param name="scaleFactor">
/// The scaling factor to be used for rendering objects. This is calculated and
/// passed down by the parent <see cref="GraphPane"/> object using the
/// <see cref="PaneBase.CalcScaleFactor"/> method, and is used to proportionally adjust
/// font sizes, etc. according to the actual size of the graph.
/// </param>
/// <returns>The scaled text dimensions, in pixels, in the form of
/// a <see cref="SizeF"/> struct</returns>
public SizeF BoundingBoxTenPower( IGraphics g, string text, float scaleFactor )
{
//Remake( scaleFactor, this.Size, ref this.scaledSize, ref this.font );
float scaledSuperSize = _scaledSize * Default.SuperSize;
Remake( scaleFactor, this.Size * Default.SuperSize, ref scaledSuperSize,
ref _superScriptFont );
// Get the width and height of the text
SizeF size10 = MeasureString( g, "10", scaleFactor );
SizeF sizeText = g.MeasureString( text, _superScriptFont );
if ( _isDropShadow )
{
sizeText.Width += (float)( Math.Cos( _dropShadowAngle ) *
_dropShadowOffset * _superScriptFont.Height );
sizeText.Height += (float)( Math.Sin( _dropShadowAngle ) *
_dropShadowOffset * _superScriptFont.Height );
}
SizeF totSize = new SizeF( size10.Width + sizeText.Width,
size10.Height + sizeText.Height * Default.SuperShift );
float cs = (float)Math.Abs( Math.Cos( _angle * Math.PI / 180.0 ) );
float sn = (float)Math.Abs( Math.Sin( _angle * Math.PI / 180.0 ) );
SizeF s2 = new SizeF( totSize.Width * cs + totSize.Height * sn,
totSize.Width * sn + totSize.Height * cs );
return s2;
}