将控件垂直和水平保持在其容器的中心

Keep a Control vertically and horizontally at center of its container(将控件垂直和水平保持在其容器的中心)

本文介绍了将控件垂直和水平保持在其容器的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建一个带有边框的自定义面板,其颜色可以更改,以便在某些情况下"突出显示"面板。

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

标签的Anchor设置为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;
  • 将标签TextAlign设置为MiddleCenter;

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

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

这篇关于将控件垂直和水平保持在其容器的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将控件垂直和水平保持在其容器的中心

基础教程推荐