简体   繁体   English

如何只重绘特殊区域?

[英]How do I only repaint a special area?

My English skill is poor because I'm not a native speaker.我的英语水平很差,因为我不是母语人士。

I have written an editor that has a visual effect like Highlight.我写了一个编辑器,它具有像 Highlight 一样的视觉效果。 The editor has DrawingControl for representing a visual effect as the following code.编辑器具有用于表示视觉效果的 DrawingControl,如下代码所示。

public class HighlightTextBox : TextBox
{
    private DrawingControl renderCanvas;
}

public class DrawingControl : FrameworkElement
{
    private VisualCollection visuals;
    private DrawingVisual visual;

    public DrawingControl()
    {
        visual = new DrawingVisual();
        visuals = new VisualCollection(this);
        visuals.Add(visual);
    }

    public DrawingContext GetContext()
    {
        return visual.RenderOpen();
    }

    protected override int VisualChildrenCount
    {
        get { return visuals.Count; }
    }

    protected override Visual GetVisualChild(int index)
    {
        if (index < 0 || index >= visuals.Count)
            throw new ArgumentOutOfRangeException();
        return visuals[index];
    }
}

If a user input a character on the editor then the TextChanged event is called.如果用户在编辑器上输入字符,则调用 TextChanged 事件。 And after that, the OnRender function is called by calling InvalidateVisual function.之后,通过调用 InvalidateVisual function 调用 OnRender function。

TextChanged(s,e)
{
    InvalidateVisual();
}

Then all text is drawn that has a visual effect by using DrawingContext in the DrawingControl.然后使用 DrawingControl 中的 DrawingContext 绘制所有具有视觉效果的文本。

protected override void OnRender(DrawingContext drawingContext)
{
    if (this.bSingleLineChanged)
    {
        this.SingleLineChangedRender(drawingContext);
        this.bSingleLineChanged = false;
        return;
    }
    else
        this.AllRender(drawingContext);
}

private void AllRender(DrawingContext drawingContext)
{
    // Calculate drawing position, texts, etc...

    var dc = this.renderCanvas.GetContext();

    // Here draw texts

    dc.Close();
    base.OnRender(drawingContext);
}

As a result, drawing process success when all text is repainted but the way take a lot of time.结果,当所有文本都被重新绘制时,绘制过程成功,但这种方式需要很多时间。 To shorten a drawing time, I tried to repaint only an updated part.为了缩短绘图时间,我尝试只重新绘制更新的部分。 The part drawing process is defined in the SingleLineChangedRender function.零件绘制过程在 SingleLineChangedRender function 中定义。 Also the logic of the function alike with an AllRender function. function 的逻辑也与 AllRender function 类似。

Now I would explain a problem.现在我要解释一个问题。 I calculate an updated part and have written logic in the SingleLineChangedRender function.我计算了一个更新的部分,并在 SingleLineChangedRender function 中编写了逻辑。 But when is called a GetContext function in the SingleLineChangedRender function all drawn context is erased.但是当在 SingleLineChangedRender function 中调用 GetContext function 时,所有绘制的上下文都会被删除。

private void SingleLineChangedRender(DrawingContext drawingContext)
{
    // I think that here all drawn contexts are erased.
    var dc = this.renderCanvas.GetContext();

    // Here draw texts

    dc.Close();    
    base.OnRender(drawingContext);
}

I want to repaint only an updated part but I can't do it because all contexts is erased when the GetContext function is called.我只想重新绘制更新的部分,但我不能这样做,因为当调用 GetContext function 时,所有上下文都会被删除。

I would like to seek your advice to solve this problem.我想征求你的意见来解决这个问题。

Thank you for reading.感谢您的阅读。

I follow your advice and I solved this problem by updating DrawingControl class.我听从了您的建议,并通过更新 DrawingControl class 解决了这个问题。

I updated the TextViewer class as a container of DrawingContext.我将 TextViewer class 更新为 DrawingContext 的容器。

As a result, the class has formed the following.结果,class 形成了以下。

public class TextViewer : FrameworkElement
{
    // For highlighting of the selected line.
    private DrawingVisual selectionLineAppearance = new DrawingVisual();

    // For highlighting tokens of the lines.
    private VisualCollection lines;

    protected override int VisualChildrenCount
    {
        // selectionLineAppearance + lines
        get { return 1+lines.Count; }
    }

    protected override Visual GetVisualChild(int index)
    {
        if (index < 0 || index >= VisualChildrenCount)
            throw new ArgumentOutOfRangeException();

        if (index == 0) return this.selectionLineAppearance;
        return lines[index-1];
    }

    // logices
    ...

}

Now, Editor can repaint only an updated part.现在,编辑器只能重绘更新的部分。 Addition can paint the appearance of the selected line.加法可以绘制所选线的外观。

Thank you for your advice.感谢您的意见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM