如何在边框中绘制一条将其分成两半的直线
How to draw in the border a straight line dividing it into 2 halves
我在 wpf 应用程序中有一个边框。
- 如何在边框画一条直线将它分成两半?
- 我有一个用于单击边框的处理程序。 MouseDown后如何在按下点(p.Y,p.X)画一个黑点?以及第二次按下后如何擦除旧的并绘制新的?
要画一条线来划分边界,你可以像这样使用 Line 或 Polyline:
(你需要找到你的边框高度和重量才能得到半点,假设它是 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);
}
画点可以用Ellipse,也可以用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);
}
我在 wpf 应用程序中有一个边框。
- 如何在边框画一条直线将它分成两半?
- 我有一个用于单击边框的处理程序。 MouseDown后如何在按下点(p.Y,p.X)画一个黑点?以及第二次按下后如何擦除旧的并绘制新的?
要画一条线来划分边界,你可以像这样使用 Line 或 Polyline:
(你需要找到你的边框高度和重量才能得到半点,假设它是 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);
}
画点可以用Ellipse,也可以用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);
}