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


C# Graphics.MeasureStringCache方法代码示例

本文整理汇总了C#中System.Drawing.Graphics.MeasureStringCache方法的典型用法代码示例。如果您正苦于以下问题:C# Graphics.MeasureStringCache方法的具体用法?C# Graphics.MeasureStringCache怎么用?C# Graphics.MeasureStringCache使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Drawing.Graphics的用法示例。


在下文中一共展示了Graphics.MeasureStringCache方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetBox

		/// <summary>
		/// Returns a polygon that defines the bounding box of
		/// the text, taking into account alignment and rotation parameters.
		/// </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">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>A polygon of 4 points defining the area of this text</returns>
		public PointF[] GetBox( Graphics 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.MeasureStringCache(text, _font);
			else
                sizeF = g.MeasureStringCache(text, _font, layoutArea);

			// Create a bounding box rectangle for the text
			RectangleF rect = new RectangleF( new PointF( -sizeF.Width / 2.0F, 0.0F ), sizeF );

			Matrix matrix = new Matrix();
			SetupMatrix( matrix, x, y, sizeF, alignH, alignV, _angle );

			PointF[] pts = new PointF[4];
			pts[0] = new PointF( rect.Left, rect.Top );
			pts[1] = new PointF( rect.Right, rect.Top );
			pts[2] = new PointF( rect.Right, rect.Bottom );
			pts[3] = new PointF( rect.Left, rect.Bottom );
			matrix.TransformPoints( pts );

			return pts;
		}
开发者ID:ans10528,项目名称:MissionPlanner-MissionPlanner1.3.34,代码行数:60,代码来源:FontSpec.cs

示例2: DrawTenPower

		/// <summary>
		/// Render the specified <paramref name="text"/> to the specifed
		/// <see cref="Graphics"/> device.  The text, border, and fill options
		/// will be rendered as required.  This special case method will show the
		/// specified text as a power of 10, using the <see cref="Default.SuperSize"/>
		/// and <see cref="Default.SuperShift"/>.
		/// </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="pane">
		/// A reference to the <see cref="ZedGraph.GraphPane"/> object that is the parent or
		/// owner of this object.
		/// </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>
		public void DrawTenPower( Graphics g, GraphPane pane, string text, float x,
			float y, AlignH alignH, AlignV alignV,
			float scaleFactor )
		{
			SmoothingMode sModeSave = g.SmoothingMode;
			TextRenderingHint sHintSave = g.TextRenderingHint;
			if ( _isAntiAlias )
			{
				g.SmoothingMode = SmoothingMode.HighQuality;
				g.TextRenderingHint = TextRenderingHint.AntiAlias;
			}

			// make sure the font size is properly scaled
			Remake( scaleFactor, this.Size, ref _scaledSize, ref _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 = g.MeasureStringCache("10", _font);
            SizeF sizeText = g.MeasureStringCache(text, _superScriptFont);
			SizeF totSize = new SizeF( size10.Width + sizeText.Width,
									size10.Height + sizeText.Height * Default.SuperShift );
            float charWidth = g.MeasureStringCache("x", _superScriptFont).Width;

			// Save the old transform matrix for later restoration
			Matrix saveMatrix = g.Transform;

			g.Transform = SetupMatrix( g.Transform, x, y, totSize, alignH, alignV, _angle );

			// make a center justified StringFormat alignment
			// for drawing the text
			StringFormat strFormat = new StringFormat();
			strFormat.Alignment = _stringAlignment;

			// Create a rectangle representing the border around the
			// text.  Note that, while the text is drawn based on the
			// TopCenter position, the rectangle is drawn based on
			// the TopLeft position.  Therefore, move the rectangle
			// width/2 to the left to align it properly
			RectangleF rectF = new RectangleF( -totSize.Width / 2.0F, 0.0F,
				totSize.Width, totSize.Height );

			// If the background is to be filled, fill it
			_fill.Draw( g, rectF );

			// Draw the border around the text if required
			_border.Draw( g, pane, scaleFactor, rectF );

			// make a solid brush for rendering the font itself
			using ( SolidBrush brush = new SolidBrush( _fontColor ) )
			{
				// Draw the actual text.  Note that the coordinate system
				// is set up such that 0,0 is at the location where the
				// CenterTop of the text needs to be.
				g.DrawString( "10", _font, brush,
								( -totSize.Width + size10.Width ) / 2.0F,
								sizeText.Height * Default.SuperShift, strFormat );
				g.DrawString( text, _superScriptFont, brush,
								( totSize.Width - sizeText.Width - charWidth ) / 2.0F,
								0.0F,
								strFormat );
			}

			// Restore the transform matrix back to original
			g.Transform = saveMatrix;
//.........这里部分代码省略.........
开发者ID:ans10528,项目名称:MissionPlanner-MissionPlanner1.3.34,代码行数:101,代码来源:FontSpec.cs

示例3: 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, Graphics 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.MeasureStringCache(text, _font);
			else
                sizeF = g.MeasureStringCache(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] );
		}
开发者ID:ans10528,项目名称:MissionPlanner-MissionPlanner1.3.34,代码行数:61,代码来源:FontSpec.cs

示例4: 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( Graphics 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.MeasureStringCache(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;
		}
开发者ID:ans10528,项目名称:MissionPlanner-MissionPlanner1.3.34,代码行数:57,代码来源:FontSpec.cs

示例5: 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( Graphics g, string text, float scaleFactor, SizeF layoutArea )
		{
			Remake( scaleFactor, this.Size, ref _scaledSize, ref _font );
			SizeF size = g.MeasureStringCache( 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;
		}
开发者ID:ans10528,项目名称:MissionPlanner-MissionPlanner1.3.34,代码行数:39,代码来源:FontSpec.cs

示例6: 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( Graphics g, string text, float scaleFactor )
		{
			Remake( scaleFactor, this.Size, ref _scaledSize, ref _font );
            float width = g.MeasureStringCache(text, _font).Width;
			if ( _isDropShadow )
				width += (float)( Math.Cos( _dropShadowAngle ) * _dropShadowOffset * _font.Height );
			return width;
		}
开发者ID:ans10528,项目名称:MissionPlanner-MissionPlanner1.3.34,代码行数:24,代码来源:FontSpec.cs


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