我在wpf应用程序中有一个边框。
答案 0 :(得分:0)
要绘制一条线来划分边界,可以使用“线”或“折线”,如下所示:
(您需要找到边框的高度和重量来获得半点,假设是200,300)
public MainWindow()
{
InitializeComponent();
canvas.Children.Clear();
Point[] points = new Point[2]
{
new Point(0, 100),
new Point(300 , 100)
};
DrawLine(points);
}
private void DrawLine(Point[] points)
{
Polyline line = new Polyline();
PointCollection collection = new PointCollection();
foreach (Point p in points)
{
collection.Add(p);
}
line.Points = collection;
line.Stroke = new SolidColorBrush(Colors.Red);
line.StrokeThickness = 1;
canvas.Children.Add(line);
}
要绘制点,可以使用椭圆,也可以使用e.GetPosition()
来获得鼠标单击的位置:
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
\\ Remove the last object from the canvas
if(Canvas.Children.Count>0)
canvas.Children.RemoveAt(canvas.Children.Count-1);
Ellipse ellipse = new Ellipse();
ellipse.Fill = Brushes.Sienna;
ellipse.Width = 10;
ellipse.Height = 10;
ellipse.StrokeThickness = 2;
canvas.Children.Add(ellipse);
\\Set the position of the point.
Canvas.SetLeft(ellipse, e.GetPosition(canvas).X);
Canvas.SetTop(ellipse, e.GetPosition(canvas).Y);
}