本文整理汇总了C#中System.Drawing.StringFormat.SetMeasurableCharacterRanges方法的典型用法代码示例。如果您正苦于以下问题:C# StringFormat.SetMeasurableCharacterRanges方法的具体用法?C# StringFormat.SetMeasurableCharacterRanges怎么用?C# StringFormat.SetMeasurableCharacterRanges使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Drawing.StringFormat
的用法示例。
在下文中一共展示了StringFormat.SetMeasurableCharacterRanges方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MeasureTextWidth
private int MeasureTextWidth(TreeNode tn)
{
if (tn == null | tn.TreeView == null)
return 0;
TreeView tree = tn.TreeView;
String text = this.GetText(tn);
if (text == null || text == "")
return 0;
Graphics g = Graphics.FromHwnd(tree.Handle);
using (Font font = new Font(tree.Font, tn.FontStyle))
{
using (StringFormat format = new StringFormat(StringFormat.GenericDefault))
{
RectangleF rect = new RectangleF(0, 0, 1000, 1000);
CharacterRange[] ranges = { new CharacterRange(0, text.Length) };
Region[] regions = new Region[1];
format.SetMeasurableCharacterRanges(ranges);
format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
regions = g.MeasureCharacterRanges(text, font, rect, format);
rect = regions[0].GetBounds(g);
return (int)rect.Right + 3 + (int)(text.Length * 0.25);
}
}
}
示例2: SelectDeviceDialog
public SelectDeviceDialog()
{
CultureInfo UICulture = GarminFitnessView.UICulture;
InitializeComponent();
GarminDeviceManager.Instance.TaskCompleted += new GarminDeviceManager.TaskCompletedEventHandler(OnManagerTaskCompleted);
this.Text = GarminFitnessView.GetLocalizedString("SelectDeviceText");
IntroLabel.Text = GarminFitnessView.GetLocalizedString("SelectDeviceIntroText");
RefreshButton.Text = GarminFitnessView.GetLocalizedString("RefreshButtonText");
Cancel_Button.Text = CommonResources.Text.ActionCancel;
OKButton.Text = CommonResources.Text.ActionOk;
Graphics tempGraphics = this.CreateGraphics();
Region[] stringRegion;
StringFormat format = new StringFormat();
format.SetMeasurableCharacterRanges(new CharacterRange[] { new CharacterRange(0, RefreshButton.Text.Length) });
stringRegion = tempGraphics.MeasureCharacterRanges(RefreshButton.Text, RefreshButton.Font, new Rectangle(0, 0, 1000, 1000), format);
RefreshButton.Size = stringRegion[0].GetBounds(tempGraphics).Size.ToSize();
tempGraphics.Dispose();
RefreshButton.Location = new Point((this.Width - RefreshButton.Width) / 2, RefreshButton.Location.Y);
RefreshDeviceComboBox();
}
示例3: Scale
/// <summary>
/// Scales are instantiated only by the ruler.
/// </summary>
public Scale(Ruler parent, Orientation orientation)
{
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.ResizeRedraw, true);
_parent = parent;
_orientation = orientation;
_alignment = Alignment.Near;
_unit = RulerUnit.Millimeter;
_projectionColor = Color.LightSteelBlue;
_enableGuides = true;
_guideColor = SystemColors.ControlText;
_cursorColor = Color.Red;
_aligning = false;
_alignedNodes = new Hashtable();
_speeder = new StringFormat();
_speeder.SetMeasurableCharacterRanges(new CharacterRange[]
{
new CharacterRange(0, 5)
});
}
示例4: DrawString
public static void DrawString(this Image image, string text, Corner corner, Font font, Color color)
{
using (var graphics = Graphics.FromImage(image))
{
var stringFormat = new StringFormat();
stringFormat.SetMeasurableCharacterRanges(new[] { new CharacterRange(0, text.Length) });
Region[] region = graphics.MeasureCharacterRanges(text, font, new Rectangle(0, 0, image.Width, image.Height), stringFormat);
RectangleF rect = region[0].GetBounds(graphics);
rect.Width += (int)Math.Ceiling(rect.Width * 0.05d);
var point = new PointF();
switch (corner)
{
case Corner.UpperLeft:
point.X = 0;
point.Y = 0;
break;
case Corner.UpperRight:
point.X = image.Width - rect.Width;
point.Y = 0;
break;
case Corner.BottomLeft:
point.X = 0;
point.Y = image.Height - rect.Height;
break;
case Corner.BottomRight:
point.X = image.Width - rect.Width;
point.Y = image.Height - rect.Height;
break;
}
graphics.DrawString(text, font, new SolidBrush(color), point);
}
}
示例5: MeasureStringBoundaries
/// <summary>
/// Measures a string's pixel boundaries (without the extra padding MeasureString gives). <br />
/// NOTE: This bench tests just under 50% slower than MeasureString.
/// </summary>
/// <param name="gfx">The source graphics object</param>
/// <param name="text">The text to measure</param>
/// <param name="font">The font to measure</param>
/// <returns></returns>
public static RectangleF MeasureStringBoundaries(this Graphics gfx, string text, Font font)
{
var rect = new RectangleF(0, 0, int.MaxValue, int.MaxValue);
var format = new StringFormat();
format.SetMeasurableCharacterRanges(new[] { new CharacterRange(0, text.Length) });
var regions = gfx.MeasureCharacterRanges(text, font, rect, format);
return regions[0].GetBounds(gfx);
}
示例6: EMFStringFormat
private EMFStringFormat(byte[] RecordData)
{
ObjectType = EmfObjectType.stringformat;
myStringFormat = new System.Drawing.StringFormat();
//put the Data into a stream and use a binary reader to read the data
MemoryStream _ms = new MemoryStream(RecordData);
BinaryReader _br = new BinaryReader(_ms);
_br.ReadUInt32(); //Who cares about version..not me!
myStringFormat.FormatFlags = (StringFormatFlags)_br.ReadUInt32();
_br.ReadBytes(4);//Language...Ignore for now!
myStringFormat.LineAlignment = (StringAlignment)_br.ReadUInt32();
myStringFormat.Alignment = (StringAlignment)_br.ReadUInt32();
UInt32 DigitSubstitutionMethod = _br.ReadUInt32();
UInt32 DigitSubstitutionLanguage = _br.ReadUInt32();
myStringFormat.SetDigitSubstitution((int)DigitSubstitutionLanguage, (StringDigitSubstitute)DigitSubstitutionMethod);
Single FirstTabOffSet = _br.ReadSingle();
myStringFormat.HotkeyPrefix = (System.Drawing.Text.HotkeyPrefix) _br.ReadInt32();
_br.ReadSingle();//leading Margin
_br.ReadSingle();//trailingMargin
_br.ReadSingle();//tracking
myStringFormat.Trimming = (StringTrimming)_br.ReadUInt32();
Int32 TabStopCount = _br.ReadInt32();
Int32 RangeCount = _br.ReadInt32();
//Next is stringformatdata...
Single[] TabStopArray;
System.Drawing.CharacterRange[] RangeArray;
if (TabStopCount > 0)
{
TabStopArray = new Single[TabStopCount];
for (int i = 0; i < TabStopCount; i++)
{
TabStopArray[i] = _br.ReadSingle();
}
myStringFormat.SetTabStops(FirstTabOffSet, TabStopArray);
}
if (RangeCount > 0)
{
RangeArray = new System.Drawing.CharacterRange[RangeCount];
for (int i = 0; i < RangeCount; i++)
{
RangeArray[i].First = _br.ReadInt32();
RangeArray[i].Length = _br.ReadInt32();
}
myStringFormat.SetMeasurableCharacterRanges(RangeArray);
}
}
示例7: 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);
}
示例8: 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);
}
示例9: 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);
}
示例10: TextWithHyperlinks
public TextWithHyperlinks(string text, HyperlinkExtractor extractor = null)
{
_text = text;
_sf = (StringFormat)(StringFormat.GenericTypographic.Clone());
_sf.FormatFlags = StringFormatFlags.FitBlackBox | StringFormatFlags.MeasureTrailingSpaces;
if(extractor == null) extractor = new HyperlinkExtractor();
_glyphs = extractor.ExtractHyperlinks(text)
.Select(h => new HyperlinkGlyph(h))
.ToArray();
_sf.SetMeasurableCharacterRanges(
_glyphs.Select(l => new CharacterRange(l.Start, l.Length)).ToArray());
_hoveredLink = new TrackingService<HyperlinkGlyph>();
_hoveredLink.Changed += OnHoveredLinkChanged;
}
示例11: 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);
}
示例12: GetCharacterXPositions
public float[] GetCharacterXPositions( Graphics g, string str )
{
// Setup the StringFormat with proper CharacterRange references.
StringFormat testFormat = new StringFormat();
CharacterRange[] ranges = new CharacterRange[ str.Length ];
for ( int i=0; i < str.Length; i++ )
ranges[i] = new CharacterRange( i, 1 );
testFormat.SetMeasurableCharacterRanges( ranges );
// Measure into Regions
Region[] regions = g.MeasureCharacterRanges( str, _font, new Rectangle( 0, 0, 1000, 1000 ), testFormat );
// Convert Regions to Rects, then X coords.
float[] xCoords = regions.Select( region => region.GetBounds( g ).X ).ToArray();
return xCoords;
}
示例13: 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);
}
示例14: 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;
}
}
示例15: 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;
}