自动连线代码分享
实现上面多个按钮相同名称的自动连线
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;
using System.Windows.Media;
namespace WpfApplication1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public double verticalHeight = 30; // 垂直线的高度
public double horizonWidth = 50; // 水平的宽度
public double leftBtn = 0; // 左侧按钮的位置
public double typeNameCount = 0;
public MainWindow()
{
InitializeComponent();
List<ButtonStyle> btnStyleList = new List<ButtonStyle>();
ButtonStyle buttonStyle1 = new ButtonStyle();
buttonStyle1.Name = "测试";
buttonStyle1.OriginValue = 1000;
buttonStyle1.RemainValue = 200;
buttonStyle1.IsVisible = true;
btnStyleList.Add(buttonStyle1);
ButtonStyle buttonStyle2 = new ButtonStyle();
buttonStyle2.Name = "测试";
buttonStyle2.OriginValue = 1000;
buttonStyle2.RemainValue = 200;
buttonStyle2.IsVisible = true;
btnStyleList.Add(buttonStyle2);
ButtonStyle buttonStyle3 = new ButtonStyle();
buttonStyle3.Name = "测试1";
buttonStyle3.OriginValue = 1000;
buttonStyle3.RemainValue = 200;
buttonStyle3.IsVisible = true;
btnStyleList.Add(buttonStyle3);
ButtonStyle buttonStyle4 = new ButtonStyle();
buttonStyle4.Name = "测试1";
buttonStyle4.OriginValue = 1000;
buttonStyle4.RemainValue = 200;
buttonStyle4.IsVisible = true;
btnStyleList.Add(buttonStyle4);
ButtonStyle buttonStyle5 = new ButtonStyle();
buttonStyle5.Name = "测试1";
buttonStyle5.OriginValue = 1000;
buttonStyle5.RemainValue = 200;
buttonStyle5.IsVisible = true;
btnStyleList.Add(buttonStyle5);
for (int i = 0; i < btnStyleList.Count; i++)
{
Button btn = (Button)this.FindName("button" + (i + 1));
if (btnStyleList[i].IsVisible == false)
{
btn.Visibility = Visibility.Hidden;
}
btn.Content = btnStyleList[i].Name;
double left = btn.Margin.Left + btn.Width / 2;
double top = btn.Margin.Top + btn.Height;
// 与右侧类型一致,垂直线高度减半
if (i < btnStyleList.Count - 1)
{
if (btnStyleList[i].Name == btnStyleList[i + 1].Name)
{
typeNameCount = typeNameCount + 1;
int count = btnStyleList.FindAll(x => x.Name == btnStyleList[i].Name).Count;
// 判断当前是否是居中位置
if (typeNameCount == (count / 2 + 1))
{
AddVerticalLine(left, top, verticalHeight);
}
else
{
// 绘制垂直
AddVerticalLine(left, top, verticalHeight / 2);
}
}
else
{
AddVerticalLine(left, top, verticalHeight);
typeNameCount = 0;
}
}
else
{
if (btnStyleList[i - 1].Name == btnStyleList[i].Name)
{
typeNameCount = typeNameCount + 1;
int count = btnStyleList.FindAll(x => x.Name == btnStyleList[i].Name).Count;
// 判断当前是否是居中位置
if (typeNameCount == (count / 2 + 1))
{
AddVerticalLine(left, top, verticalHeight);
}
else
{
// 绘制垂直
AddVerticalLine(left, top, verticalHeight / 2);
}
}
else
{
AddVerticalLine(left, top, verticalHeight);
typeNameCount = 0;
}
}
if (i > 0)
{
// 查找左侧类型是否一致
if ((btnStyleList[i - 1].Name == btnStyleList[i].Name))
{
// 绘制水平线
AddHorizonLine(leftBtn, top + verticalHeight / 2, left);
}
}
leftBtn = left;
}
}
public class ButtonStyle
{
public String Name;
public Double OriginValue;
public Double RemainValue;
public Boolean IsVisible;
}
private void AddHorizonLine(double start, double top, double end)
{
Line l = new Line();
l.X1 = start;
l.X2 = end;
l.Y1 = top;
l.Y2 = top;
l.Stroke = System.Windows.Media.Brushes.Coral;
l.StrokeThickness = 1;
l.StrokeDashArray = new DoubleCollection() { 2, 3 };
l.StrokeDashCap = PenLineCap.Triangle;
l.StrokeEndLineCap = PenLineCap.Square;
l.StrokeStartLineCap = PenLineCap.Round;
xGrid.Children.Add(l);
}
private void AddVerticalLine(double left, double top, double height)
{
Line l = new Line();
l.X1 = left;
l.X2 = left;
l.Y1 = top;
l.Y2 = top + height;
l.Stroke = System.Windows.Media.Brushes.Coral;
l.StrokeThickness = 1;
l.StrokeDashArray = new DoubleCollection() { 2, 3 };
l.StrokeDashCap = PenLineCap.Triangle;
l.StrokeEndLineCap = PenLineCap.Square;
l.StrokeStartLineCap = PenLineCap.Round;
xGrid.Children.Add(l);
}
}
}
另外一种方案,每个框下面绘制一半的线,计算好哪些需要哪些不需要。