gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:08:36 25 4
gpt4 key购买 nike

我使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com