WPF 中的 Windows 窗体绘制等效事件 [英] Windows Form Paint equivalent event in WPF

查看:15
本文介绍了WPF 中的 Windows 窗体绘制等效事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用 PAINT 事件在 Winows 窗体应用程序的面板上绘制波浪.但是在使用 WPF 时,我没有找到任何与具有 Paint 事件的 Panel 等效的元素.谷歌搜索了很多,但没有多大用处.

I have used the PAINT event to draw a wave on a Panel in Winows Form Application. But when using it WPF, I didn't find any such element equivalent to a Panel which has a Paint Event. Googled a lot too but no great use.

嗯,我需要在 WPF 中绘制一个波形,所以建议使用 PaintArgsEvent 的适当解决方案或一个新的解决方案.

Well, I need to draw a waveform in WPF so suggest appropriate solutions wrt PaintArgsEvent or a new solution altogether.

谢谢!

推荐答案

您正在寻找 DrawingVisual 班级

来自第一个链接:

DrawingVisual 是一个轻量级的绘图类,用于渲染形状、图像或文本.此类被认为是轻量级的,因为它不提供布局或事件处理,从而提高了其性能.因此,绘图是背景和剪贴画的理想选择.

The DrawingVisual is a lightweight drawing class that is used to render shapes, images, or text. This class is considered lightweight because it does not provide layout or event handling, which improves its performance. For this reason, drawings are ideal for backgrounds and clip art.

<小时>

您还可以访问 PolyLine 类 您可以向其中添加点集合.此示例是经过修改的 MSDN论坛示例


You also have access to a PolyLine Class that you can add a point Collection to. This example is a modified MSDN Forum example

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        float x0 = 100f;
        float y0 = 100f;
        Polyline myPoly = new Polyline();
        PointCollection polyPoints = myPoly.Points;
        Point[] points = new Point[200];

        for (int j = 0; j < 200; j++)
        {
            points[j] = new Point();
            points[j].X = x0 + j;
            points[j].Y = y0 -
            (float)(Math.Sin((2 * Math.PI * j) / 200) * (200 / (2 * Math.PI)));
        }

        for (int i = 0; i < points.Length ; i++)
        {
            polyPoints.Add(points[i]);
        }

        myPoly.Stroke = Brushes.Green;
        myPoly.StrokeThickness = 5;
        StackPanel mainPanel = new StackPanel();
        mainPanel.Children.Add(myPoly);
        this.Content = mainPanel;

    }
}

<小时>

和修改后的 MSDN 示例:


And a Modified MSDN example:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        float x0 = 100f;
        float y0 = 100f;
        Point[] points = new Point[200];

        for (int j = 0; j < 200; j++)
        {
            points[j] = new Point();
            points[j].X = x0 + j;
            points[j].Y = y0 -
            (float)(Math.Sin((2 * Math.PI * j) / 200) * (200 / (2 * Math.PI)));
        }

        DrawingBrush db = new DrawingBrush(CreateDrawingVisualRectangle(points).Drawing);
        StackPanel mainPanel = new StackPanel();
        mainPanel.Background = db;
        this.Content = mainPanel;

    }

    private DrawingVisual CreateDrawingVisualRectangle( Point[] pointarray)
    {
        DrawingVisual drawingVisual = new DrawingVisual();

        // Retrieve the DrawingContext in order to create new drawing content.
        DrawingContext drawingContext = drawingVisual.RenderOpen();

       // Create a rectangle and draw it in the DrawingContext.
       for (int i = 0; i < pointarray.Length-1; i++)
       {
           drawingContext.DrawLine(new Pen(new SolidColorBrush(Colors.Blue), 2), pointarray[i], pointarray[i + 1]);
       }

       // Persist the drawing content.
       drawingContext.Close();

       return drawingVisual;
     }

}

这篇关于WPF 中的 Windows 窗体绘制等效事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆