0

我找到了这个链接:http: //msdn.microsoft.com/en-us/library/system.windows.media.formattedtext.aspx

这是一个如何通过覆盖该OnRender方法来绘制文本的示例。

我已经使用以下代码覆盖了 的OnRender方法Window,但文本不可见。我做错了什么?

protected override void OnRender(DrawingContext drawingContext)
{
    string testString = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor";

    // Create the initial formatted text string.
    FormattedText formattedText = new FormattedText(
        testString,
        CultureInfo.GetCultureInfo("en-us"),
        FlowDirection.LeftToRight,
        new Typeface("Verdana"),
        32,
        Brushes.Black);

    // Set a maximum width and height. If the text overflows these values, an ellipsis "..." appears.
    formattedText.MaxTextWidth = 300;
    formattedText.MaxTextHeight = 240;

    // Use a larger font size beginning at the first (zero-based) character and continuing for 5 characters.
    // The font size is calculated in terms of points -- not as device-independent pixels.
    formattedText.SetFontSize(36 * (96.0 / 72.0), 0, 5);

    // Use a Bold font weight beginning at the 6th character and continuing for 11 characters.
    formattedText.SetFontWeight(FontWeights.Bold, 6, 11);

    // Use a linear gradient brush beginning at the 6th character and continuing for 11 characters.
    formattedText.SetForegroundBrush(
                            new LinearGradientBrush(
                            Colors.Orange,
                            Colors.Teal,
                            90.0),
                            6, 11);

    // Use an Italic font style beginning at the 28th character and continuing for 28 characters.
    formattedText.SetFontStyle(FontStyles.Italic, 28, 28);

    // Draw the formatted text string to the DrawingContext of the control.
    drawingContext.DrawText(formattedText, new Point(10, 0));
}
4

2 回答 2

1

覆盖窗口的 OnRender 可能不是一件好事;我原以为它会很好,但我仍然认为它一定与布局管理器、剪辑边界或相关的东西有关,因为将覆盖放到 Window 类中肯定会调用该代码。绘图上下文都是延迟渲染,我怀疑父视觉要么没有使用任何绘图上下文,要么用布局中的不透明框覆盖它。

无论如何,如果您在项目中进行自定义控件并在其中添加 OnRender 代码,并将其添加到 xaml 的根目录中,则此代码片段可以正常工作。

于 2010-01-05T16:54:00.450 回答
0

我刚刚写了这个FrameworkElement,它将实现带有轮廓的渲染文本几何。花了大约 10 分钟研究,然后 20 分钟写。

用法:

<Grid>
    <local:OutlineTextElement Text="Hello" />
</Grid>

代码:

public class OutlineTextElement : FrameworkElement
{
    public FontFamily FontFamily { get; set; }
    public FontWeight FontWeight { get; set; }
    public FontStyle FontStyle { get; set; }
    public int FontSize { get; set; }
    public int Stroke { get; set; }

    public SolidColorBrush Background { get; set; }
    public SolidColorBrush Foreground { get; set; }
    public SolidColorBrush BorderBrush { get; set; }

    private Typeface Typeface;
    private VisualCollection Visuals;
    private Action RenderTextAction;
    private DispatcherOperation CurrentDispatcherOperation;

    private string text;
    public string Text
    {
        get { return text; }
        set
        {
            if (String.Equals(text, value, StringComparison.CurrentCulture))
                return;

            text = value;
            QueueRenderText();
        }
    }

    public OutlineTextElement()
    {
        Visuals = new VisualCollection(this);

        FontFamily = new FontFamily("Century");
        FontWeight = FontWeights.Bold;
        FontStyle = FontStyles.Normal;
        FontSize = 240;
        Stroke = 4;
        Typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretches.Normal);

        Foreground = Brushes.Black;
        BorderBrush = Brushes.Gold;

        RenderTextAction = () => { RenderText(); };
        Loaded += (o, e) => { QueueRenderText(); };
    }

    private void QueueRenderText()
    {
        if (CurrentDispatcherOperation != null)
            CurrentDispatcherOperation.Abort();

        CurrentDispatcherOperation = Dispatcher.BeginInvoke(RenderTextAction, DispatcherPriority.Render, null);

        CurrentDispatcherOperation.Aborted += (o, e) => { CurrentDispatcherOperation = null; };
        CurrentDispatcherOperation.Completed += (o, e) => { CurrentDispatcherOperation = null; };
    }

    private void RenderText()
    {
        Visuals.Clear();

        DrawingVisual visual = new DrawingVisual();
        using (DrawingContext dc = visual.RenderOpen())
        {
            FormattedText ft = new FormattedText(Text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, Typeface, FontSize, Foreground);
            Geometry geometry = ft.BuildGeometry(new Point(0.0, 0.0)); 
            dc.DrawText(ft, new Point(0.0, 0.0));
            dc.DrawGeometry(null, new Pen(BorderBrush, Stroke), geometry);

        }
        Visuals.Add(visual);
    }

    protected override Visual GetVisualChild(int index)
    {
        return Visuals[index];
    }

    protected override int VisualChildrenCount
    {
        get { return Visuals.Count; }
    }
}
于 2011-09-15T21:32:01.360 回答