3
我已經使用PAINT事件在Winows窗體應用程序中的Panel上繪製一個波形。但是,當使用WPF時,我沒有發現任何與具有Paint事件的面板等效的元素。 Google搜索得很多,但用處不大。WPF中的Windows Form Paint等效事件
嗯,我需要在WPF中繪製一個波形,所以建議適當的解決方案與PaintArgsEvent或一個新的解決方案。
謝謝!
我已經使用PAINT事件在Winows窗體應用程序中的Panel上繪製一個波形。但是,當使用WPF時,我沒有發現任何與具有Paint事件的面板等效的元素。 Google搜索得很多,但用處不大。WPF中的Windows Form Paint等效事件
嗯,我需要在WPF中繪製一個波形,所以建議適當的解決方案與PaintArgsEvent或一個新的解決方案。
謝謝!
您正在尋找DrawingVisual
Class
從第一個鏈接:
的DrawingVisual是用於呈現形狀,圖像或文本的輕量級繪畫班。這個類被認爲是輕量級的,因爲它不提供佈局或事件處理,從而提高了它的性能。出於這個原因,圖紙是背景和剪貼畫的理想選擇。
你也可以使用一個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;
}
}
我可以用這個類的幫助下得出的波形(例如正弦波)? – abhi154
@ abhi154是通過使用['DrawingContext'](http://msdn.microsoft.com/en-us/library/system.windows.media.drawingcontext(v = vs.110).aspx)Class。它具有與Winforms圖形對象相同的方法類型 –