用WPF线连接两个椭圆

本文关键字:两个 WPF 连接 | 更新日期: 2023-09-27 18:19:07

我写了一些省略号到txtBox,然后点击添加到画布上。

我想写出应该用直线连接的省略号。但是如何添加这条直线的坐标呢?我认为它应该是椭圆的中心但是怎么知道它的坐标呢?

XAML:

<Window x:Class="draw.ellipse"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="draw" Height="768" Width="1024" WindowStartupLocation="CenterScreen" Name="Create" Closed="Create_Closed">
<Canvas Name="can" Background="White" MouseDown="can_MouseDown">
    <TextBox Name="txbNumber" Height="34" Canvas.Left="797" TextWrapping="Wrap" Text="5" Canvas.Top="76" Width="209" FontSize="18"/>
    <Button Name="btnCreate" Content="create" Canvas.Left="797" Canvas.Top="138" Width="209" Height="66" FontSize="18" Click="btnCreate_Click"/>
    <TextBox Name="txb1" Height="34" Canvas.Left="797" TextWrapping="Wrap" Text="3" Canvas.Top="222" Width="78" FontSize="18"/>
    <Button Name="btnCreateEdge" Content="create edge" Canvas.Left="797" Canvas.Top="276" Width="209" Height="66" FontSize="18" Click="btnCreateEdge_Click"/>
    <TextBox Name="txb2" Height="34" Canvas.Left="928" TextWrapping="Wrap" Text="4" Canvas.Top="222" Width="78" FontSize="18"/>
    <Label Name='lbl' Content="Label" Canvas.Left="797" Canvas.Top="374" Height="54" Width="169" FontSize="20"/>
</Canvas>  

c#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Graduate
{
/// <summary>
/// Логика взаимодействия для Create.xaml
/// </summary>
public partial class draw: Window
{
    public draw()
    {
        InitializeComponent();
    }
    int n, i, k, k2, j, j2, f;
    Ellipse[] v;
    private void btnCreate_Click(object sender, RoutedEventArgs e)
    {
        n = Convert.ToInt16(txbNumber.Text);
        f = n;
        v = new Ellipse[n];
        var elipsy = can.Children.OfType<Ellipse>().ToList();
        foreach (var ellipses in elipsy)
        {
            can.Children.Remove(ellipses);
        }
    }

    private void Create_Closed(object sender, EventArgs e)
    {
        Application.Current.Shutdown();
    }
    private void can_MouseDown(object sender, MouseButtonEventArgs e)
    {
        Ellipse myEllipse = new Ellipse();
        SolidColorBrush mySolidColorBrush = new SolidColorBrush();
        mySolidColorBrush.Color = Colors.Transparent;
        for (i = 0; i < n; i++)
        {
            v[i] = new Ellipse();
            v[i].Fill = mySolidColorBrush;
            v[i].StrokeThickness = 2;
            v[i].Stroke = Brushes.Black;
            v[i].Width = 75;
            v[i].Height = 75;
            v[i].Margin = new Thickness(e.GetPosition(can).X, e.GetPosition(can).Y, 0, 0);

            can.Children.Add(v[i]);
        }
        if (n <= 0)
            return;
        n--;
    }
    private void btnCreateEdge_Click(object sender, RoutedEventArgs e)
    {
        k2 = Convert.ToInt16(txb1.Text);
        j2 = Convert.ToInt16(txb2.Text);
        k = f - k2;
        j = f - j2;
        Line line = new Line();
        //line.X1 = v[k].;
        can.Children.Add(line);
    }
}

}

用WPF线连接两个椭圆

当处理程序被调用时,省略号被放置在彼此的顶部,所以即使您找到了在它们中放入一行的方法,您也可能看不到它。

回答你的问题

  1. 通过Canvas找到椭圆的位置。顶部和画布左侧。有了这些信息,再加上它的宽度或高度,你就可以得到椭圆的中心了。

  2. 你可以使用#1的信息为你的X1, X2和Y1, Y2的直线连接省略号