WPF中控件的对齐方式
Posted hllxy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF中控件的对齐方式相关的知识,希望对你有一定的参考价值。
private void Align(string align) { List<double> leftList = new List<double>();//左边的点坐标 List<double> topList = new List<double>();//上 List<double> rightList = new List<double>();//右边的点=左+宽 List<double> bottomList = new List<double>();//下=上+高 List<double> btnWiList = new List<double>();//控件的宽 List<double> btnHeiList = new List<double>();//控件的高 List<UIElement> uiList = new List<UIElement>();//存放控件 double gap = 0;//平均间距 int uiNum = 0;//控件数量 double left = 0;//记录控件与的间距,设置坐标 DocumentPanel panel = documentContainer.SelectedItem as DocumentPanel; if (panel != null) { string[] namesList = panel.Caption.ToString().Split(‘|‘); if (!SvduEditBaseConfig.SvduNameInfo.Contains(namesList[0])) { Grid grid = null; if (namesList.Length >= 2 && namesList[1] == "PanelModels") { ScrollViewer scv = panel.Control as ScrollViewer; if (scv != null) grid = scv.Content as Grid; } else { grid = getGrid(panel.Control); } if (grid != null) { foreach (UIElement element in grid.Children) { if (element is Canvas) { Canvas cavPanel = (Canvas)element; AdornerLayer layer = null; try { layer = AdornerLayer.GetAdornerLayer(cavPanel); } catch (Exception ex) { string str = string.Format("获取控件失败,原因:{0}", ex.Message); MessageBox.Show(str, "提示", MessageBoxButton.OK, MessageBoxImage.Information); } if (layer != null) { foreach (UIElement ui in cavPanel.Children) { var adorners = layer.GetAdorners(ui); if (adorners != null) { uiNum++; if (ui is Button) { Button bt = (Button)ui; leftList.Add(bt.GetLeft()); topList.Add(bt.GetTop()); rightList.Add(bt.GetLeft() + bt.Width); bottomList.Add(bt.GetTop() + bt.Height); btnWiList.Add(bt.Width); btnHeiList.Add(bt.Height); uiList.Add(bt); } if (ui is Label) { Label bt = (Label)ui; leftList.Add(bt.GetLeft()); topList.Add(bt.GetTop()); rightList.Add(bt.GetLeft() + bt.Width); bottomList.Add(bt.GetTop() + bt.Height); btnWiList.Add(bt.Width); btnHeiList.Add(bt.Height); uiList.Add(bt); } if (ui is Line) { Line bt = (Line)ui; double angle = 0; double b = bt.X2 / Math.Sqrt(2); //判断是否有旋转角度 RotateTransform rotateTransform = bt.RenderTransform as RotateTransform; if (rotateTransform != null) { angle = rotateTransform.Angle; } if (angle == 135 || angle == 45)//135°的时候左对齐,相当于右对齐 { rightList.Add(bt.GetLeft() + b); btnHeiList.Add(b); btnWiList.Add(b); bottomList.Add(bt.GetTop() + b); } else if (angle == 90) { rightList.Add(bt.GetLeft() + bt.Height); btnHeiList.Add(bt.Width); btnWiList.Add(bt.StrokeThickness); bottomList.Add(bt.GetTop() + bt.Width); } else { rightList.Add(bt.GetLeft() + bt.Width); btnHeiList.Add(bt.Height); btnWiList.Add(bt.Width); bottomList.Add(bt.GetTop() + bt.Height); } leftList.Add(bt.GetLeft()); topList.Add(bt.GetTop()); uiList.Add(bt); } if (ui is TextBox) { TextBox bt = (TextBox)ui; leftList.Add(bt.GetLeft()); topList.Add(bt.GetTop()); rightList.Add(bt.GetLeft() + bt.Width); bottomList.Add(bt.GetTop() + bt.Height); btnWiList.Add(bt.Width); btnHeiList.Add(bt.Height); uiList.Add(bt); } if (ui is Rectangle) { Rectangle bt = (Rectangle)ui; leftList.Add(bt.GetLeft()); topList.Add(bt.GetTop()); rightList.Add(bt.GetLeft() + bt.Width); bottomList.Add(bt.GetTop() + bt.Height); btnWiList.Add(bt.Width); btnHeiList.Add(bt.Height); uiList.Add(bt); } if (ui is Image) { Image bt = (Image)ui; leftList.Add(bt.GetLeft()); topList.Add(bt.GetTop()); rightList.Add(bt.GetLeft() + bt.Width); bottomList.Add(bt.GetTop() + bt.Height); btnWiList.Add(bt.Width); btnHeiList.Add(bt.Height); uiList.Add(bt); } if (ui is Canvas) { Canvas bt = (Canvas)ui; leftList.Add(bt.GetLeft()); topList.Add(bt.GetTop()); rightList.Add(bt.GetLeft() + bt.Width); bottomList.Add(bt.GetTop() + bt.Height); btnWiList.Add(bt.Width); btnHeiList.Add(bt.Height); uiList.Add(bt); } if (ui is ControlValuePanel) { ControlValuePanel bt = (ControlValuePanel)ui; leftList.Add(bt.GetLeft()); topList.Add(bt.GetTop()); rightList.Add(bt.GetLeft() + bt.Width); bottomList.Add(bt.GetTop() + bt.Height); btnWiList.Add(bt.Width); btnHeiList.Add(bt.Height); uiList.Add(bt); } } } if (uiNum > 0) { #region 左对齐 if (align == "Left")//左对齐 { gap = leftList.Min(); foreach (UIElement ui in uiList) { if (ui is Button) { Button bt = (Button)ui; bt.SetLeft(gap); } if (ui is Label) { Label bt = (Label)ui; bt.SetLeft(gap); } if (ui is Line) { Line bt = (Line)ui; double angle = 0; double ness = bt.StrokeThickness; //判断是否有旋转角度 RotateTransform rotateTransform = bt.RenderTransform as RotateTransform; if (rotateTransform != null) { angle = rotateTransform.Angle; } if (angle == 135)//135°的时候左对齐,相当于右对齐 { double b = bt.Width / Math.Sqrt(2);// Canvas.SetLeft(bt, gap + ness + b); } else if (angle == 90) { bt.SetLeft(gap + ness); } else bt.SetLeft(gap); } if (ui is TextBox) { TextBox bt = (TextBox)ui; bt.SetLeft(gap); } if (ui is Rectangle) { Rectangle bt = (Rectangle)ui; bt.SetLeft(gap); } if (ui is Image) { Image bt = (Image)ui; bt.SetLeft(gap); } if (ui is Canvas) { Canvas bt = (Canvas)ui; bt.SetLeft(gap); } if (ui is ControlValuePanel) { ControlValuePanel bt = (ControlValuePanel)ui; bt.SetLeft(gap); } } } #endregion #region 水平居中 else if (align == "HCenter")//s水平居中 { gap = (bottomList.Max() - topList.Min()) / 2 + topList.Min(); foreach (UIElement ui in uiList) { if (ui is Button) { Button bt = (Button)ui; bt.SetTop(gap - (bt.Height / 2)); } if (ui is Label) { Label bt = (Label)ui; bt.SetTop(gap - (bt.Height / 2)); } if (ui is Line) { Line bt = (Line)ui; double ness = bt.StrokeThickness; double angle = 0; //判断是否有旋转角度 RotateTransform rotateTransform = bt.RenderTransform as RotateTransform; if (rotateTransform != null) { angle = rotateTransform.Angle; } if (angle == 135 || angle == 45)//135°的时候左对齐,相当于右对齐 { double b = bt.Width / Math.Sqrt(2); bt.SetTop(gap - (b / 2)); } else if (angle == 90) { bt.SetTop(gap - (bt.Width / 2)); } else bt.SetTop(gap - (bt.Height / 2)); } if (ui is TextBox) { TextBox bt = (TextBox)ui; bt.SetTop(gap - (bt.Height / 2)); } if (ui is Rectangle) { Rectangle bt = (Rectangle)ui; bt.SetTop(gap - (bt.Height / 2)); } if (ui is Image) { Image bt = (Image)ui; bt.SetTop(gap - (bt.Height / 2)); } if (ui is Canvas) { Canvas bt = (Canvas)ui; bt.SetTop(gap - (bt.Height / 2)); } if (ui is ControlValuePanel) { ControlValuePanel bt = (ControlValuePanel)ui; bt.SetTop(gap - (bt.Height / 2)); } } } #endregion #region 右对齐 else if (align == "Right")//you对齐 { gap = rightList.Max(); foreach (UIElement ui in uiList) { if (ui is Button) { Button bt = (Button)ui; bt.SetLeft(gap - bt.Width); } if (ui is Label) { Label bt = (Label)ui; bt.SetLeft(gap - bt.Width); } if (ui is Line) { Line bt = (Line)ui; double ness = bt.StrokeThickness; double angle = 0; double btLeft = bt.GetLeft(); //判断是否有旋转角度 RotateTransform rotateTransform = bt.RenderTransform as RotateTransform; if (rotateTransform != null) { angle = rotateTransform.Angle; } if (angle == 135)//135°的时候左对齐,相当于右对齐 { // Canvas.SetLeft(bt, gap + ness); //Canvas.SetLeft(bt, gap); } else if (angle == 90) { Canvas.SetLeft(bt, gap); } else if (angle == 45) { double b = bt.Width / Math.Sqrt(2); bt.SetLeft(gap - b); } else bt.SetLeft(gap - bt.Width); } if (ui is TextBox) { TextBox bt = (TextBox)ui; bt.SetLeft(gap - bt.Width); } if (ui is Rectangle) { Rectangle bt = (Rectangle)ui; bt.SetLeft(gap - bt.Width); } if (ui is Image) { Image bt = (Image)ui; bt.SetLeft(gap - bt.Width); } if (ui is Canvas) { Canvas bt = (Canvas)ui; bt.SetLeft(gap - bt.Width); } if (ui is ControlValuePanel) { ControlValuePanel bt = (ControlValuePanel)ui; bt.SetLeft(gap - bt.Width); } } } #endregion #region 上对齐 else if (align == "Top")//上对齐 { gap = topList.Min(); foreach (UIElement ui in uiList) { if (ui is Button) { Button bt = (Button)ui; bt.SetTop(gap); } if (ui is Label) { Label bt = (Label)ui; bt.SetTop(gap); } if (ui is Line) { Line bt = (Line)ui; bt.SetTop(gap); } if (ui is TextBox) { TextBox bt = (TextBox)ui; bt.SetTop(gap); } if (ui is Rectangle) { Rectangle bt = (Rectangle)ui; bt.SetTop(gap); } if (ui is Image) { Image bt = (Image)ui; bt.SetTop(gap); } if (ui is Canvas) { Canvas bt = (Canvas)ui; bt.SetTop(gap); } if (ui is ControlValuePanel) { ControlValuePanel bt = (ControlValuePanel)ui; bt.SetTop(gap); } } } #endregion #region 垂直居中 else if (align == "VCenter")//垂直居中 { gap = (rightList.Max() - leftList.Min()) / 2 + leftList.Min(); foreach (UIElement ui in uiList) { if (ui is Button) { Button bt = (Button)ui; bt.SetLeft(gap - (bt.Width / 2)); } if (ui is Label) { Label bt = (Label)ui; bt.SetLeft(gap - (bt.Width / 2)); } if (ui is Line) { Line bt = (Line)ui; double ness = bt.StrokeThickness; double angle = 0; double b = bt.Width / Math.Sqrt(2); //判断是否有旋转角度 RotateTransform rotateTransform = bt.RenderTransform as RotateTransform; if (rotateTransform != null) { angle = rotateTransform.Angle; } if (angle == 45)//135°的时候左对齐,相当于右对齐 { bt.SetLeft(gap - (b / 2)); } else if (angle == 90) { bt.SetLeft(gap - (bt.Height / 2)); } else if (angle == 135) { bt.SetLeft(gap + (b / 2)); } else bt.SetLeft(gap - (bt.Width / 2)); } if (ui is TextBox) { TextBox bt = (TextBox)ui; bt.SetLeft(gap - (bt.Width / 2)); } if (ui is Rectangle) { Rectangle bt = (Rectangle)ui; bt.SetLeft(gap - (bt.Width / 2)); } if (ui is Image) { Image bt = (Image)ui; bt.SetLeft(gap - (bt.Width / 2)); } if (ui is Canvas) { Canvas bt = (Canvas)ui; bt.SetLeft(gap - (bt.Width / 2)); } if (ui is ControlValuePanel) { ControlValuePanel bt = (ControlValuePanel)ui; bt.SetLeft(gap - (bt.Width / 2)); } } } #endregion #region 下对齐 else if (align == "Bottom")//xia对齐 { gap = bottomList.Max(); foreach (UIElement ui in uiList) { if (ui is Button) { Button bt = (Button)ui; bt.SetTop(gap - bt.Height); } if (ui is Label) { Label bt = (Label)ui; bt.SetTop(gap - bt.Height); } if (ui is Line) { Line bt = (Line)ui; double ness = bt.StrokeThickness; double angle = 0; double btLeft = bt.GetLeft(); //判断是否有旋转角度 RotateTransform rotateTransform = bt.RenderTransform as RotateTransform; if (rotateTransform != null) { angle = rotateTransform.Angle; } if (angle == 135 || angle == 45) { double b = bt.Width / Math.Sqrt(2); bt.SetTop(gap - b); } else if (angle == 90) { bt.SetTop(gap - bt.Width); } else bt.SetTop(gap - bt.Height); } if (ui is TextBox) { TextBox bt = (TextBox)ui; bt.SetTop(gap - bt.Height); } if (ui is Rectangle) { Rectangle bt = (Rectangle)ui; bt.SetTop(gap - bt.Height); } if (ui is Image) { Image bt = (Image)ui; bt.SetTop(gap - bt.Height); } if (ui is Canvas) { Canvas bt = (Canvas)ui; bt.SetTop(gap - bt.Height); } if (ui is ControlValuePanel) { ControlValuePanel bt = (ControlValuePanel)ui; bt.SetTop(gap - bt.Height); } } } #endregion #region 水平等间距 else if (align == "Horizontal") { int maxIndex = leftList.FindIndex(tt => tt.Equals(leftList.Max())); int minIndex = leftList.FindIndex(tt => tt.Equals(leftList.Min())); double ABWeidth = leftList.Max() - (leftList.Min() + btnWiList[minIndex]); gap = (ABWeidth - (btnWiList.Sum() - btnWiList[minIndex] - btnWiList[maxIndex])) / (uiNum - 1); if (gap > 0) { left = leftList.Min() + btnWiList[minIndex] + gap; uiList.RemoveAt(minIndex); btnWiList.RemoveAt(minIndex); leftList.RemoveAt(minIndex); maxIndex = leftList.FindIndex(tt => tt.Equals(leftList.Max())); uiList.RemoveAt(maxIndex); leftList.RemoveAt(maxIndex); btnWiList.RemoveAt(maxIndex); for (int i = uiList.Count - 1; i >= 0; i--) { int le = leftList.FindIndex(tt => tt.Equals(leftList.Min())); leftList.RemoveAt(le); btnWiList.RemoveAt(le); UIElement ui = uiList[le];// uiList.RemoveAt(le); if (ui is Button) { Button bt = (Button)ui; bt.SetLeft(left); left += gap + bt.Width; } if (ui is Label) { Label bt = (Label)ui; bt.SetLeft(left); left += gap + bt.Width; } if (ui is Line) { Line bt = (Line)ui; double ness = bt.StrokeThickness; double angle = 0; double btLeft = bt.GetLeft(); //判断是否有旋转角度 RotateTransform rotateTransform = bt.RenderTransform as RotateTransform; if (rotateTransform != null) { angle = rotateTransform.Angle; } double b = bt.X2 / Math.Sqrt(2); if (angle == 45)//135°的时候左对齐,相当于右对齐 { Canvas.SetLeft(bt, left); left += gap + b; } else if (angle == 90) { bt.SetLeft(left); left += gap + bt.Height; } else if (angle == 135) { bt.SetLeft(left + b); left += gap + b; } else { bt.SetLeft(left); left += gap + bt.Width; } } if (ui is TextBox) { TextBox bt = (TextBox)ui; bt.SetLeft(left); left += gap + bt.Width; } if (ui is Rectangle) { Rectangle bt = (Rectangle)ui; bt.SetLeft(left); left += gap + bt.Width; } if (ui is Image) { Image bt = (Image)ui; bt.SetLeft(left); left += gap + bt.Width; } if (ui is Canvas) { Canvas bt = (Canvas)ui; bt.SetLeft(left); left += gap + bt.Width; } if (ui is ControlValuePanel) { ControlValuePanel bt = (ControlValuePanel)ui; bt.SetLeft(left); left += gap + bt.Width; } } } } #endregion #region 垂直等间距 else if (align == "Vertical") { int maxIndex = topList.FindIndex(tt => tt.Equals(topList.Max())); int minIndex = topList.FindIndex(tt => tt.Equals(topList.Min())); double ABWeidth = topList.Max() - topList.Min() - btnHeiList[minIndex]; gap = (ABWeidth - (btnHeiList.Sum() - btnHeiList[minIndex] - btnHeiList[maxIndex])) / (uiNum - 1); if (gap > 0) { left = topList.Min() + btnHeiList[minIndex] + gap; uiList.RemoveAt(minIndex); topList.RemoveAt(minIndex); btnHeiList.RemoveAt(minIndex); maxIndex = topList.FindIndex(tt => tt.Equals(topList.Max())); uiList.RemoveAt(maxIndex); topList.RemoveAt(maxIndex); btnHeiList.RemoveAt(maxIndex); for (int i = uiList.Count - 1; i >= 0; i--) { int le = topList.FindIndex(tt => tt.Equals(topList.Min())); topList.RemoveAt(le); btnHeiList.RemoveAt(le); UIElement ui = uiList[le];// uiList.RemoveAt(le); if (ui is Button) { Button bt = (Button)ui; bt.SetTop(left); left += gap + bt.Height; } if (ui is Label) { Label bt = (Label)ui; bt.SetTop(left); left += gap + bt.Height; } if (ui is Line) { Line bt = (Line)ui; double ness = bt.StrokeThickness; double angle = 0; double btLeft = bt.GetLeft(); //判断是否有旋转角度 RotateTransform rotateTransform = bt.RenderTransform as RotateTransform; if (rotateTransform != null) { angle = rotateTransform.Angle; } double b = bt.Width / Math.Sqrt(2); if (angle == 45 || angle == 135) { bt.SetTop(left); left += gap + b; } else if (angle == 90) { bt.SetTop(left); left += gap + bt.Width; } else { bt.SetTop(left); left += gap + bt.Height; } } if (ui is TextBox) { TextBox bt = (TextBox)ui; bt.SetTop(left); left += gap + bt.Height; } if (ui is Rectangle) { Rectangle bt = (Rectangle)ui; bt.SetTop(left); left += gap + bt.Height; } if (ui is Image) { Image bt = (Image)ui; bt.SetTop(left); left += gap + bt.Height; } if (ui is Canvas) { Canvas bt = (Canvas)ui; bt.SetTop(left); left += gap + bt.Height; } if (ui is ControlValuePanel) { ControlValuePanel bt = (ControlValuePanel)ui; bt.SetTop(left); left += gap + bt.Height; } } } } #endregion } } } } } } } }
用户对框选的控件进行对齐操作,align 为对齐参数
以上是关于WPF中控件的对齐方式的主要内容,如果未能解决你的问题,请参考以下文章