2016-08-18 14 views
1

我试图创建一个带有边框的自定义面板,其颜色可以更改以在特定条件下“突出显示”面板。如何将面板中的标签居中而不设置底座填充

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

标记锚点设置为无,它的位置是

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

的[居中控制可能的复制)?](http://stackoverflow.com/questions/491399/centering-controls-within-a-form-in-net-winforms) – Breeze

+1

将标签放置在中间并将Anchor左,右和自动大小设置为false –

+0

I认为我的问题是与Locatio ñ。设置锚点和AutoSize属性不会执行任何操作。面板中间的计算是否正确? – NickV987

回答

3

enter image description here

我们可以通过简单的步骤实现这一目标

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

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

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

您可以更具体地了解如何在面板中间以编程方式放置标签?我不一定会知道事先需要创建多少个面板,所以我需要以编程方式创建每个面板和每个Label。 – NickV987

+0

int X =(面板宽度 - 标签宽度)/ 2; Label.Location = new Point(X,height); –

+0

好的,我刚刚在创建标签后添加了该标签。但是现在标签完全消失了。位置显然是(-9,496)... – NickV987

0

保持一个控制垂直和水平方向在中心容器

的最简单的方法是使用一个带有TableLayoutPanel 1列和第1行,而不是一个Panel的。将Label放入其中,则将标签的Anchor设置为None就足以使标签始终处于垂直和水平中心位置。

2 1

而且画自定义边框,它足以应付的TableLayoutPanelCellPaint事件并绘制自定义边框:在.NET(一种形式的WinForms内

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

这是使用设计器将父控件集中在父母中的最简单的选项,大多数用户不知道“TableLayoutPanel”中的这种功能。 –