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); } } }
public static RectangleF MeasureDisplayStringF(Graphics graphics, string text, Font font, int width, int height) { if (text == string.Empty || text == null) { return(new RectangleF(0, 0, 0, 0)); } System.Drawing.StringFormat format = new System.Drawing.StringFormat(); System.Drawing.RectangleF rect = new System.Drawing.RectangleF(0, 0, width, height); 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); //cleanup format.Dispose(); ranges = null; regions[0].Dispose(); regions = null; return(rect); }
/// <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) }); }
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); } }
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(); }
/// <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); }
static public int MeasureDisplayStringWidth(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 + 20.0f)); }
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); } }
/// <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); }
public Vector2 MeasureString2(string text) { 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 = _fontGraphics.MeasureCharacterRanges(text, _font, rect, format); rect = regions[0].GetBounds(_fontGraphics); return(new Vector2(rect.Right + 1.0f, _charHeight)); }
/// <summary> /// Calculate the width of the string in pixels /// </summary> /// <param name="graphics"></param> /// <param name="value"></param> /// <param name="font"></param> /// <returns></returns> private int GetStringWidth(Graphics graphics, string value, Font font) { StringFormat format = new System.Drawing.StringFormat(); RectangleF rect = new System.Drawing.RectangleF(0, 0, 250, 120); CharacterRange[] ranges = { new System.Drawing.CharacterRange(0, value.Length) }; Region[] regions = new System.Drawing.Region[1]; format.SetMeasurableCharacterRanges(ranges); regions = graphics.MeasureCharacterRanges(value, font, rect, format); rect = regions[0].GetBounds(graphics); return(( int )(rect.Right + 1.0f)); }
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); }
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); }
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; }
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); }
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; }
/// <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 { 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((Int32)rect.Right); } catch (IndexOutOfRangeException) { return(0); } }
/// <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; } }
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; }
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); }
////////////////////////////////////////////////////////////////////////// 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); }
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; }
public SizeF MeasureString(Font font, string text) { /* BAD (but not really!?) * * return this.Control.MeasureString (text, FontHandler.GetControl (font), sd.PointF.Empty, defaultStringFormat).ToEto (); * /**/ if (string.IsNullOrEmpty(text)) { return(Size.Empty); } sd.CharacterRange[] ranges = { new sd.CharacterRange(0, text.Length) }; defaultStringFormat.SetMeasurableCharacterRanges(ranges); var regions = this.Control.MeasureCharacterRanges(text, FontHandler.GetControl(font), sd.Rectangle.Empty, defaultStringFormat); var rect = regions [0].GetBounds(this.Control); return(rect.Size.ToEto()); /**/ }
//adapted from http://www.codeproject.com/KB/GDI-plus/measurestring.aspx public static SizeF MeasureDisplayStringWidth(System.Drawing.Graphics gCanvas, string sText, Font fntFont) { System.Drawing.StringFormat sfFormat = new System.Drawing.StringFormat(); System.Drawing.RectangleF rtRect = new System.Drawing.RectangleF(0, 0, 1000, 1000); System.Drawing.CharacterRange[] craRanges = { new System.Drawing.CharacterRange(0, sText.Length) }; System.Drawing.Region[] raRegions = new System.Drawing.Region[1]; sfFormat.SetMeasurableCharacterRanges(craRanges); raRegions = gCanvas.MeasureCharacterRanges(sText, fntFont, rtRect, sfFormat); rtRect = raRegions[0].GetBounds(gCanvas); SizeF sfFinal = new SizeF(); sfFinal.Width = (rtRect.Right + 1.0f); sfFinal.Height = (rtRect.Bottom); return(sfFinal); }
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)); }
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); }
/// <summary> /// Calculates the exact size of a string. /// Code taken from http://www.codeproject.com/KB/GDI-plus/measurestring.aspx /// </summary> /// <param name="graphics">The graphics object used to calculate the string's size.</param> /// <param name="font">The font which will be used to draw the string.</param> /// <param name="text">The actual string which will be drawn.</param> /// <returns>Returns the untransformed size of the string when being drawn.</returns> static public SizeF MeasureDisplayStringWidth(Graphics graphics, string text, Font font) { // set something to generate the minimum size bool minimum= false; if(text ==string.Empty) { minimum= true; text= " "; } 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 minimum ? new SizeF(0.0f, rect.Height) : rect.Size; }
public static Size CalculateStringSize( IntPtr handle, Font font, string text ) { StringFormat stringFormat = new StringFormat(); RectangleF rect = new RectangleF( 0, 0, 9999, 9999 ); CharacterRange[] ranges = { new CharacterRange( 0, text.Length ) }; Region[] regions = new Region[1]; stringFormat.SetMeasurableCharacterRanges( ranges ); Graphics g = Graphics.FromHwnd( handle ); regions = g.MeasureCharacterRanges( text, font, rect, stringFormat ); rect = regions[0].GetBounds( g ); g.Dispose(); float fudgeFactor = ( font.SizeInPoints / 8.25F ) * 3.0F; return new Size( (int)(rect.Width + fudgeFactor), (int)(rect.Height) ); }
/// <summary> /// Measures the location of words within a string; limited by .Net 1.1 to 32 words /// MEASUREMAX is a constant that defines that limit /// </summary> /// <param name="s"></param> /// <param name="g"></param> /// <param name="drawFont"></param> /// <param name="drawFormat"></param> /// <param name="cra"></param> /// <returns></returns> private WordStartFinish[] MeasureString32(string s, Graphics g, Font drawFont, StringFormat drawFormat, CharacterRange[] cra) { if (s == null || s.Length == 0) return null; drawFormat.SetMeasurableCharacterRanges(cra); Region[] rs = new Region[cra.Length]; rs = g.MeasureCharacterRanges(s, drawFont, new RectangleF(0, 0, float.MaxValue, float.MaxValue), drawFormat); WordStartFinish[] sz = new WordStartFinish[cra.Length]; int isz = 0; foreach (Region r in rs) { RectangleF mr = r.GetBounds(g); sz[isz].start = RSize.PointsFromPixels(g, mr.Left); sz[isz].end = RSize.PointsFromPixels(g, mr.Right); isz++; } return sz; }
private SizeF MeasureString(string s, Graphics g, Font drawFont, StringFormat drawFormat) { if (s == null || s.Length == 0) return SizeF.Empty; CharacterRange[] cr = {new CharacterRange(0, s.Length)}; drawFormat.SetMeasurableCharacterRanges(cr); Region[] rs = new Region[1]; rs = g.MeasureCharacterRanges(s, drawFont, new RectangleF(0,0,float.MaxValue,float.MaxValue), drawFormat); RectangleF mr = rs[0].GetBounds(g); return new SizeF(mr.Width, mr.Height); }
/// <summary> /// Raises the <see cref="System.Windows.Forms.Control.Paint"/> event. /// </summary> /// <param name="e">A <see cref="PaintEventArgs"/> that contains the event data.</param> protected override void OnPaint(PaintEventArgs e) { if (this.inEditMode) { e.Graphics.Clear(this.BackColor); base.OnPaint(e); return; } e.Graphics.Clear(this.Enabled ? (this.isValidDate ? this.BackColor : this.invalidDateBackColor) : SystemColors.Window); using (StringFormat format = new StringFormat(StringFormatFlags.LineLimit | StringFormatFlags.NoClip | StringFormatFlags.NoWrap)) { format.LineAlignment = StringAlignment.Center; if (this.RightToLeft == RightToLeft.Yes) { format.Alignment = StringAlignment.Far; } using (SolidBrush foreBrush = new SolidBrush(this.Enabled ? (this.isValidDate ? this.ForeColor : this.invalidDateForeColor) : SystemColors.GrayText), selectedBrush = new SolidBrush(SystemColors.HighlightText), selectedBack = new SolidBrush(SystemColors.Highlight)) { MonthCalendar cal = this.datePicker.Picker; ICustomFormatProvider provider = cal.FormatProvider; MonthCalendarDate date = new MonthCalendarDate(cal.CultureCalendar, this.currentDate); DatePatternParser parser = new DatePatternParser(provider.ShortDatePattern, provider); string dateString = parser.ParsePattern(date, this.datePicker.UseNativeDigits ? this.datePicker.Culture.NumberFormat.NativeDigits : null); this.dayPartIndex = parser.DayPartIndex; this.monthPartIndex = parser.MonthPartIndex; this.yearPartIndex = parser.YearPartIndex; List<CharacterRange> rangeList = new List<CharacterRange>(); int dayIndex = parser.DayIndex; int monthIndex = parser.MonthIndex; int yearIndex = parser.YearIndex; if (!string.IsNullOrEmpty(parser.DayString)) { rangeList.Add(new CharacterRange(dayIndex, parser.DayString.Length)); } if (!string.IsNullOrEmpty(parser.MonthString)) { rangeList.Add(new CharacterRange(monthIndex, parser.MonthString.Length)); } if (!string.IsNullOrEmpty(parser.YearString)) { rangeList.Add(new CharacterRange(yearIndex, parser.YearString.Length)); } format.SetMeasurableCharacterRanges(rangeList.ToArray()); Rectangle layoutRect = this.ClientRectangle; e.Graphics.DrawString(dateString, this.Font, foreBrush, layoutRect, format); Region[] dateRegions = e.Graphics.MeasureCharacterRanges(dateString, this.Font, layoutRect, format); this.dayBounds = dateRegions[0].GetBounds(e.Graphics); this.monthBounds = dateRegions[1].GetBounds(e.Graphics); this.yearBounds = dateRegions[2].GetBounds(e.Graphics); if (this.selectedPart == SelectedDatePart.Day) { e.Graphics.FillRectangle(selectedBack, this.dayBounds.X, this.dayBounds.Y - 2, this.dayBounds.Width + 1, this.dayBounds.Height + 1); e.Graphics.DrawString(parser.DayString, this.Font, selectedBrush, this.dayBounds.X - 2, this.dayBounds.Y - 2); } if (this.selectedPart == SelectedDatePart.Month) { e.Graphics.FillRectangle(selectedBack, this.monthBounds.X, this.monthBounds.Y - 2, this.monthBounds.Width + 1, this.monthBounds.Height + 1); e.Graphics.DrawString(parser.MonthString, this.Font, selectedBrush, this.monthBounds.X - 2, this.monthBounds.Y - 2); } if (this.selectedPart == SelectedDatePart.Year) { e.Graphics.FillRectangle(selectedBack, this.yearBounds.X, this.yearBounds.Y - 2, this.yearBounds.Width + 1, this.yearBounds.Height + 1); e.Graphics.DrawString(parser.YearString, this.Font, selectedBrush, this.yearBounds.X - 2, this.yearBounds.Y - 2); } } } base.OnPaint(e); }
static private int MeasureDisplayStringWidthExact(Graphics graphics, string text, Font font) { var ranges = new[] { new CharacterRange(0, text.Length) }; var format = new StringFormat(); format.SetMeasurableCharacterRanges(ranges); var rect = new RectangleF(0, 0, 1000, 1000); var regions = graphics.MeasureCharacterRanges(text, font, rect, format); rect = regions[0].GetBounds(graphics); return (int)(rect.Right + 1.0f); }
/// <summary> /// Get the smallest encompassing Region of the supplied string using the supplied Font, Rectangle and StringFormat on the supplied Graphics context. /// </summary> /// <param name="g">Graphics context object to measure string on.</param> /// <param name="str">String to measure.</param> /// <param name="fnt">Font to use for string.</param> /// <param name="rct">Rectangle to measure string in.</param> /// <param name="strfmt">StringFormat to use when measuring the string.</param> /// <returns>Smallest Region encompassing the Text of the Control supplied using the StringFormat supplied.</returns> public static Region GetStringFormattedStringRegion(Graphics g, string str, Font fnt, Rectangle rct, StringFormat strfmt) { RectangleF rctF = new RectangleF(rct.X, rct.Y, rct.Width, rct.Height); CharacterRange[] ranges = {new CharacterRange(0, str.Length)}; Region[] regions = new Region[1]; strfmt.SetMeasurableCharacterRanges(ranges); regions = g.MeasureCharacterRanges(str, fnt, rctF, strfmt); return regions[0]; }
/// <summary> /// Get the smallest encompassing Region for the supplied string, Font and Graphics context. /// </summary> /// <param name="g">Graphics context object to measure string on.</param> /// <param name="str">String to measure.</param> /// <param name="fnt">Font to use for string.</param> /// <returns>Smallest Region encompassing the supplied string.</returns> public static Region GetGraphicalStringRegion(Graphics g, string str, Font fnt) { StringFormat format = new StringFormat(); RectangleF rect = new RectangleF(0, 0, 1000, 1000); CharacterRange[] ranges = {new CharacterRange(0, str.Length)}; Region[] regions = new Region[1]; format.SetMeasurableCharacterRanges(ranges); regions = g.MeasureCharacterRanges(str, fnt, rect, format); return regions[0]; }
public void MeasureCharacterRanges_StringFormat_LineAlignment_DirectionVertical () { if (font == null) Assert.Ignore ("Couldn't create required font"); string text = "Hello Mono::"; CharacterRange[] ranges = new CharacterRange[1]; ranges[0] = new CharacterRange (5, 4); StringFormat string_format = new StringFormat (); string_format.FormatFlags = StringFormatFlags.DirectionVertical; string_format.SetMeasurableCharacterRanges (ranges); using (Bitmap bitmap = new Bitmap (20, 20)) { using (Graphics g = Graphics.FromImage (bitmap)) { string_format.LineAlignment = StringAlignment.Near; Region[] regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format); Assert.AreEqual (1, regions.Length, "Near.Region"); RectangleF near = regions[0].GetBounds (g); string_format.LineAlignment = StringAlignment.Center; regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format); Assert.AreEqual (1, regions.Length, "Center.Region"); RectangleF center = regions[0].GetBounds (g); string_format.LineAlignment = StringAlignment.Far; regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format); Assert.AreEqual (1, regions.Length, "Far.Region"); RectangleF far = regions[0].GetBounds (g); Assert.IsTrue (near.X < center.X, "near-center/X"); Assert.AreEqual (near.Y, center.Y, 0.1, "near-center/Y"); Assert.AreEqual (near.Width, center.Width, 0.1, "near-center/Width"); Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height"); Assert.IsTrue (center.X < far.X, "center-far/X"); Assert.AreEqual (center.Y, far.Y, 0.1, "center-far/Y"); Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width"); Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height"); } } }
public void MeasureCharacterRanges_Prefix () { if (font == null) Assert.Ignore ("Couldn't create required font"); string text = "Hello &Mono::"; CharacterRange[] ranges = new CharacterRange[1]; ranges[0] = new CharacterRange (5, 4); StringFormat string_format = new StringFormat (); string_format.SetMeasurableCharacterRanges (ranges); using (Bitmap bitmap = new Bitmap (20, 20)) { using (Graphics g = Graphics.FromImage (bitmap)) { SizeF size = g.MeasureString (text, font, new Point (0, 0), string_format); RectangleF layout_rect = new RectangleF (0.0f, 0.0f, size.Width, size.Height); // here & is part of the measure and visible string_format.HotkeyPrefix = HotkeyPrefix.None; Region[] regions = g.MeasureCharacterRanges (text, font, layout_rect, string_format); RectangleF bounds_none = regions[0].GetBounds (g); // here & is part of the measure (range) but visible as an underline string_format.HotkeyPrefix = HotkeyPrefix.Show; regions = g.MeasureCharacterRanges (text, font, layout_rect, string_format); RectangleF bounds_show = regions[0].GetBounds (g); Assert.IsTrue (bounds_show.Width < bounds_none.Width, "Show<None"); // here & is part of the measure (range) but invisible string_format.HotkeyPrefix = HotkeyPrefix.Hide; regions = g.MeasureCharacterRanges (text, font, layout_rect, string_format); RectangleF bounds_hide = regions[0].GetBounds (g); Assert.AreEqual (bounds_hide.Width, bounds_show.Width, "Hide==None"); } } }
private void MeasureCharacterRanges (string text, int first, int length) { if (font == null) Assert.Ignore ("Couldn't create required font"); CharacterRange[] ranges = new CharacterRange[1]; ranges[0] = new CharacterRange (first, length); StringFormat string_format = new StringFormat (); string_format.FormatFlags = StringFormatFlags.NoClip; string_format.SetMeasurableCharacterRanges (ranges); using (Bitmap bitmap = new Bitmap (20, 20)) { using (Graphics g = Graphics.FromImage (bitmap)) { SizeF size = g.MeasureString (text, font, new Point (0, 0), string_format); RectangleF layout_rect = new RectangleF (0.0f, 0.0f, size.Width, size.Height); g.MeasureCharacterRanges (text, font, layout_rect, string_format); } } }