C# 如何在c语言中以编程方式集中一组控件#
我是这个社区的新手,我有一个应用程序,可以通过编程方式添加控件。C# 如何在c语言中以编程方式集中一组控件#,c#,winforms,visual-studio,C#,Winforms,Visual Studio,我是这个社区的新手,我有一个应用程序,可以通过编程方式添加控件。 我想集中所有添加的控件,就像选择它们并在VisualStudio上按center一样。不,我不想把每个人都放在一边。 下面是我用来获取所有控件的代码: private void GetAllControl(Control c, List<Control> list) { //gets all controls and saves them to a list foreac
我想集中所有添加的控件,就像选择它们并在VisualStudio上按center一样。不,我不想把每个人都放在一边。 下面是我用来获取所有控件的代码:
private void GetAllControl(Control c, List<Control> list)
{
//gets all controls and saves them to a list
foreach (Control control in c.Controls)
{
list.Add(control);
}
}
//And then call it like this
List<Control> list = new List<Control>();
GetAllControl(PNL_custom, list);
foreach (Play_panel m in list)
{
//And here I want to insert that center code
}
private void GetAllControl(控件c,列表)
{
//获取所有控件并将其保存到列表中
foreach(c.Controls中的控件)
{
列表。添加(控制);
}
}
//然后像这样称呼它
列表=新列表();
GetAllControl(PNL_自定义,列表);
foreach(播放列表中的面板m)
{
//我想在这里插入中心代码
}
提前感谢,
VBTheory获取控件容器(另一个控件或窗体)的宽度和高度。控件的坐标是以像素为单位的距离,相对于其容器的左上角(即(0,0))。因此,您只需将控件的
x
坐标设置为(表单宽度-控件宽度)/2
。高度也是如此。您的所有控件都将添加到一个容器中(最有可能是表单),尽管它们可以很容易地放在一个分组框中等
要使它们在容器内居中对齐,您需要做一点数学运算,然后自己定位:)
在容器中居中控制之前,需要找到容器的中点。这将是容器的宽度/2:容器的高度/2
我将使用一个名为cmdButton1的控件来突出显示-您将希望遍历控件列表并依次对所有控件执行此操作
int midParentX = cmdButton1.Parent.width / 2;
int midParentX = cmdButton1.Parent.height / 2;
然后,可以在此中点定位控件:
cmdButton1.Location.X = midParentX;
cmdButton1.Location.Y = midParentY;
但是,您的控件(在我的示例中是cmdButton)被锚定在控件的左上角(0,0),因此我们需要将其前后移动一半宽度和高度
cmdButton1.Location.X -= (cmdButton1.width / 2);
cmdButton1.Location.Y -= (cmdButton1.height / 2);
更相关的是:
foreach (Play_panel m in list)
{
int pX = m.Parent.width;
int pY = m.Parent.height;
m.Location.X = (pX / 2) - (m.width / 2);
m.Location.Y = (pY / 2) - (m.height / 2);
}
“不,我不想把每个人都放在一边。” 因此,您希望“对齐”控件列表?…如中所示: 格式化-->对齐-->中心 格式化-->对齐-->中间线 如果是,则计算每个控件的中心,并将X,Y坐标相加,以便计算“平均”点(质心)。现在,您可以迭代控件,并根据所需的方向将其用作对齐X或Y值。只需减去一半的宽度或高度,并保留另一个值 比如:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
List<Control> list = new List<Control>();
GetAllControl(PNL_custom, list);
CenterControls(list, Direction.Vertical);
}
public enum Direction
{
Vertical,
Horizontal
}
private void CenterControls(List<Control> controls, Direction direction)
{
if (controls.Count > 1)
{
int xSum = 0;
int ySum = 0;
Point center;
foreach (Control ctl in controls)
{
center = new Point(ctl.Location.X + ctl.Width / 2, ctl.Location.Y + ctl.Height / 2);
xSum = xSum + center.X;
ySum = ySum + center.Y;
}
Point average = new Point(xSum / controls.Count, ySum / controls.Count);
foreach (Control ctl in controls)
{
switch (direction)
{
case Direction.Vertical:
ctl.Location = new Point(average.X - ctl.Width / 2, ctl.Location.Y);
break;
case Direction.Horizontal:
ctl.Location = new Point(ctl.Location.X, average.Y - ctl.Height / 2);
break;
}
}
}
}
private void GetAllControl(Control c, List<Control> list)
{
//gets all controls and saves them to a list
foreach (Control control in c.Controls)
{
list.Add(control);
}
}
}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
列表=新列表();
GetAllControl(PNL_自定义,列表);
中心控件(列表、方向、垂直);
}
公共枚举方向
{
垂直的,
水平的
}
专用空心中心控件(列表控件、方向)
{
如果(controls.Count>1)
{
int xSum=0;
int-ySum=0;
点中心;
foreach(控制中的控制ctl)
{
中心=新点(控制位置X+控制宽度/2,控制位置Y+控制高度/2);
xSum=xSum+center.X;
ySum=ySum+中心Y;
}
点平均值=新点(xSum/controls.Count,ySum/controls.Count);
foreach(控制中的控制ctl)
{
开关(方向)
{
案例方向。垂直:
控制位置=新点(平均.X-控制宽度/2,控制位置.Y);
打破
案例方向。横向:
控制位置=新点(控制位置X,平均值Y-控制高度/2);
打破
}
}
}
}
私有void GetAllControl(控件c,列表)
{
//获取所有控件并将其保存到列表中
foreach(c.Controls中的控件)
{
列表。添加(控制);
}
}
}
以下是如何将控件作为一个组集中起来。它几乎和以前一样,只是我们计算了组的质心必须移动多远才能成为父控件的中心。然后我们迭代所有控件,并将它们的位置偏移那么多。这将使它们居中,同时保持它们彼此的相对位置:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
List<Control> list = new List<Control>();
GetAllControl(PNL_custom, list);
CenterControlsAsGroup(list, Direction.Both); // center group in the center of the parent
}
public enum Direction
{
Vertical,
Horizontal,
Both
}
private void CenterControlsAsGroup(List<Control> controls, Direction direction)
{
if (controls.Count > 1)
{
int xSum = 0;
int ySum = 0;
Point center;
foreach (Control ctl in controls)
{
center = new Point(ctl.Location.X + ctl.Width / 2, ctl.Location.Y + ctl.Height / 2);
xSum = xSum + center.X;
ySum = ySum + center.Y;
}
Point average = new Point(xSum / controls.Count, ySum / controls.Count);
center = new Point(controls[0].Parent.Width / 2, controls[0].Parent.Height / 2);
int xOffset = center.X - average.X;
int yOffset = center.Y - average.Y;
foreach (Control ctl in controls)
{
switch (direction)
{
case Direction.Vertical:
ctl.Location = new Point(ctl.Location.X + xOffset, ctl.Location.Y);
break;
case Direction.Horizontal:
ctl.Location = new Point(ctl.Location.X, ctl.Location.Y + yOffset);
break;
case Direction.Both:
ctl.Location = new Point(ctl.Location.X + xOffset, ctl.Location.Y + yOffset);
break;
}
}
}
}
private void GetAllControl(Control c, List<Control> list)
{
//gets all controls and saves them to a list
foreach (Control control in c.Controls)
{
list.Add(control);
}
}
}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
列表=新列表();
GetAllControl(PNL_自定义,列表);
CenterControlsAsGroup(list,Direction.Both);//位于父对象中心的中心组
}
公共枚举方向
{
垂直的,
水平的,
二者都
}
专用void CenterControlsAsGroup(列表控件、方向)
{
如果(controls.Count>1)
{
int xSum=0;
int-ySum=0;
点中心;
foreach(控制中的控制ctl)
{
中心=新点(控制位置X+控制宽度/2,控制位置Y+控制高度/2);
xSum=xSum+center.X;
ySum=ySum+中心Y;
}
点平均值=新点(xSum/controls.Count,ySum/controls.Count);
中心=新点(控制[0].Parent.Width/2,控制[0].Parent.Height/2);
int xOffset=center.X—average.X;
int yOffset=中心.Y-平均.Y;
foreach(控制中的控制ctl)
{
开关(方向)
{
案例方向。垂直:
控制位置=新点(控制
public enum ArrangeOrientation : int {
None,
Horizonatal,
Vertical,
HorizontalGrid,
VerticalGrid,
TopLeftGrid,
TopRightGrid,
BottomLeftGrid,
BottomRightGrid
}
private void ArrangeButtons(List<Control> controls, List<Control> parents, ArrangeOrientation orientation, double shrinkFactor = 1d) {
if(controls == null) return;
if(parents == null) parents = new List<Control>();
List<Control> childs = new List<Control>();
Control parent = null;
foreach(Control ctrl in controls) {
if(parent == null && !parents.Contains(ctrl.Parent)) {
parents.Add(ctrl.Parent);
parent = ctrl.Parent;
}
if(parent == ctrl.Parent)
childs.Add(ctrl);
}
if(parent != null && childs.Count > 0) {
ArrangeControlsToGridLayout(childs, orientation, shrinkFactor);
ArrangeButtons(controls, parents, orientation, shrinkFactor);
}
}
private void ArrangeControlsToGridLayout(List<Control> controls, ArrangeOrientation orientation, double shrinkFactor = 1d) {
// do nothing if nothing set
if(orientation == ArrangeOrientation.None) return;
if(shrinkFactor == 0d|| shrinkFactor > 1d) shrinkFactor = 1d;
// buffer controls in separate list to avoid manipulating parameter
List<Control> ctrl = new List<Control>(controls.ToArray());
// remove invisible controls
int j = 0;
while(j < ctrl.Count) {
if(!ctrl[j].Visible) ctrl.RemoveAt(j);
else j++;
}
// loop arrangement
int count = ctrl.Count;
int xDelta, yDelta, xOffs, yOffs, y, x, columns, rows, parentWidth, parentHeight, xShrinkOffs, yShrinkOffs;
if(count >= 1) {
// parents size
parentWidth = ctrl[0].Parent.Width;
parentHeight = ctrl[0].Parent.Height;
// shrink factor offset
parentWidth = Convert.ToInt32(parentWidth * shrinkFactor);
parentHeight = Convert.ToInt32(parentHeight * shrinkFactor);
// shrink factor offset
xShrinkOffs = Convert.ToInt32((ctrl[0].Parent.Width - parentWidth) / 2d);
yShrinkOffs = Convert.ToInt32((ctrl[0].Parent.Height - parentHeight) / 2d);
// calculate columns rows grid layout
if(orientation == ArrangeOrientation.Horizonatal) {
rows = 1;
columns = count;
}
else if(orientation == ArrangeOrientation.Vertical) {
rows = count;
columns = 1;
}
else if(orientation == ArrangeOrientation.TopLeftGrid
|| orientation == ArrangeOrientation.TopRightGrid
|| orientation == ArrangeOrientation.BottomLeftGrid
|| orientation == ArrangeOrientation.BottomRightGrid) {
rows = 1;
columns = count;
}
else {
rows = Convert.ToInt32(Math.Floor(Math.Sqrt(count)));
if(Math.Sqrt(count) % 1d != 0d) rows++;
columns = count / rows + (count % rows != 0 ? 1 : 0);
}
if(orientation == ArrangeOrientation.HorizontalGrid) {
int swap = columns;
columns = rows;
rows = columns;
}
// calculate position offsets, grid distance
xDelta = parentWidth / count;
yDelta = parentHeight / count;
xOffs = xDelta / 2;
yOffs = yDelta / 2;
if(orientation == ArrangeOrientation.TopLeftGrid) {
}
else if(orientation == ArrangeOrientation.TopRightGrid) {
xOffs = parentWidth - xOffs;
xDelta = -xDelta;
}
else if(orientation == ArrangeOrientation.BottomLeftGrid) {
yOffs = parentHeight - yOffs;
yDelta = -yDelta;
}
else if(orientation == ArrangeOrientation.BottomRightGrid) {
xOffs = parentWidth - xOffs;
yOffs = parentHeight - yOffs;
xDelta = -xDelta;
yDelta = -yDelta;
}
else {
xDelta = parentWidth / columns;
yDelta = parentHeight / rows;
xOffs = xDelta / 2;
yOffs = yDelta / 2;
}
// fit controls in grid layout
Point pRoot = new Point(/*ctrl[0].Parent.Location.X + */xOffs, /*ctrl[0].Parent.Location.Y + */yOffs);
y = 0; x = 0;
for(int i = 0; i < count; i++) {
if(orientation == ArrangeOrientation.VerticalGrid) {
// actual x/y - points zero based index
y = Convert.ToInt32(Math.Floor((double)i % rows));
// next row? zero based index
if(i % rows == 0 && i != 0) x++;
}
else {
// actual x/y - points zero based index
x = Convert.ToInt32(Math.Floor((double)i % columns));
// next row? zero based index
if(i % columns == 0 && i != 0) y++;
if(orientation == ArrangeOrientation.TopLeftGrid
|| orientation == ArrangeOrientation.TopRightGrid
|| orientation == ArrangeOrientation.BottomLeftGrid
|| orientation == ArrangeOrientation.BottomRightGrid)
y = x;
} // assign controls to grid
ctrl[i].Location = new Point(pRoot.X + x * xDelta - ctrl[i].Size.Width / 2 + xShrinkOffs, pRoot.Y + y * yDelta - ctrl[i].Size.Height / 2 + yShrinkOffs);
}
}
}
public enum Direction
{
Vertical,
Horizontal,
Both
}
public void CenterControls(List<Control> controls, Direction direction)
{
if (controls.Count > 1)
{
int controls_sum_width = 0;
int controls_seperation = 20;
int parentwidth = 0;
Point center;
foreach (Control ctl in controls)
{
controls_sum_width = controls_sum_width + ctl.Width + controls_seperation;
}
Point Container_center = new Point(controls[0].Parent.Width / 2, controls[0].Parent.Height / 2);
parentwidth = controls[0].Parent.Width;
int xoffset = (parentwidth - controls_sum_width) / 2;
int Location_X = 0;
foreach (Control ctl in controls)
{
center = new Point( ctl.Width / 2, ctl.Height / 2);
int yOffset = Container_center.Y - center.Y;
switch (direction)
{
case Direction.Vertical:
ctl.Location = new Point(ctl.Location.X + xoffset, ctl.Location.Y);
break;
case Direction.Horizontal:
ctl.Location = new Point(ctl.Location.X, yOffset);
break;
case Direction.Both:
ctl.Location = new Point(Location_X + xoffset, yOffset);
break;
}
Location_X = Location_X + ctl.Width+ controls_seperation;
}
}
else
{
Point parent_center;
Point center;
parent_center = new Point(controls[0].Parent.Width / 2, controls[0].Parent.Height / 2);
center = new Point(controls[0].Location.X + controls[0].Width / 2, controls[0].Location.Y + controls[0].Height / 2);
int xOffset = parent_center.X - center.X;
int yOffset = parent_center.Y - center.Y;
switch (direction)
{
case Direction.Vertical:
controls[0].Location = new Point(controls[0].Location.X + xOffset, controls[0].Location.Y);
break;
case Direction.Horizontal:
controls[0].Location = new Point(controls[0].Location.X, controls[0].Location.Y + yOffset);
break;
case Direction.Both:
controls[0].Location = new Point(controls[0].Location.X + xOffset, controls[0].Location.Y + yOffset);
break;
}
}
}
public void GetAllControl(Control c, List<Control> list)
{
//gets all controls and saves them to a list
foreach (Control control in c.Controls)
{
list.Add(control);
}
}