public static class Sort { #region 設定PanelControl上按鈕顯示位置 /// <summary> /// 設定按鈕顯示位置 /// </summary> /// <param name="targetPanel">需要調整按鈕順序的Panel</param> /// <param name="buttonSpace">按鈕間隔</param> public static void SetButtonCenter(ScrollableControl targetPanel, int buttonSpace) { int length = 0; int maxHeight = 0; List<Control> listBtn = new List<Control>(); System.Windows.Forms.Control.ControlCollection c = targetPanel.Controls; foreach (Control btn in c) { listBtn.Add(btn); length += btn.Width + buttonSpace; if (maxHeight < btn.Height)//擷取最大高度 { maxHeight = btn.Height; } } int pnlLength = targetPanel.Width; if (length > pnlLength) //本身按鈕的長度總和大於了panel的長度,不調整 { return; } int startPos = ((pnlLength - length) / 2); int yPos = 0; if (maxHeight < targetPanel.Height) { yPos = (targetPanel.Height - maxHeight) / 2;//距離panel上邊框的距離 } else { yPos = targetPanel.Height / 10;//距離panel上邊框的距離 } int xPos = startPos; listBtn.Sort(new ButtonSort()); foreach (Control btn in listBtn) { btn.Location = new System.Drawing.Point(xPos, yPos); xPos += btn.Width + buttonSpace; } } #endregion #region 設定Control上按鈕顯示位置 /// <summary> /// 設定按鈕顯示位置 /// </summary> /// <param name="container">需要調整按鈕順序的容器控制項</param> /// <param name="buttonSpace">按鈕間隔</param> public static void SetButtonCenter(Control container, int buttonSpace) { int length = 0; int maxHeight = 0; List<Control> listControl = new List<Control>(); System.Windows.Forms.Control.ControlCollection c = container.Controls; foreach (Control btn in c) { listControl.Add(btn); length += btn.Width + buttonSpace; if (maxHeight < btn.Height)//擷取最大高度 { maxHeight = btn.Height; } } int pnlLength = container.Width; if (length > pnlLength) //本身按鈕的長度總和大於了panel的長度,不調整 { return; } int startPos = ((pnlLength - length) / 2); int yPos = 0; if (maxHeight < container.Height) { yPos = (container.Height - maxHeight) / 2;//距離panel上邊框的距離 } else { yPos = container.Height / 10;//距離panel上邊框的距離 } int xPos = startPos; listControl.Sort(new ButtonSort()); foreach (Control btn in listControl) { btn.Location = new System.Drawing.Point(xPos, yPos); xPos += btn.Width + buttonSpace; } } #endregion }
public class ButtonSort : IComparer<Control> { #region IComparer<Button> Members //IComparer<T> 介面:定義類型為比較兩個對象而實現的方法。 public int Compare(Control x, Control y) { if (x.TabIndex >= y.TabIndex) { return 1; } else { return -1; } } #endregion }
Sort類完善版本(修正傳入控制項集合大小不一致,排序後文本顯示問題)
public static class Sort { #region 設定PanelControl上按鈕顯示位置 /// <summary> /// 設定按鈕顯示位置 /// </summary> /// <param name="targetPanel">需要調整按鈕順序的Panel</param> /// <param name="buttonSpace">按鈕間隔</param> public static void SetButtonCenter(ScrollableControl targetPanel, int buttonSpace) { int length = 0; int maxHeight = 0; bool controlsHeightSame = true;//控制項高度是否一致 List<Control> lisControl = new List<Control>(); System.Windows.Forms.Control.ControlCollection controls = targetPanel.Controls; foreach (Control btn in controls) { lisControl.Add(btn); length += btn.Width + buttonSpace; if (maxHeight < btn.Height)//擷取最大高度 { maxHeight = btn.Height; } } //判斷控制項高度是否一致 //lisControl.ForEach(delegate(Control control) //{ // if (control.Height != maxHeight) // { // controlsHeightSame = false; // } //}); lisControl.ForEach(control => { if (control.Height != maxHeight) { controlsHeightSame = false; } }); int pnlLength = targetPanel.Width; if (length > pnlLength) //本身按鈕的長度總和大於了panel的長度,不調整 { return; } int startPos = ((pnlLength - length) / 2); int yPos = 0; int xPos = startPos; lisControl.Sort(new ButtonSort()); //控制項繪製的起點是左上方的頂點,yPos即控制項的左上方頂點的y座標 if (controlsHeightSame)//控制項高度一致 { if (maxHeight < targetPanel.Height) { yPos = (targetPanel.Height - maxHeight) / 2;//距離panel上邊框的距離 } else { yPos = targetPanel.Height / 10;//距離panel上邊框的距離 } foreach (Control btn in lisControl) { btn.Location = new System.Drawing.Point(xPos, yPos); xPos += btn.Width + buttonSpace; } } else//控制項大小不一致,每個控制項的yPos單獨計算 { foreach (Control btn in lisControl) { yPos = (targetPanel.Height - btn.Height) / 2;//距離panel上邊框的距離 btn.Location = new System.Drawing.Point(xPos, yPos); xPos += btn.Width + buttonSpace; } } } #endregion #region 設定Control上按鈕顯示位置 /// <summary> /// 設定按鈕顯示位置 /// </summary> /// <param name="container">需要調整按鈕順序的容器控制項</param> /// <param name="buttonSpace">按鈕間隔</param> public static void SetButtonCenter(Control container, int buttonSpace) { int length = 0; int maxHeight = 0; bool controlsHeightSame = true;//控制項高度是否一致 List<Control> listControl = new List<Control>(); System.Windows.Forms.Control.ControlCollection c = container.Controls; foreach (Control btn in c) { listControl.Add(btn); length += btn.Width + buttonSpace; if (maxHeight < btn.Height)//擷取最大高度 { maxHeight = btn.Height; } } //判斷控制項高度是否一致 //listControl.ForEach(delegate(Control control) //{ // if (control.Height != maxHeight) // { // controlsHeightSame = false; // } //}); listControl.ForEach(control => { if (control.Height != maxHeight) { controlsHeightSame = false; } }); int pnlLength = container.Width; if (length > pnlLength) //本身按鈕的長度總和大於了panel的長度,不調整 { return; } int startPos = ((pnlLength - length) / 2); int yPos = 0; int xPos = startPos; listControl.Sort(new ButtonSort()); //控制項繪製的起點是左上方的頂點,yPos即控制項的左上方頂點的y座標 if (controlsHeightSame)//控制項高度一致 { if (maxHeight < container.Height) { yPos = (container.Height - maxHeight) / 2;//距離panel上邊框的距離 } else { yPos = container.Height / 10;//距離panel上邊框的距離 } foreach (Control btn in listControl) { btn.Location = new System.Drawing.Point(xPos, yPos); xPos += btn.Width + buttonSpace; } } else//控制項大小不一致,每個控制項的yPos單獨計算 { foreach (Control btn in listControl) { yPos = (container.Height - btn.Height) / 2;//距離panel上邊框的距離 btn.Location = new System.Drawing.Point(xPos, yPos); xPos += btn.Width + buttonSpace; } } } #endregion }
以上就是C# 容器上控制項排序的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!