本文整理汇总了C#中System.Drawing.Graphics.MeasureString方法的典型用法代码示例。如果您正苦于以下问题:C# Graphics.MeasureString方法的具体用法?C# Graphics.MeasureString怎么用?C# Graphics.MeasureString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Drawing.Graphics
的用法示例。
在下文中一共展示了Graphics.MeasureString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PaintJunk
public void PaintJunk(Graphics g)
{
g.FillRectangle(Brushes.Transparent, 0, 0, 1000, 1000);
using (Font font = new Font("Courier New", 20, FontStyle.Bold))
{
if (bounceCounter == -1) return;
const string str = "DEVELOPER BUILD";
float x = 0;
int timefactor = bounceCounter;
for (int i = 0; i < str.Length; i++)
{
string slice = str.Substring(i, 1);
g.PageUnit = GraphicsUnit.Pixel;
x += g.MeasureString(slice, font).Width - 1;
int offset = -i * 3 + timefactor*3;
int yofs = 0;
if (offset < 0)
{ continue; }
else
if (offset < DigitTable.Length)
yofs = DigitTable[offset];
g.DrawString(slice, font, Brushes.Black, 5 + x, 15 - yofs);
}
}
}
示例2: Draw
public override void Draw(Graphics g, Pen p)
{
base.Draw(g, p);
var r = this.RectangleF;
var sText = g.MeasureString(this.ColouredPlace.ColorSetName, new Font("Arial", 8));
g.FillRectangle(Brushes.Gray, r.Right, r.Top - sText.Height, sText.Width, sText.Height);
g.DrawString(
this.ColouredPlace.ColorSetName,
new Font("Arial", 8),
Brushes.Blue,
r.Right,
r.Top - sText.Height
);
var tokensString = this.ColouredPlace.Tokens.ToString();
var f = new Font("", 7);
sText = g.MeasureString(tokensString, f);
g.FillRectangle(Brushes.Green, r.Right, r.Bottom, sText.Width, sText.Height);
g.DrawString(
tokensString,
new Font("", 7),
Brushes.Black,
r.Right,
r.Bottom
);
}
示例3: _DrawItem
protected override void _DrawItem(Graphics g, int index, int y)
{
//base._DrawItem(g, index, y); return;
//this._Broker.LoadItem(index);
_Broker.Index = index;
BookItem item = _Broker.Current;
int x = 4;
g.DrawRectangle(this._PenBorder, x, y, _Width - 2 * x, this._ItemHeight);
g.FillRectangle(Brushes.Khaki, x + 1, y + 1, _Width - 2 * x-1, this._ItemHeight-1);
x += 3;
g.DrawString(item.Author, this._FontAutor , Brushes.Black, x, y);
SizeF size = g.MeasureString(item.Author, this._FontAutor);
x += (int)size.Width + 20;
g.DrawString(item.Title, this._FontTitle, Brushes.Blue, x, y);
size = g.MeasureString(item.Author, this._FontTitle);
if (index == this._SelectedIndex)
{
g.FillEllipse(Brushes.WhiteSmoke, _Width - 35, y + 5, 20, 20);
g.DrawEllipse(this._PenBorder, _Width - 35, y + 5, 20, 20);
}
}
示例4: Plot
/// <summary>
/// </summary>
/// <param name="graphics"></param>
/// <param name="bounds"></param>
/// <param name="min"></param>
/// <param name="max"></param>
public override void Plot(Graphics graphics, Rectangle bounds, double min, double max)
{
if (Bars == null)
return;
// Recalculate the proper string size should the chart control object font and axis color change
if (textBrush.Color != ChartControl.AxisColor || textFont != ChartControl.Font)
{
textBrush.Color = ChartControl.AxisColor;
textFont = (Font) ChartControl.Font.Clone();
SizeF size = graphics.MeasureString((CountDown ? "Volume remaining = %" : "Volume count = %") + Bars.Period.Value, textFont);
textWidth = size.Width + 5;
textHeight = size.Height + 5;
SizeF noTickSize = graphics.MeasureString(errorText, textFont);
noTickTextWidth = noTickSize.Width + 5;
noTickTextHeight = noTickSize.Height + 5;
}
// Plot the volume count message to the lower right hand corner of the chart
if (Bars.Period.Id == PeriodType.Volume)
{
double volumeCount = ShowPercent ? CountDown ? (1 - Bars.PercentComplete) * 100 : Bars.PercentComplete * 100 : CountDown ? Bars.Period.Value - volume : volume;
graphics.DrawString((CountDown ? " Volume remaining = " + volumeCount : "Volume = " + volumeCount) + (ShowPercent ? "%" : ""), ChartControl.Font, textBrush, bounds.X + bounds.Width - textWidth, bounds.Y + bounds.Height - textHeight, stringFormat);
}
else
graphics.DrawString(errorText, ChartControl.Font, textBrush, bounds.X + bounds.Width - noTickTextWidth, bounds.Y + bounds.Height - noTickTextHeight, stringFormat);
}
示例5: DrawParallelEdgeSelected
public static void DrawParallelEdgeSelected(Graphics g, IEdge e, IEnumerable<IEdge> es, Font f, PointF pSource, PointF pTarget, int Radius = 5)
{
var ps = pSource;
var pt = pTarget;
String s = "";
List<float> Heights = new List<float>();
foreach (var et in es)
{
string str = et.AttributesToString();
s += str;
Heights.Add(g.MeasureString(str, f).Height);
}
SizeF size = g.MeasureString(s, f);
PointF location = new PointF((pt.X * 2 + 3 * ps.X) / 5 - 4, (pt.Y * 2 + ps.Y * 3) / 5 - 4);
RectangleF rect = new RectangleF(location, size);
GraphicsPath roundedRect = GetRoundedRect(rect, Radius);
g.DrawLine(penHover, ps, pt);
g.FillEllipse(brushHover, (pt.X + ps.X) / 2 - 2, (pt.Y + ps.Y) / 2 - 2, 4, 4);
float hM = -1;
g.FillPath(brushAttrPanel, roundedRect);
foreach (var h in Heights)
{
hM += h;
g.DrawLine(penAttrBorderSlipt, location.X + Radius, location.Y + hM, location.X + size.Width - Radius, location.Y + hM);
}
g.DrawPath(penAttrBorder, roundedRect);
g.DrawString(s, f, brushText, rect);
}
示例6: GetPointForCenterString
public static PointF GetPointForCenterString(Graphics graphics, Rectangle rectangle, string Message, Font font, int margin, string spacer, out string formatedMessage)
{
SizeF size = graphics.MeasureString(Message, font);
if (size.Width > rectangle.Width - margin)
{
StringBuilder resultStr = new StringBuilder(Message.Length + spacer.Length);
resultStr.Append(Message);
while ((resultStr.Length > 0) && (size.Width > rectangle.Width - margin))
{
resultStr = resultStr.Remove(resultStr.Length - 1, 1);
size = graphics.MeasureString(resultStr.ToString() + spacer, font);
}
if (resultStr.Length > 0)
{
formatedMessage = resultStr.ToString() + spacer;
return new PointF(margin, rectangle.Height / 2);
}
else
{
formatedMessage = string.Empty;
return PointF.Empty;
}
}
else
{
formatedMessage = Message;
return new PointF((rectangle.Width / 2) - (size.Width / 2), (rectangle.Height / 2) - (font.SizeInPoints / 2));
}
}
示例7: EaterSubRoutine
/// <summary>
/// Replaces the trailer symbols of the specified line with "...".
/// </summary>
private string EaterSubRoutine(string stringToTrim, Font stringFont, float maxLength, Graphics grfx)
{
Debug.Assert(stringFont != null, "stringFont != null");
Debug.Assert(grfx != null, "grfx != null");
if (stringToTrim == null)
{
return "";
}
if (maxLength <= grfx.MeasureString(EAT_TRAILER, stringFont).Width)
return "";
// Maybe the input string is already of necessary length.
if (grfx.MeasureString(stringToTrim, stringFont).Width < maxLength)
{
return stringToTrim;
}
else
{
if (stringToTrim.Length < 4)
stringToTrim = "";
else
stringToTrim = stringToTrim.Remove(stringToTrim.Length - 4, 1);
return this.EaterSubRoutine(stringToTrim, stringFont, maxLength, grfx);
}
}
示例8: AppropriateFont
public static Font AppropriateFont(Graphics g, Size layoutSize, string s, Font f, out SizeF extent)
{
//if (maxFontSize == minFontSize)
// f = new Font(f.FontFamily, minFontSize, f.Style);
extent = g.MeasureString(s, f);
//if (maxFontSize <= minFontSize)
// return f;
float hRatio = layoutSize.Height / extent.Height;
float wRatio = layoutSize.Width / extent.Width;
float ratio = (hRatio < wRatio) ? hRatio : wRatio;
float newSize = f.Size * ratio;
//if (newSize < minFontSize)
// newSize = minFontSize;
//else if (newSize > maxFontSize)
// newSize = maxFontSize;
f = new Font(f.FontFamily, newSize, f.Style);
extent = g.MeasureString(s, f);
return f;
}
示例9: Plot
/// <summary>
/// </summary>
/// <param name="graphics"></param>
/// <param name="bounds"></param>
/// <param name="min"></param>
/// <param name="max"></param>
public override void Plot(Graphics graphics, Rectangle bounds, double min, double max)
{
if (Bars == null)
return;
// Recalculate the proper string size should the chart control object font and axis color change
if (textBrush.Color != ChartControl.AxisColor || textFont != ChartControl.Font)
{
textBrush.Color = ChartControl.AxisColor;
textFont = (Font) ChartControl.Font.Clone();
SizeF size = graphics.MeasureString((CountDown ? "Ticks remaining = %" : "Tick count = %") + Bars.Period.Value, textFont);
textWidth = size.Width + 5;
textHeight = size.Height + 5;
SizeF noTickSize = graphics.MeasureString(errorTick, textFont);
noTickTextWidth = noTickSize.Width + 5;
noTickTextHeight = noTickSize.Height + 5;
}
// Plot the tick count message to the lower right hand corner of the chart
if (Bars.Period.Id == PeriodType.Tick || (Bars.Period.BasePeriodType == PeriodType.Tick
&& Bars.Period.Id != PeriodType.PointAndFigure && Bars.Period.Id != PeriodType.Kagi && Bars.Period.Id != PeriodType.LineBreak))
{
int periodValue = (Bars.Period.Id == PeriodType.Tick) ? Bars.Period.Value : Bars.Period.BasePeriodValue;
double tickCount = ShowPercent ? CountDown ? (1 - Bars.PercentComplete) * 100 : Bars.PercentComplete * 100 : CountDown ? periodValue - Bars.TickCount : Bars.TickCount;
graphics.DrawString((CountDown ? " Ticks remaining = " + tickCount : "Tick count = " + tickCount) + (ShowPercent ? "%" : ""), ChartControl.Font, textBrush, bounds.X + bounds.Width - textWidth, bounds.Y + bounds.Height - textHeight, stringFormat);
}
else
graphics.DrawString(errorTick, ChartControl.Font, textBrush, bounds.X + bounds.Width - noTickTextWidth, bounds.Y + bounds.Height - noTickTextHeight, stringFormat);
}
示例10: Measure
public override SizeF Measure(Graphics graphics)
{
if (!string.IsNullOrWhiteSpace(this.Text))
{
if (this.TextSize.IsEmpty)
{
var size = new Size(GraphConstants.MinimumItemWidth, GraphConstants.MinimumItemHeight);
if (this.Input.Enabled != this.Output.Enabled)
{
if (this.Input.Enabled)
this.TextSize = graphics.MeasureString(this.Text, SystemFonts.MenuFont, size, GraphConstants.LeftMeasureTextStringFormat);
else
this.TextSize = graphics.MeasureString(this.Text, SystemFonts.MenuFont, size, GraphConstants.RightMeasureTextStringFormat);
} else
this.TextSize = graphics.MeasureString(this.Text, SystemFonts.MenuFont, size, GraphConstants.CenterMeasureTextStringFormat);
this.TextSize.Width = Math.Max(size.Width, this.TextSize.Width);
this.TextSize.Height = Math.Max(size.Height, this.TextSize.Height);
}
return this.TextSize;
} else
{
return new SizeF(GraphConstants.MinimumItemWidth, GraphConstants.MinimumItemHeight);
}
}
示例11: CreateRenderData
public RenderData CreateRenderData(Graph graph, Graphics g, int width, int height)
{
RenderData data = new RenderData
{
Graph = graph,
g = g,
Width = width,
Height = height,
AxisPen = new Pen(Color.Black),
AxisFont = new Font(FontFamily.GenericSansSerif, 10.0f),
AxisBrush = new SolidBrush(Color.Black),
AxisStringFormat = "#,##0.##",
LegendFont = new Font(FontFamily.GenericSansSerif, 10.0f),
LegendBrush = new SolidBrush(Color.Black),
LargeTickLength = 10,
SeriesPens = graph.Series.Select(s => new Pen(s.Color)).ToArray()
};
data.LegendFontMetrics = g.MeasureString("Title of a Series", data.LegendFont);
int legendWidth = (int)(data.LegendFontMetrics.Width + 20);
data.LegendRect = new Rectangle(width - legendWidth, 0, legendWidth, height);
SizeF axisFontMetrics = g.MeasureString(graph.ActualYMaximum.ToString(data.AxisStringFormat), data.AxisFont);
data.AxisYRect = new Rectangle(0, 0, (int)(axisFontMetrics.Width + data.LargeTickLength), (int)(height - axisFontMetrics.Height - data.LargeTickLength));
data.AxisXRect = new Rectangle(data.AxisYRect.Right, data.AxisYRect.Bottom, width - data.AxisYRect.Left - data.LegendRect.Width, (int)(axisFontMetrics.Height + data.LargeTickLength));
data.GraphRect = new Rectangle(data.AxisYRect.Right, data.AxisYRect.Top, width - data.AxisYRect.Width - data.LegendRect.Width,
height - data.AxisYRect.Top - data.AxisXRect.Height);
data.XMin = data.Graph.ActualXMinimum;
data.XMax = data.Graph.ActualXMaximum;
data.XInterval = data.Graph.ActualXInterval;
if (Math.Abs(data.XMax - data.XMin) < 0.0001)
{
data.XPixelInterval = width / 10.0;
data.XPixelsPerValue = 0.0;
}
else
{
data.XPixelInterval = Math.Max(1.0, data.GraphRect.Width / ((data.XMax - data.XMin) / data.XInterval));
data.XPixelsPerValue = data.GraphRect.Width / (data.XMax - data.XMin);
}
data.YMin = data.Graph.ActualYMinimum;
data.YMax = data.Graph.ActualYMaximum;
data.YInterval = data.Graph.ActualYInterval;
if (Math.Abs(data.YMax - data.YMin) < 0.0001)
{
data.YPixelInterval = height / 10.0;
data.YPixelsPerValue = 0.0;
}
else
{
data.YPixelInterval = Math.Max(1.0, data.GraphRect.Height / ((data.YMax - data.YMin) / data.YInterval));
data.YPixelsPerValue = data.GraphRect.Height / (data.YMax - data.YMin);
}
return data;
}
示例12: DrawNamedValue
public static SizeF DrawNamedValue( Graphics g, string name, string value, Font font, PointF point )
{
string _name = name + ": ";
SizeF size1 = g.MeasureString( _name, font );
g.DrawString( _name, font, Brushes.Gray, point );
SizeF size2 = g.MeasureString( value, font );
g.DrawString( value, new Font( font, FontStyle.Bold ), Brushes.Black, point.X + size1.Width, point.Y );
return new SizeF( size1.Width + size2.Width, Math.Max( size1.Height, size2.Height ) );
}
示例13: Analyze
/// <summary>
/// Analyzes UI for display. Assumes ideal height for provided width wihtout invalidating or painting.
/// </summary>
/// <param name="g">A Graphics object used for measurements.</param>
/// <param name="width">Control's width.</param>
public void Analyze(Graphics g, int width)
{
bool isTextPoolNew = textPool == null;
// If width or script has not changed, nothing to do.
if (analyzedWidth == width) return;
analyzedWidth = Width;
positionedBlocks = null;
targetHiliteIndexes = null;
// This is how we measure
StringFormat sf = StringFormat.GenericTypographic;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
// On-demand: measure a space's width - for entry text flow
// Also line height in entry text
if (spaceWidth == 0)
{
SizeF sz = g.MeasureString(spaceTestStr, getFont(fntSenseLatin), 65535, sf);
spaceWidth = (int)sz.Width;
lemmaCharHeight = sz.Height;
lemmaLineHeight = sz.Height * Magic.LemmaLineHeightScale;
sz = g.MeasureString(Magic.PinyinSpaceTestString, getFont(fntPinyinHead), 65535, sf);
pinyinSpaceWidth = sz.Width;
}
// Create text pool if needed
if (textPool == null) textPool = new TextPool();
// Headword and pinyin
// Will not measure redundantly
doAnalyzeHeadword(g);
doAnalyzePinyin(g);
// OK, now onto body
// Measure blocks in themselves on demand
// Will not measure redundantly
doMeasureBlocks(g);
// Finalize text pool - compact in memory
if (isTextPoolNew) textPool.FinishBuilding();
// Get rid of entry. Not keeping it in memory once we're done analyzing.
entry = null;
// Arrange blocks
float lemmaW = ((float)width) - headInfo.HeadwordRight - padMid - padRight;
float lemmaL = headInfo.HeadwordRight + padMid;
float lastTop = doArrangeBlocks(lemmaL, lemmaW);
// My height: bottom of headword or bottom of entry, whichever is lower
float entryHeight = lastTop + padBottom;
float zhoHeight = headInfo.HeadwordBottom + padBottom;
float trueHeight = Math.Max(entryHeight, zhoHeight);
// Assume this height, and also provided width
Size = new Size(width, (int)trueHeight);
}
示例14: Draw
public override void Draw(Graphics g, GraphPane pane)
{
SizeF size;
PointF pos;
float x, y1, y2, y3;
string label;
//Draw Axis Title
if (mTitle.Visible) {
using (Brush brush = new SolidBrush(mTitle.Color)) {
if (mTitle.Orientation == Orientation.Vertical) {
g.RotateTransform(-90);
g.TranslateTransform(-mPane.ClientRectangle.Height, 0);
size = g.MeasureString(mTitle.Text, mTitle.Font);
pos = new PointF(mTitle.Padding, mRect.Left + mRect.Width / 2 - size.Height / 2);
g.DrawString(mTitle.Text, mTitle.Font, brush, pos);
g.ResetTransform();
} else {
size = g.MeasureString(mTitle.Text, mTitle.Font);
pos = new PointF(mRect.Left + (mRect.Width / 2 - size.Width / 2), mRect.Top + mTitle.Padding);
if (mLabels.Visible) pos.Y += (mLabels.Font.Height + (mLabels.Padding * 2));
g.DrawString(mTitle.Text, mTitle.Font, brush, pos);
}
}
}
//Draw Minor Gridlines and Marks
y1 = mRect.Top - mMinorMark.Size;
y2 = mRect.Top;
for (double dx = Minimum; dx <= Maximum; dx += mMinorStep) {
x = mRect.X + (float)((dx - mMinimum) * mScale);
if (mMinorMark.Visible) g.DrawLine(mMinorMark.Pen, x, y1, x, y2);
}
//Draw Major Gridlines, Marks and Labels
y1 = mRect.Top - mMajorMark.Size;
y2 = mRect.Top;
y3 = y2 + mLabels.Padding;
using (Brush b = new SolidBrush(mLabels.Color)) {
for (double dx = Minimum; dx <= Maximum; dx += mMajorStep) {
x = mRect.X + (float)((dx - mMinimum) * mScale);
if (mMajorMark.Visible) g.DrawLine(mMajorMark.Pen, x, y1, x, y2);
if (mLabels.Visible) {
label = FormatLabel(dx);
size = g.MeasureString(label, mLabels.Font);
x = x - size.Width / 2;
if (mPane.ClientRectangle.Contains(x, y3) && mPane.ClientRectangle.Contains(x + size.Width, y3 + size.Height))
g.DrawString(label, mLabels.Font, b, x, y3);
}
}
}
//Draw The Axis
g.DrawLine(mPen, mRect.Left, mRect.Top, mRect.Right, mRect.Top);
}
示例15: Draw
public void Draw(Graphics g)
{
Font font = new Font("Helvetica", Priority * 7, FontStyle.Italic);
Brush brush = new SolidBrush(System.Drawing.Color.Black);
if (Activity != true)
brush = new SolidBrush(System.Drawing.Color.Green);
g.DrawString(TaskName, font, brush, xValue, yValue);
Height = g.MeasureString(TaskName, font).Height;
Width = g.MeasureString(TaskName, font).Width;
}