将控件垂直和水平放置在其容器的中心

Keep a Control vertically and horizontally at center of its container

我试图创建一个带有边框的自定义面板,可以更改其颜色以便 "highlight" 在特定条件下面板。

专家组还需要通过文本传达某些信息。为此,我在面板中添加了一个标签。我已经尝试了将标签居中的规定方法,但出于某种原因,它总是将它放在面板的左上角。我无法将 Label 的 Dock 设置为 Fill,因为它会覆盖已创建的自定义边框。所以我需要使标签适合边框。

标签的锚点设置为 None,其位置为

new Point((ClientSize.Width - Size.Width)/2, (ClientSize.Height - Size.Height)/2);

自定义面板的代码是:

public class CustomPanel : Panel
{
    public CustomPanel(int borderThickness, Color borderColor) : base()
    {
        SetStyle(ControlStyles.AllPaintingInWmPaint | 
                 ControlStyles.UserPaint | 
                 ControlStyles.OptimizedDoubleBuffer | 
                 ControlStyles.ResizeRedraw, true);

        BackColor = SystemColors.ActiveCaption;
        BorderStyle = BorderStyle.FixedSingle;
        Size = new Size(45, 45);
        Margin = new Padding(0);
        BorderThickness = borderThickness;
        BorderColor = borderColor;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        if (BorderStyle == BorderStyle.FixedSingle)
        {
            int halfThickness = BorderThickness / 2;
            using (Pen p = new Pen(BorderColor, BorderThickness))
            {
                e.Graphics.DrawRectangle(p, new Rectangle(halfThickness,
                     halfThickness,
                     ClientSize.Width - BorderThickness, ClientSize.Height - BorderThickness));
            }
        }
    }

    public int BorderThickness { get; set; }
    public Color BorderColor { get; set; }
}

表单代码为:

private void NewPanelTest_Load(object sender, EventArgs e)
{
    CustomPanel cp = new CustomPanel(3, Color.Black);

    // Create new Label
    Label info = new Label()
    {
        Size = new Size(30, 30),
        Text = "Info",
        Anchor = AnchorStyles.None,
        TextAlign = ContentAlignment.MiddleCenter,
        Enabled = false,
        Font = new Font("Microsoft Sans Serif", 6),
        ForeColor = Color.White,
        Location = new Point(ClientSize.Width/2 - Width/2, ClientSize.Height/2 - Height/2)
    };

    cp.Controls.Add(info);

    this.Controls.Add(cp);
}

编辑:我看过类似的问题并尝试更改标签的属性但没有结果。

// Create new Label
Label info = new Label()
{
    // Same code as before

    // Different code
    Left = (this.ClientSize.Width - Size.Width) / 2,
    Top = (this.ClientSize.Height - Size.Height) / 2,
    //Location = new Point(ClientSize.Width/2 - Width/2, ClientSize.Height/2 - Height/2)
};

我也试过改变面板的填充,也没有结果。

Padding = new Padding(5);

编辑:尝试以编程方式将标签放置在面板的中心(产生 X = 0、Y = 0 的结果)

// Create new Label
Label info = new Label()
{
    // Same code as before (excluding "Left", "Top", and "Location")
};
int X = (info.ClientSize.Width - info.Width) / 2;
int Y = (info.ClientSize.Height - info.Height) / 2;
info.Location = new Point(X, Y);
MessageBox.Show(info.Location.ToString());

cp.Controls.Add(info);

我们可以通过简单的步骤实现这个

  • 将标签锚点设置为左右
  • 将标签自动调整大小设置为 false ;
  • 将 Label TextAlign 设置为 MiddleCenter;

现在将标签放在面板中间。

   int x = (panel1.Size.Width - label1.Size.Width) / 2;
    label1.Location = new Point(x, label1.Location.Y);

将控件垂直和水平放置在容器中心

最简单的选择是使用 1 列 1 行的 TableLayoutPanel 而不是 Panel。将 Label 放入其中,然后将标签的 Anchor 设置为 None 即可使标签始终在垂直和水平方向上居中。

还要绘制自定义边框,处理 TableLayoutPanelCellPaint 事件并绘制自定义边框就足够了:

private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
    var r = e.CellBounds;
    r.Width--;
    r.Height--;
    e.Graphics.DrawRectangle(Pens.Red, r);
}

嘿,这个问题很容易解决。我假设将来你可能会有更多的标签需要居中,所以我制作了这个函数来接受你想要居中的标签和父面板。此代码适用于 Visual C# Windows 表单应用程序。在调用此函数之前,我们必须做一些事情。我们需要:

  • Select 标签并将其锚点设置为左、右
  • 删除自动调整大小
  • 将标签 TextAlign 设置为 MiddleCenter

这是您需要为我们的功能编写的代码

        public void Centroid(Label label, Panel parent)
        {
            int x = (parent.Size.Width - label.Size.Width) / 2;
            label.Location = new Point(x, label.Location.Y);
        }

并调用您必须键入的函数:Centroid(label1, panel1); 这是假设您有一个名为 label1 的标签和一个名为 panel 1 的面板。您可以用任何东西替换这些值,只要它是标签和面板即可。

希望对您有所帮助:)

我对面板和标签进行了水平居中对齐:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        label1 = new System.Windows.Forms.Label();
        this.SuspendLayout(); 
        this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 23F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(1401, 462);
        this.Controls.Add(this.label1);
        this.Font = new System.Drawing.Font("Times New Roman", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Margin = new System.Windows.Forms.Padding(6, 5, 6, 5);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
        this.PerformLayout();


        int borderThickness = 5;
        Color borderColor = Color.Cyan;
        CustomPanel panel1 = new CustomPanel(borderThickness, borderColor);
        panel1.BackColor = Color.Yellow;
        panel1.Location = new Point(400, 30);
        panel1.Size = new Size(300, 300);
        panel1.Parent = this;
        this.Controls.Add(panel1);

        label1.Name = "label1";
        label1.TabIndex = 0;
        label1.AutoSize = true;
        label1.ForeColor = Color.Black;
        label1.Text = "this is the text whose center I want to align";
        label1.Location = new Point(panel1.Location.X + panel1.Width / 2 - label1.Width / 2, 80);
        if (this.Controls.Contains(label1))
        {
            label1.BringToFront();
        }
    }

    private Label label1;
}

自从我发布了答案后,我发现为了让标签与面板的中心对齐,语句:

this.Controls.Add(label1);

必须位于语句之后:

label1 = new Label();

在语句之前:

label1.Location = new Point(panel1.Location.X + panel1.Width / 2 - label1.Width / 2, 80);