本文整理汇总了C#中System.Drawing.Graphics.MeasureCharacterRanges方法的典型用法代码示例。如果您正苦于以下问题:C# Graphics.MeasureCharacterRanges方法的具体用法?C# Graphics.MeasureCharacterRanges怎么用?C# Graphics.MeasureCharacterRanges使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Drawing.Graphics
的用法示例。
在下文中一共展示了Graphics.MeasureCharacterRanges方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UpdateMeasures
/// <summary>
/// Updates the width of the word as it would be
/// when drawn with the specified font in the specified graphics.
/// </summary>
public virtual void UpdateMeasures(Graphics graphics, Font font)
{
string word = _value;
int rangePos = 0;
int rangeLen = word.Length;
if (IsWhitespace)
{
// Enclose the word in printable characters for
// Graphics.MeasureText to work correctly.
// Furthermore tabs are not measured, so replace
// tabs with, say, 4 spaces
word = "W" + word + "W";
word = word.Replace("\t", " ");
rangePos = 1;
rangeLen = word.Length - 2;
}
_sformat.SetMeasurableCharacterRanges(new CharacterRange[]
{
new CharacterRange(rangePos, rangeLen)
});
Region[] r = graphics.MeasureCharacterRanges(
word, font, _srect, _sformat);
RectangleF bounds = r[0].GetBounds(graphics);
r[0].Dispose();
_width = bounds.Width;
}
示例2: GetDisplayLength
public static int GetDisplayLength(Graphics g, string s, Font font, TextDrawingMode textDrawingMode)
{
if (string.IsNullOrEmpty(s))
return 0;
if (textDrawingMode == TextDrawingMode.GRAPHICS)
{
if (s.Contains("\t"))
s = s.Replace("\t", " ");
ranges[0].Length = s.Length;
//CharacterRange[] ranges = new CharacterRange[] { new CharacterRange(0, s.Length) };
sf.SetMeasurableCharacterRanges(ranges);
Region[] regions = g.MeasureCharacterRanges(s, font, layoutRect, sf);
RectangleF rectF = regions[0].GetBounds(g);
//return (int)rectF.Width;//プロポーショナルでなくても数ピクセルずれる
return (int)((int)((rectF.Width - 1) / fontDisplaySize + 0.95f) * fontDisplaySize);
}
else if (textDrawingMode == TextDrawingMode.TEXTRENDERER)
{
Size size = TextRenderer.MeasureText(g, s, font, layoutSize, TextFormatFlags.NoPadding | TextFormatFlags.NoPrefix);
//Size size = TextRenderer.MeasureText(g, s, StaticConfig.Font);
return size.Width;
}
else// if (StaticConfig.TextDrawingMode == TextDrawingMode.WINAPI)
{
Size size = GDI.MeasureText(s, font);
return size.Width;
}
//来るわけがない
//else
// throw new ExeEE("描画モード不明");
}
示例3: MeasureString
public static SizeF MeasureString(Graphics g, string str, Font font, Rectangle rect, StringFormat sf)
{
var sfTemp = sf.Clone() as StringFormat;
var ranges = new CharacterRange[] { new CharacterRange(0, str.Length) };
sfTemp.SetMeasurableCharacterRanges(ranges);
var regions = g.MeasureCharacterRanges(str, font, rect, sfTemp);
if (regions != null && regions.Length > 0) return regions[0].GetBounds(g).Size;
return new SizeF();
}
示例4: GetTextSize
private static SizeF GetTextSize(string str, Graphics g, bool fTitle) {
SizeF empty = SizeF.Empty;
CharacterRange[] ranges = new CharacterRange[] { new CharacterRange(0, str.Length) };
sfMeasure.SetMeasurableCharacterRanges(ranges);
Region[] regionArray = g.MeasureCharacterRanges(str, fTitle ? font : fontSubText, rctMeasure, sfMeasure);
using(regionArray[0]) {
empty = regionArray[0].GetBounds(g).Size;
empty.Width += 6f;
}
return empty;
}
示例5: measureText
/// <summary>
/// Measures the Size of a Text.
/// </summary>
/// <param name="g">Graphics object used to draw the text</param>
/// <param name="text">The text</param>
/// <param name="font">Font used to draw the text</param>
/// <returns></returns>
public static SizeF measureText(Graphics g, string text, Font font)
{
if (text == null) return Size.Empty;
StringFormat format = new StringFormat();
RectangleF rect = new RectangleF(0, 0, 1000, 1000);
CharacterRange[] ranges = { new CharacterRange(0, text.Length) };
Region[] regions = new Region[1];
format.SetMeasurableCharacterRanges(ranges);
regions = g.MeasureCharacterRanges(text, font, rect, format);
rect = regions[0].GetBounds(g);
return new SizeF(rect.Right + 1f, rect.Bottom + 1f);
}
示例6: StringWidth
private static int StringWidth(Graphics graphics, string text, Font font)
{
System.Drawing.StringFormat format = new System.Drawing.StringFormat();
System.Drawing.RectangleF rect = new System.Drawing.RectangleF(0, 0, 1000, 1000);
System.Drawing.CharacterRange[] ranges = { new System.Drawing.CharacterRange(0, text.Length) };
System.Drawing.Region[] regions = new System.Drawing.Region[1];
format.SetMeasurableCharacterRanges(ranges);
regions = graphics.MeasureCharacterRanges(text, font, rect, format);
rect = regions[0].GetBounds(graphics);
return (int)(rect.Right + 1.0f);
}
示例7: MeasureCharWidth
public static int MeasureCharWidth( Graphics graphics, char c, Font font)
{
if (c == ' ') c = '_';
char[] ch = new char[1];
ch[0] = c;
string str = new string( ch );
StringFormat format = new StringFormat( StringFormat.GenericTypographic );
RectangleF rect = new RectangleF( 0, 0, 1000, 1000 );
CharacterRange[] ranges = { new CharacterRange( 0, str.Length ) };
Region[] regions = new Region[1];
format.SetMeasurableCharacterRanges( ranges );
regions = graphics.MeasureCharacterRanges( str, font, rect, format );
rect = regions[0].GetBounds( graphics );
return (int)(rect.Right + 1.0f);
}
示例8: GetTextSize
public static Size GetTextSize(Graphics graphics, string text, Font font, Size size)
{
if (text.Length == 0)
{
return Size.Empty;
}
StringFormat stringFormat = new StringFormat();
stringFormat.FormatFlags = StringFormatFlags.FitBlackBox;
RectangleF layoutRect = new RectangleF(0f, 0f, size.Width, size.Height);
CharacterRange[] ranges = new CharacterRange[] {new CharacterRange(0, text.Length)};
Region[] regionArray = new Region[1];
stringFormat.SetMeasurableCharacterRanges(ranges);
Rectangle rectangle =
Rectangle.Round(graphics.MeasureCharacterRanges(text, font, layoutRect, stringFormat)[0].GetBounds(graphics));
return new Size(rectangle.Width, rectangle.Height);
}
示例9: MeasureDisplayStringWidth
/// <summary>
/// Measures the actual width of the text
/// http://www.codeproject.com/KB/GDI-plus/measurestring.aspx
/// </summary>
public static Int32 MeasureDisplayStringWidth(Graphics graphics, String text, Font font)
{
try
{
StringFormat format = new StringFormat();
RectangleF rect = new RectangleF(0, 0, 1000, 1000);
CharacterRange[] ranges = { new CharacterRange(0, text.Length) };
Region[] regions = new Region[1];
format.SetMeasurableCharacterRanges(ranges);
regions = graphics.MeasureCharacterRanges(text, font, rect, format);
rect = regions[0].GetBounds(graphics);
return (Int32)rect.Right;
}
catch (IndexOutOfRangeException)
{
return 0;
}
}
示例10: MeasureDisplayStringWidth
public static RectangleF MeasureDisplayStringWidth(Graphics graphics, string text, Font font)
{
var zFormat = new StringFormat
{
Alignment = StringAlignment.Near,
LineAlignment = StringAlignment.Near,
};
var rect = new RectangleF(0, 0, 65536, 65536);
CharacterRange[] ranges = { new CharacterRange(0, text.Length) };
var regions = new Region[1];
zFormat.SetMeasurableCharacterRanges(ranges);
regions = graphics.MeasureCharacterRanges(text, font, rect, zFormat);
rect = regions[0].GetBounds(graphics);
return rect;
}
示例11: GetTextSize
public static Size GetTextSize(Graphics graphics, string text, Font font, Size size)
{
if(text.Length == 0) return Size.Empty;
StringFormat format = new StringFormat();
format.FormatFlags = StringFormatFlags.FitBlackBox; //MeasureTrailingSpaces;
RectangleF layoutRect = new System.Drawing.RectangleF(0, 0, size.Width, size.Height);
CharacterRange[] chRange = {new CharacterRange(0, text.Length)};
Region[] regs = new Region[1];
format.SetMeasurableCharacterRanges(chRange);
regs = graphics.MeasureCharacterRanges(text, font, layoutRect, format);
Rectangle rect = Rectangle.Round(regs[0].GetBounds(graphics));
return new Size(rect.Width, rect.Height);
}
示例12: MeasureDisplayStringWidth
//////////////////////////////////////////////////////////////////////////
public static SizeF MeasureDisplayStringWidth(Graphics graphics, string text, Font font)
{
if (text == "") return new SizeF(0, 0);
System.Drawing.StringFormat format = new System.Drawing.StringFormat();
System.Drawing.RectangleF rect = new System.Drawing.RectangleF(0, 0, 1000, 1000);
System.Drawing.CharacterRange[] ranges =
{ new System.Drawing.CharacterRange(0,
text.Length) };
System.Drawing.Region[] regions = new System.Drawing.Region[1];
format.SetMeasurableCharacterRanges(ranges);
regions = graphics.MeasureCharacterRanges(text, font, rect, format);
rect = regions[0].GetBounds(graphics);
return new SizeF(rect.Right + 1.0f, rect.Bottom);
}
示例13: MeasureString
public static Size MeasureString(Graphics graphics, string text, Font font, RectangleF bounds, StringFormat format)
{
var size = Size.Empty;
using (format = new StringFormat(format))
{
if (bounds.IsEmpty)
{
bounds = new RectangleF(0, 0, float.MaxValue, float.MaxValue);
// We need to clear RightToLeft and set StringAlignment.Near, otherwise we get an incorrect measurement.
format.FormatFlags &= ~StringFormatFlags.DirectionRightToLeft;
format.Alignment = StringAlignment.Near;
format.LineAlignment = StringAlignment.Near;
}
try
{
// Measure text
format.SetMeasurableCharacterRanges(new[] { new CharacterRange(0, text.Length) });
var regions = graphics.MeasureCharacterRanges(text, font, bounds, format);
// Need to use Right and Bottom to account for leadingi
var rect = regions[0].GetBounds(graphics);
size = new Size((int)Math.Ceiling(rect.Right), (int)Math.Ceiling(rect.Bottom));
// Remain compatible with Graphics.MeasureString
size.Height += 1;
}
catch (ExternalException ex)
{
// eat the exception when the text is too long.
if (ex.ErrorCode != NativeMethods.E_FAIL)
{
throw;
}
size = Size.Empty;
}
}
return size;
}
示例14: GetCharSize
private Point GetCharSize(Graphics CurGraphics)
{
// DrawString doesn't actually print where you tell it to but instead consistently prints
// with an offset. This is annoying when the other draw commands do not print with an offset
// this method returns a point defining the offset so we can take it off the printstring command.
CharacterRange[] characterRanges = {new CharacterRange(0, 1)};
RectangleF layoutRect = new RectangleF(0, 0, 100, 100);
StringFormat stringFormat = new StringFormat();
stringFormat.SetMeasurableCharacterRanges(characterRanges);
Region[] stringRegions = new Region[1];
stringRegions = CurGraphics.MeasureCharacterRanges(
"A",
this.Font,
layoutRect,
stringFormat);
RectangleF measureRect1 = stringRegions[0].GetBounds(CurGraphics);
return new Point((Int32) (measureRect1.Width + 0.5), (Int32) (measureRect1.Height + 0.5));
}
示例15: getDrawLength
private static int getDrawLength(Graphics g, Font fnt, RectangleF rect, StringFormat sf, int maxLen)
{
CharacterRange[] ranges = new CharacterRange[] { new CharacterRange(0, 1) };
float num = 1f;
float width = 0f;
int num3 = 2;
string text = StrFunc.CreateInstance().MakeCycleStr(maxLen, "X");
while ((num3 <= maxLen) && (width != num))
{
num = width;
ranges[0].Length = num3;
sf.SetMeasurableCharacterRanges(ranges);
width = g.MeasureCharacterRanges(text, fnt, rect, sf)[0].GetBounds(g).Width;
num3++;
}
if (num3 == maxLen)
{
return maxLen;
}
return (num3 - 1);
}