wpf - WPF 中的 Windows 窗体绘制等效事件

标签 wpf vb.net waveform paintevent

我使用 PAINT 事件在 Windows 窗体应用程序的面板上绘制波浪。但是在使用 WPF 时,我没有发现任何此类元素等同于具有 Paint Event 的 Panel。也用 Google 搜索了很多,但没什么用。

好吧,我需要在 WPF 中绘制一个波形,因此建议使用 PaintArgsEvent 的适当解决方案或一个新的解决方案。

谢谢!

最佳答案

您正在寻找 DrawingVisual Class

来自第一个链接:

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 Class您可以向其中添加一个点集合。此示例是经过修改的 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 示例:

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 - WPF 中的 Windows 窗体绘制等效事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13813753/

相关文章:

wpf - CallerMemberName 与 VB 的 INotifyPropertyChange 一起使用?

vb.net - 限制列表(Of T)的大小 - VB.NET

ruby-on-rails - 构建一个Soundcloud类型的音频播放器

c# - 使用 Naudio 将每个 WAV channel 保存为单 channel WAV 文件

Java播放错误的声音频率

wpf - 使用 MVVM 和 WPF 登录 - 登录对象必须在其他窗口中使用。 (全局化)

c# - 用户报告具有锁定值的 WPF 控件;这怎么会发生?

c# - 无窗口wpf应用程序示例?

c# - 使用 sql 数据库部署 wpf 应用程序

vb.net - 有什么办法可以缩短这段代码吗?