当前位置: 首页>>代码示例>>C#>>正文


C# Graphics.MeasureCharacterRanges方法代码示例

本文整理汇总了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;
		}
开发者ID:ChrisMoreton,项目名称:Test3,代码行数:33,代码来源:Text.cs

示例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("描画モード不明");
 }
开发者ID:utau1116b,项目名称:Hello-World,代码行数:31,代码来源:StringMeasure.cs

示例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();
        }
开发者ID:cmrazek,项目名称:ProbeNpp,代码行数:10,代码来源:Util.cs

示例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;
 }
开发者ID:KnowNo,项目名称:test-code-backup,代码行数:11,代码来源:QTabItemBase.cs

示例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);
        }
开发者ID:PokeD,项目名称:Ohana3DS-Rebirth,代码行数:21,代码来源:DrawingUtils.cs

示例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);
        }
开发者ID:Wesco,项目名称:SaveWorkbook,代码行数:14,代码来源:Settings.cs

示例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);
 }
开发者ID:silverio,项目名称:rush,代码行数:15,代码来源:CreateFont.cs

示例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);
 }
开发者ID:NanQi,项目名称:demo,代码行数:16,代码来源:TextUtil.cs

示例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;
     }
 }
开发者ID:ImaginationSydney,项目名称:flashdevelop,代码行数:22,代码来源:DrawHelper.cs

示例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;
        }
开发者ID:nhmkdev,项目名称:cardmaker,代码行数:18,代码来源:TextMarkup.cs

示例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);
        }
开发者ID:art-drobanov,项目名称:RecoveryStar,代码行数:18,代码来源:TextUtil.cs

示例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);
        }
开发者ID:segafan,项目名称:wme1_jankavan_tlc_edition-repo,代码行数:19,代码来源:ComplexText.cs

示例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;
            }
开发者ID:Cireson,项目名称:EntityFramework6,代码行数:43,代码来源:StringRenderer.cs

示例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));
        }
开发者ID:RSchwoerer,项目名称:Terminals,代码行数:21,代码来源:TerminalControl.Drawing.cs

示例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);
 }
开发者ID:huamanhtuyen,项目名称:VNACCS,代码行数:21,代码来源:ComponentProperty.cs


注:本文中的System.Drawing.Graphics.MeasureCharacterRanges方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。