[英]How to center a Label inside a Panel without setting Dock to Fill
我試圖創建一個帶有邊框的自定義面板,其顏色可以更改,以便在某些條件下“突出顯示”面板。
小組還需要通過案文傳達某些信息。 為此,我在Panel中添加了一個Label。 我已嘗試使用規定的方法對Label進行居中,但出於某種原因,它始終將其放在Panel的左上角。 我無法將Label的Dock設置為Fill,因為它掩蓋了已創建的自定義邊框。 所以我需要做到這一點,以便Label適合邊框。
Label的Anchor設置為None,其位置為
new Point((ClientSize.Width - Size.Width)/2, (ClientSize.Height - Size.Height)/2);
自定義Panel的代碼是:
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);
}
編輯:我看過類似的問題並嘗試更改Label的屬性,但沒有結果。
// 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)
};
我也試過改變Panel的Padding,也沒有結果。
Padding = new Padding(5);
編輯:嘗試以編程方式將Label放置在Panel的中心(產生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);
將控件垂直和水平放置在容器中心
最簡單的選擇是使用帶有1列和1行而不是Panel
的TableLayoutPanel
。 將Label
放入其中,然后將Label
的Anchor
設置為None
,使標簽始終垂直和水平居中。
另外,為了繪制自定義邊框,它足以處理TableLayoutPanel
CellPaint
事件並繪制自定義邊框:
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窗體應用程序。 在調用此函數之前,我們需要做一些事情。 我們要:
這是您需要為我們的函數編寫的代碼
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.