本文整理汇总了C#中System.Windows.Controls.TextBlock.Measure方法的典型用法代码示例。如果您正苦于以下问题:C# TextBlock.Measure方法的具体用法?C# TextBlock.Measure怎么用?C# TextBlock.Measure使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Controls.TextBlock
的用法示例。
在下文中一共展示了TextBlock.Measure方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TextLayout
protected unsafe override bool TextLayout(textpara_t* text, byte** fontpath)
{
TextBlock label = new TextBlock();
label.Inlines.Add(text->Text);
label.FontFamily = new FontFamily(text->FontName);
label.FontSize = text->fontsize;
switch ((char)text->just)
{
case 'l':
label.TextAlignment = TextAlignment.Left;
break;
case 'n':
label.TextAlignment = TextAlignment.Center;
break;
case 'r':
label.TextAlignment = TextAlignment.Right;
break;
}
label.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
Size desiredSize = label.DesiredSize;
text->width = desiredSize.Width;
text->height = desiredSize.Height;
text->yoffset_centerline = 3;
return true;
}
示例2: PostInfoDisplay
public PostInfoDisplay(double textRightEdge,
double viewRightEdge,
Geometry newTextGeometry,
string body)
{
if (brush == null)
{
brush = new SolidColorBrush(Color.FromArgb(0x20, 0x48, 0x3d, 0x8b));
brush.Freeze();
Brush penBrush = new SolidColorBrush(Colors.DarkSlateBlue);
penBrush.Freeze();
solidPen = new Pen(penBrush, 0.5);
solidPen.Freeze();
dashPen = new Pen(penBrush, 0.5);
dashPen.DashStyle = DashStyles.Dash;
dashPen.Freeze();
}
this.textGeometry = newTextGeometry;
TextBlock tb = new TextBlock();
tb.Text = "Blog Entry: " + body;
const int MarginWidth = 8;
this.postGrid = new Grid();
this.postGrid.RowDefinitions.Add(new RowDefinition());
this.postGrid.RowDefinitions.Add(new RowDefinition());
ColumnDefinition cEdge = new ColumnDefinition();
cEdge.Width = new GridLength(MarginWidth);
ColumnDefinition cEdge2 = new ColumnDefinition();
cEdge2.Width = new GridLength(MarginWidth);
this.postGrid.ColumnDefinitions.Add(cEdge);
this.postGrid.ColumnDefinitions.Add(new ColumnDefinition());
this.postGrid.ColumnDefinitions.Add(cEdge2);
System.Windows.Shapes.Rectangle rect = new System.Windows.Shapes.Rectangle();
rect.RadiusX = 6;
rect.RadiusY = 3;
rect.Fill = brush;
rect.Stroke = Brushes.DarkSlateBlue;
Size inf = new Size(double.PositiveInfinity, double.PositiveInfinity);
tb.Measure(inf);
this.postGrid.Width = tb.DesiredSize.Width + 2 * MarginWidth;
Grid.SetColumn(rect, 0);
Grid.SetRow(rect, 0);
Grid.SetRowSpan(rect, 1);
Grid.SetColumnSpan(rect, 3);
Grid.SetRow(tb, 0);
Grid.SetColumn(tb, 1);
this.postGrid.Children.Add(rect);
this.postGrid.Children.Add(tb);
Canvas.SetLeft(this.postGrid, Math.Max(viewRightEdge - this.postGrid.Width - 20.0, textRightEdge + 20.0));
Canvas.SetTop(this.postGrid, textGeometry.GetRenderBounds(solidPen).Top);
this.Children.Add(this.postGrid);
}
示例3: SetTooltip
/// <summary>
/// Sets the tooltip to the TextBlock only if the entire text does not
/// fit in the available space (i.e. the TextBlock shows Ellipis at the end)
/// </summary>
/// <param name="textBlock">TextBlock object</param>
private static void SetTooltip(TextBlock textBlock)
{
if (textBlock == null)
return;
textBlock.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
var width = textBlock.DesiredSize.Width;
ToolTipService.SetToolTip(textBlock, textBlock.ActualWidth < width ? textBlock.Text : null);
}
示例4: ComputeAutoTooltip
/// <summary>
/// Assigns the ToolTip for the given TextBlock based on whether the text is trimmed
/// </summary>
private static void ComputeAutoTooltip(TextBlock textBlock)
{
textBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
var width = textBlock.DesiredSize.Width;
if (textBlock.ActualWidth < width)
{
ToolTipService.SetToolTip(textBlock, textBlock.Text);
}
else
{
ToolTipService.SetToolTip(textBlock, null);
}
}
示例5: ComputeAutoToolTip
private static void ComputeAutoToolTip(TextBlock textBlock)
{
// It is necessary to call Measure so that the DesiredSize gets updated.
textBlock.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
var desiredWidth = textBlock.DesiredSize.Width;
if (textBlock.ActualWidth < desiredWidth)
{
ToolTipService.SetToolTip(textBlock, textBlock.Text);
}
else
{
ToolTipService.SetToolTip(textBlock, null);
}
}
示例6: MeasureString
public static Size MeasureString(string s, int offset)
{
if (string.IsNullOrEmpty(s))
{
return new Size(0, 0);
}
var textBlock = new TextBlock()
{
Text = s,
FontSize = 16
};
textBlock.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
return new Size(textBlock.DesiredSize.Width + offset, textBlock.DesiredSize.Height);
}
示例7: CalculateLeftOffset
public static double CalculateLeftOffset(this ICanvasRange range)
{
double offset = 0;
for (double dy = range.Ymin; dy < range.Ymax; dy += range.YTick)
{
TextBlock tb = new TextBlock
{
Text = dy.ToString(),
TextAlignment = TextAlignment.Right
};
tb.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
if (offset < tb.DesiredSize.Width) offset = tb.DesiredSize.Width;
}
return offset + 5;
}
示例8: StringToBitmapSource
public static BitmapSource StringToBitmapSource(this string str, int fontSize, System.Windows.Media.Color foreground, System.Windows.Media.Color background)
{
TextBlock tbX = new TextBlock();
tbX.FontFamily = new System.Windows.Media.FontFamily("Consolas");
tbX.Foreground = new System.Windows.Media.SolidColorBrush(foreground);
tbX.Background = new System.Windows.Media.SolidColorBrush(background);
tbX.TextAlignment = TextAlignment.Center;
tbX.FontSize = fontSize;
tbX.FontStretch = FontStretches.Normal;
tbX.FontWeight = FontWeights.Medium;
tbX.Text = str;
var size = tbX.MeasureString();
tbX.Width = size.Width;
tbX.Height = size.Height;
tbX.Measure(new Size(size.Width, size.Height));
tbX.Arrange(new Rect(new Size(size.Width, size.Height)));
return tbX.ToBitmapSource();
}
示例9: ComputeAutoTooltip
private static void ComputeAutoTooltip(TextBlock textBlock)
{
textBlock.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
double width = textBlock.DesiredSize.Width;
if (textBlock.ActualWidth < width)
{
var toolBlock = new TextBlock()
{
Text = textBlock.Text,
FontSize = GetAutoTooltipFontSize(textBlock)
};
ToolTipService.SetToolTip(textBlock, toolBlock);
}
else
{
ToolTipService.SetToolTip(textBlock, null);
}
}
示例10: GenerateXLabels
public static void GenerateXLabels(this ICanvasRange range, double leftOffset)
{
for (double dx = range.Xmin; dx <= range.Xmax; dx += range.XTick)
{
Point pt = range.NormalizePoint(new Point(dx, range.Ymin));
Line tick = new Line
{
Stroke = Brushes.Black,
X1 = pt.X,
Y1 = pt.Y,
X2 = pt.X,
Y2 = pt.Y - 5
};
range.ChartCanvas.Children.Add(tick);
TextBlock tb = new TextBlock { Text = dx.ToString() };
tb.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
range.TextCanvas.Children.Add(tb);
Canvas.SetLeft(tb, leftOffset + pt.X - tb.DesiredSize.Width / 2);
Canvas.SetTop(tb, pt.Y + 2 + tb.DesiredSize.Height / 2);
}
}
示例11: InternalGetSize
private Size InternalGetSize(char c, double fontSize, bool bold, bool italic)
{
var @event = new AutoResetEvent(false);
var size = new Size();
((Action)(() =>
{
var textBlock = new TextBlock
{
Text = Convert.ToString(c),
FontSize = fontSize,
FontFamily = _fontFamily,
FontStyle = italic ? FontStyles.Italic : FontStyles.Normal,
FontWeight = bold ? FontWeights.Bold : FontWeights.Normal
};
textBlock.Measure(new Size(1024.0, 1024.0));
size = new Size(textBlock.ActualWidth, textBlock.ActualHeight);
@event.Set();
})).OnUIThread();
@event.WaitOne();
return size;
}
示例12: ComputeTrimAndShowToolTip
/// <summary>
/// Aprēķina vajadzību pēc uzpeldošā padoma un piešķir to teksta elementam <param name="textBlock"/>.
/// </summary>
private static void ComputeTrimAndShowToolTip(TextBlock textBlock) {
if (textBlock.Text == string.Empty) { textBlock.ToolTip=null; return; } // Neesošs teksts mēdz izmērities lielāks, nekā aizpildīts, tāpēc šeit novērš tukšos taisnstūrus.
textBlock.Measure(new Size(textBlock.TextWrapping == TextWrapping.Wrap ? textBlock.ActualWidth:Double.PositiveInfinity, Double.PositiveInfinity));
double measuredDimension, actualDimension;
if (textBlock.TextWrapping == TextWrapping.Wrap) {
measuredDimension=textBlock.DesiredSize.Height;
actualDimension=textBlock.ActualHeight;
} else {
measuredDimension=textBlock.DesiredSize.Width;
actualDimension=textBlock.ActualWidth;
}
if (actualDimension < measuredDimension) {
// Garāku tekstu rāda ilgāk, lai paspēj izlasīt.
ToolTipService.SetShowDuration(textBlock, Math.Min(textBlock.Text.Length*200, 30000));
textBlock.ToolTip=new ToolTip {
Content=textBlock.Text, Style=GetTrimAndShowToolTip(textBlock)
};
} else {
ToolTipService.SetShowDuration(textBlock, 5000);
textBlock.ToolTip=null;
}
}
示例13: PrintFixedDocument
private void PrintFixedDocument()
{
PrintDialog dialog;
if (LoadPrintDialog(out dialog))
{
var document = new FixedDocument();
var page = new FixedPage();
var t = new TextBlock
{
FontFamily = font,
FontSize = kFontSize,
Text = Data.ToString()
};
page.Children.Add(t);
t.Measure(new Size(document.DocumentPaginator.PageSize.Width, document.DocumentPaginator.PageSize.Width));
page.Height = t.DesiredSize.Height;
page.Width = t.DesiredSize.Width;
var p = new PageContent { Child = page };
document.Pages.Add(p);
Print(document, dialog);
}
}
示例14: cmdPrint_Click
private void cmdPrint_Click(object sender, RoutedEventArgs e)
{
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == true)
{
// Create the text.
Run run = new Run("This is a test of the printing functionality in the Windows Presentation Foundation.");
// Wrap it in a TextBlock.
TextBlock visual = new TextBlock(run);
visual.Margin = new Thickness(15);
// Allow wrapping to fit the page width.
visual.TextWrapping = TextWrapping.Wrap;
// Scale the TextBlock in both dimensions.
double zoom;
if (Double.TryParse(txtScale.Text, out zoom))
{
visual.LayoutTransform = new ScaleTransform(zoom / 100, zoom / 100);
// Get the size of the page.
Size pageSize = new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight);
// Trigger the sizing of the element.
visual.Measure(pageSize);
visual.Arrange(new Rect(0, 0, pageSize.Width, pageSize.Height));
// Print the element.
printDialog.PrintVisual(visual, "A Scaled Drawing");
}
else
{
MessageBox.Show("Invalid scale value.");
}
}
}
示例15: ApplyStyle
/// <summary>
/// Draws the chart without data, just the frame
/// </summary>
public void ApplyStyle()
{
double tolerance = 0.001d;
Point pt;
Line gridLine, tick;
double dx, dy;
TextBlock tb = new TextBlock();
Size size = new Size();
Canvas.SetLeft(this.chartCanvas, leftOffset);
Canvas.SetBottom(this.chartCanvas, bottomOffset);
this.chartCanvas.Width = Math.Abs(this.textCanvas.Width - leftOffset - rightOffset);
this.chartCanvas.Height = Math.Abs(this.textCanvas.Height - bottomOffset);
Rectangle chartRect = new Rectangle();
chartRect.Stroke = Brushes.Black;
chartRect.Fill = Brushes.White;
chartRect.Width = this.chartCanvas.Width;
chartRect.Height = this.chartCanvas.Height;
this.chartCanvas.Children.Add(chartRect);
// Create vertical gridlines:
if (this.drawXGrid)
{
for (dx = this.xmin + this.xtickInitDelta; dx + tolerance < this.xmax; dx += this.xtickDelta)
{
gridLine = ChartsHelper.CreateLine(this.lineColor, this.gridLineThickness, this.gridLinePattern);
pt = this.NormalizePoint(new Point(dx, this.ymin));
gridLine.X1 = pt.X;
gridLine.Y1 = pt.Y;
pt = this.NormalizePoint(new Point(dx, this.ymax));
gridLine.X2 = pt.X;
gridLine.Y2 = pt.Y;
this.chartCanvas.Children.Add(gridLine);
}
}
// Create horizontal gridlines:
if (this.drawYGrid)
{
for (dy = this.ymin + this.ytickInitDelta; dy + tolerance < this.ymax; dy += this.ytickDelta)
{
gridLine = ChartsHelper.CreateLine(this.lineColor, this.gridLineThickness, this.gridLinePattern);
pt = this.NormalizePoint(new Point(this.xmin, dy));
gridLine.X1 = pt.X;
gridLine.Y1 = pt.Y;
pt = this.NormalizePoint(new Point(this.xmax, dy));
gridLine.X2 = pt.X;
gridLine.Y2 = pt.Y;
this.chartCanvas.Children.Add(gridLine);
}
}
// Create x-axis tick marks
if (this.drawXTicks)
{
for (dx = this.xmin + this.xtickInitDelta; dx + tolerance < this.xmax; dx += this.xtickDelta)
{
pt = NormalizePoint(new Point(dx, this.ymin));
tick = ChartsHelper.CreateLine(this.lineColor, this.tickLineThickness, LinePatternEnum.Solid);
tick.X1 = pt.X;
tick.Y1 = pt.Y;
tick.X2 = pt.X;
tick.Y2 = pt.Y - this.tickSize;
this.chartCanvas.Children.Add(tick);
tb = new TextBlock();
tb.Text = Math.Round(dx, 3).ToString();
tb.Measure(new Size(Double.PositiveInfinity,
Double.PositiveInfinity));
size = tb.DesiredSize;
this.textCanvas.Children.Add(tb);
Canvas.SetLeft(tb, this.leftOffset + pt.X - size.Width / 2.0d);
Canvas.SetTop(tb, pt.Y + size.Height / 2.0d);
}
}
// Create y-axis tick marks
if (this.drawYTicks)
{
for (dy = this.ymin + this.ytickInitDelta; dy + tolerance < this.ymax; dy += this.ytickDelta)
{
pt = NormalizePoint(new Point(this.xmin, dy));
tick = ChartsHelper.CreateLine(this.lineColor, this.tickLineThickness, LinePatternEnum.Solid);
tick.X1 = pt.X;
tick.Y1 = pt.Y;
tick.X2 = pt.X + this.tickSize;
tick.Y2 = pt.Y;
this.chartCanvas.Children.Add(tick);
tb = new TextBlock();
tb.Text = Math.Round(dy, 3).ToString();
tb.Measure(new Size(Double.PositiveInfinity,
Double.PositiveInfinity));
size = tb.DesiredSize;
this.textCanvas.Children.Add(tb);
Canvas.SetRight(tb, this.chartCanvas.Width + 10.0d);
//.........这里部分代码省略.........