【问题标题】:Keep a Control vertically and horizontally at center of its container如何在不将 Dock 设置为 Fill 的情况下将标签在面板内居中
【发布时间】:2016-12-25 17:16:56
【问题描述】:

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

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

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

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
  • 我认为我的问题在于位置。设置 Anchor 和 AutoSize 属性不会做任何事情。我对面板中间的计算是否正确?
  • @Breeze 我已经看过了,那里的计算与我所拥有的基本相同。我什至尝试使用 Top 和 Left 属性来实现它,但没有运气。我也尝试过调整 Padding 属性,但仍然没有。
  • 在工具箱中的所有控件中,Label 和 PictureBox 是迄今为止最浪费的。只是点击方便,它们不值得您节省很长时间的单行代码。请改用 TextRenderer.DrawText()。你需要 ResizeRedraw = true 在构造函数中。

标签: c# winforms label border center


【解决方案1】:

在容器中心垂直和水平地保持一个控件

最简单的选项是使用具有 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);
}

【讨论】:

  • 这是使用设计器在父级中居中控件的最简单选项,大多数用户不知道 TableLayoutPanel 中的此类功能。
【解决方案2】:

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

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);

【讨论】:

    【解决方案3】:

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

    • 选择标签并将其锚点设置为左、右
    • 移除 AutoSize
    • 将标签 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 的面板。您可以用任何值替换这些值,只要它是一个标签和一个面板。

    希望对你有帮助:)

    【讨论】:

      【解决方案4】:

      我们可以通过简单的步骤来实现这一点

      • 将标签锚点设置为左右
      • 将标签 AutoSize 设置为 false ;
      • 将 Label TextAlign 设置为 MiddleCenter;

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

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

      【讨论】:

      • 您能否更具体地了解如何以编程方式将标签放置在面板中间?我不一定知道我需要事先创建多少个面板,所以我需要以编程方式创建每个面板和每个标签。
      • int X =(面板宽度 - 标签宽度)/ 2 ; Label.Location = new Point(X, height);
      • 好的,我在创建标签后添加了它。但是现在标签完全消失了。位置显然是 (-9, 496)...
      • @NickV987 使用panel1.Size.Width 而不是ClientSize 因为panel1 是标签的父控件
      • @ManuVarghese TableLayoutPanel 有一个特殊的布局引擎,只需将子级的Anchor 设置为None,您就可以将控件保持在其中心。看看我分享的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 2016-12-08
      • 2010-12-21
      • 2021-11-14
      • 2011-04-18
      相关资源
      最近更新 更多